2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 1. 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Takahiro Harada
17 typedef 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)
28 #define m_numScanBlocks z
39 u32 ScanExclusive(__local u32* data, u32 n, int lIdx, int lSize)
43 for(int nActive=n>>1; nActive>0; nActive>>=1, offset<<=1)
46 for(int iIdx=lIdx; iIdx<nActive; iIdx+=lSize)
48 int ai = offset*(2*iIdx+1)-1;
49 int bi = offset*(2*iIdx+2)-1;
58 blocksum = data[ n-1 ];
65 for(int nActive=1; nActive<n; nActive<<=1, offset>>=1 )
68 for( int iIdx = lIdx; iIdx<nActive; iIdx += lSize )
70 int ai = offset*(2*iIdx+1)-1;
71 int bi = offset*(2*iIdx+2)-1;
82 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
84 void LocalScanKernel(__global u32* dst, __global u32 *src, __global u32 *sumBuffer,
87 __local u32 ldsData[WG_SIZE*2];
89 int gIdx = GET_GLOBAL_IDX;
90 int lIdx = GET_LOCAL_IDX;
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;
95 u32 sum = ScanExclusive(ldsData, WG_SIZE*2, GET_LOCAL_IDX, GET_GROUP_SIZE);
97 if( lIdx == 0 ) sumBuffer[GET_GROUP_IDX] = sum;
99 if( (2*gIdx) < cb.m_numElems )
101 dst[2*gIdx] = ldsData[2*lIdx];
103 if( (2*gIdx + 1) < cb.m_numElems )
105 dst[2*gIdx + 1] = ldsData[2*lIdx + 1];
109 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
111 void AddOffsetKernel(__global u32 *dst, __global u32 *blockSum, uint4 cb)
113 const u32 blockSize = WG_SIZE*2;
115 int myIdx = GET_GROUP_IDX+1;
116 int lIdx = GET_LOCAL_IDX;
118 u32 iBlockSum = blockSum[myIdx];
120 int endValue = min((myIdx+1)*(blockSize), cb.m_numElems);
121 for(int i=myIdx*blockSize+lIdx; i<endValue; i+=GET_GROUP_SIZE)
127 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
129 void TopLevelScanKernel(__global u32* dst, uint4 cb)
131 __local u32 ldsData[2048];
132 int gIdx = GET_GLOBAL_IDX;
133 int lIdx = GET_LOCAL_IDX;
134 int lSize = GET_GROUP_SIZE;
136 for(int i=lIdx; i<cb.m_numScanBlocks; i+=lSize )
138 ldsData[i] = (i<cb.m_numBlocks)? dst[i]:0;
143 u32 sum = ScanExclusive(ldsData, cb.m_numScanBlocks, GET_LOCAL_IDX, GET_GROUP_SIZE);
145 for(int i=lIdx; i<cb.m_numBlocks; i+=lSize )
152 dst[cb.m_numBlocks] = sum;