1/*
2Copyright (c) 2012 Advanced Micro Devices, Inc.
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. This notice may not be removed or altered from any source distribution.
13*/
14//Originally written by Takahiro Harada
15
16
17typedef unsigned int u32;
18#define GET_GROUP_IDX get_group_id(0)
19#define GET_LOCAL_IDX get_local_id(0)
20#define GET_GLOBAL_IDX get_global_id(0)
21#define GET_GROUP_SIZE get_local_size(0)
22#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
23
24// takahiro end
25#define WG_SIZE 128
26#define m_numElems x
27#define m_numBlocks y
28#define m_numScanBlocks z
29
30/*typedef struct
31{
32	uint m_numElems;
33	uint m_numBlocks;
34	uint m_numScanBlocks;
35	uint m_padding[1];
36} ConstBuffer;
37*/
38
39u32 ScanExclusive(__local u32* data, u32 n, int lIdx, int lSize)
40{
41	u32 blocksum;
42    int offset = 1;
43    for(int nActive=n>>1; nActive>0; nActive>>=1, offset<<=1)
44    {
45        GROUP_LDS_BARRIER;
46        for(int iIdx=lIdx; iIdx<nActive; iIdx+=lSize)
47        {
48            int ai = offset*(2*iIdx+1)-1;
49            int bi = offset*(2*iIdx+2)-1;
50            data[bi] += data[ai];
51        }
52	}
53
54    GROUP_LDS_BARRIER;
55
56    if( lIdx == 0 )
57	{
58		blocksum = data[ n-1 ];
59        data[ n-1 ] = 0;
60	}
61
62	GROUP_LDS_BARRIER;
63
64	offset >>= 1;
65    for(int nActive=1; nActive<n; nActive<<=1, offset>>=1 )
66    {
67        GROUP_LDS_BARRIER;
68        for( int iIdx = lIdx; iIdx<nActive; iIdx += lSize )
69        {
70            int ai = offset*(2*iIdx+1)-1;
71            int bi = offset*(2*iIdx+2)-1;
72            u32 temp = data[ai];
73            data[ai] = data[bi];
74            data[bi] += temp;
75        }
76	}
77	GROUP_LDS_BARRIER;
78
79	return blocksum;
80}
81
82__attribute__((reqd_work_group_size(WG_SIZE,1,1)))
83__kernel
84void LocalScanKernel(__global u32* dst, __global u32 *src, __global u32 *sumBuffer,
85		uint4 cb)
86{
87	__local u32 ldsData[WG_SIZE*2];
88
89	int gIdx = GET_GLOBAL_IDX;
90	int lIdx = GET_LOCAL_IDX;
91
92	ldsData[2*lIdx]     = ( 2*gIdx < cb.m_numElems )? src[2*gIdx]: 0;
93	ldsData[2*lIdx + 1] = ( 2*gIdx+1 < cb.m_numElems )? src[2*gIdx + 1]: 0;
94
95	u32 sum = ScanExclusive(ldsData, WG_SIZE*2, GET_LOCAL_IDX, GET_GROUP_SIZE);
96
97	if( lIdx == 0 ) sumBuffer[GET_GROUP_IDX] = sum;
98
99	if( (2*gIdx) < cb.m_numElems )
100    {
101        dst[2*gIdx]     = ldsData[2*lIdx];
102	}
103	if( (2*gIdx + 1) < cb.m_numElems )
104	{
105        dst[2*gIdx + 1] = ldsData[2*lIdx + 1];
106    }
107}
108
109__attribute__((reqd_work_group_size(WG_SIZE,1,1)))
110__kernel
111void AddOffsetKernel(__global u32 *dst, __global u32 *blockSum, uint4 cb)
112{
113	const u32 blockSize = WG_SIZE*2;
114
115	int myIdx = GET_GROUP_IDX+1;
116	int lIdx = GET_LOCAL_IDX;
117
118	u32 iBlockSum = blockSum[myIdx];
119
120	int endValue = min((myIdx+1)*(blockSize), cb.m_numElems);
121	for(int i=myIdx*blockSize+lIdx; i<endValue; i+=GET_GROUP_SIZE)
122	{
123		dst[i] += iBlockSum;
124	}
125}
126
127__attribute__((reqd_work_group_size(WG_SIZE,1,1)))
128__kernel
129void TopLevelScanKernel(__global u32* dst, uint4 cb)
130{
131	__local u32 ldsData[2048];
132	int gIdx = GET_GLOBAL_IDX;
133	int lIdx = GET_LOCAL_IDX;
134	int lSize = GET_GROUP_SIZE;
135
136	for(int i=lIdx; i<cb.m_numScanBlocks; i+=lSize )
137	{
138		ldsData[i] = (i<cb.m_numBlocks)? dst[i]:0;
139	}
140
141	GROUP_LDS_BARRIER;
142
143	u32 sum = ScanExclusive(ldsData, cb.m_numScanBlocks, GET_LOCAL_IDX, GET_GROUP_SIZE);
144
145	for(int i=lIdx; i<cb.m_numBlocks; i+=lSize )
146	{
147		dst[i] = ldsData[i];
148	}
149
150	if( gIdx == 0 )
151	{
152		dst[cb.m_numBlocks] = sum;
153	}
154}
155