[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / ParallelPrimitives / kernels / RadixSort32KernelsCL.h
1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* radixSort32KernelsCL =
3         "/*\n"
4         "Bullet Continuous Collision Detection and Physics Library\n"
5         "Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org\n"
6         "This software is provided 'as-is', without any express or implied warranty.\n"
7         "In no event will the authors be held liable for any damages arising from the use of this software.\n"
8         "Permission is granted to anyone to use this software for any purpose, \n"
9         "including commercial applications, and to alter it and redistribute it freely, \n"
10         "subject to the following restrictions:\n"
11         "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.\n"
12         "2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"
13         "3. This notice may not be removed or altered from any source distribution.\n"
14         "*/\n"
15         "//Author Takahiro Harada\n"
16         "//#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
17         "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
18         "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
19         "typedef unsigned int u32;\n"
20         "#define GET_GROUP_IDX get_group_id(0)\n"
21         "#define GET_LOCAL_IDX get_local_id(0)\n"
22         "#define GET_GLOBAL_IDX get_global_id(0)\n"
23         "#define GET_GROUP_SIZE get_local_size(0)\n"
24         "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"
25         "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"
26         "#define AtomInc(x) atom_inc(&(x))\n"
27         "#define AtomInc1(x, out) out = atom_inc(&(x))\n"
28         "#define AtomAdd(x, value) atom_add(&(x), value)\n"
29         "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"
30         "#define make_uint4 (uint4)\n"
31         "#define make_uint2 (uint2)\n"
32         "#define make_int2 (int2)\n"
33         "#define WG_SIZE 64\n"
34         "#define ELEMENTS_PER_WORK_ITEM (256/WG_SIZE)\n"
35         "#define BITS_PER_PASS 4\n"
36         "#define NUM_BUCKET (1<<BITS_PER_PASS)\n"
37         "typedef uchar u8;\n"
38         "//     this isn't optimization for VLIW. But just reducing writes. \n"
39         "#define USE_2LEVEL_REDUCE 1\n"
40         "//#define CHECK_BOUNDARY 1\n"
41         "//#define NV_GPU 1\n"
42         "//     Cypress\n"
43         "#define nPerWI 16\n"
44         "//     Cayman\n"
45         "//#define nPerWI 20\n"
46         "#define m_n x\n"
47         "#define m_nWGs y\n"
48         "#define m_startBit z\n"
49         "#define m_nBlocksPerWG w\n"
50         "/*\n"
51         "typedef struct\n"
52         "{\n"
53         "       int m_n;\n"
54         "       int m_nWGs;\n"
55         "       int m_startBit;\n"
56         "       int m_nBlocksPerWG;\n"
57         "} ConstBuffer;\n"
58         "*/\n"
59         "typedef struct\n"
60         "{\n"
61         "       unsigned int m_key;\n"
62         "       unsigned int m_value;\n"
63         "} SortDataCL;\n"
64         "uint prefixScanVectorEx( uint4* data )\n"
65         "{\n"
66         "       u32 sum = 0;\n"
67         "       u32 tmp = data[0].x;\n"
68         "       data[0].x = sum;\n"
69         "       sum += tmp;\n"
70         "       tmp = data[0].y;\n"
71         "       data[0].y = sum;\n"
72         "       sum += tmp;\n"
73         "       tmp = data[0].z;\n"
74         "       data[0].z = sum;\n"
75         "       sum += tmp;\n"
76         "       tmp = data[0].w;\n"
77         "       data[0].w = sum;\n"
78         "       sum += tmp;\n"
79         "       return sum;\n"
80         "}\n"
81         "u32 localPrefixSum( u32 pData, uint lIdx, uint* totalSum, __local u32* sorterSharedMemory, int wgSize /*64 or 128*/ )\n"
82         "{\n"
83         "       {       //      Set data\n"
84         "               sorterSharedMemory[lIdx] = 0;\n"
85         "               sorterSharedMemory[lIdx+wgSize] = pData;\n"
86         "       }\n"
87         "       GROUP_LDS_BARRIER;\n"
88         "       {       //      Prefix sum\n"
89         "               int idx = 2*lIdx + (wgSize+1);\n"
90         "#if defined(USE_2LEVEL_REDUCE)\n"
91         "               if( lIdx < 64 )\n"
92         "               {\n"
93         "                       u32 u0, u1, u2;\n"
94         "                       u0 = sorterSharedMemory[idx-3];\n"
95         "                       u1 = sorterSharedMemory[idx-2];\n"
96         "                       u2 = sorterSharedMemory[idx-1];\n"
97         "                       AtomAdd( sorterSharedMemory[idx], u0+u1+u2 );                   \n"
98         "                       GROUP_MEM_FENCE;\n"
99         "                       u0 = sorterSharedMemory[idx-12];\n"
100         "                       u1 = sorterSharedMemory[idx-8];\n"
101         "                       u2 = sorterSharedMemory[idx-4];\n"
102         "                       AtomAdd( sorterSharedMemory[idx], u0+u1+u2 );                   \n"
103         "                       GROUP_MEM_FENCE;\n"
104         "                       u0 = sorterSharedMemory[idx-48];\n"
105         "                       u1 = sorterSharedMemory[idx-32];\n"
106         "                       u2 = sorterSharedMemory[idx-16];\n"
107         "                       AtomAdd( sorterSharedMemory[idx], u0+u1+u2 );                   \n"
108         "                       GROUP_MEM_FENCE;\n"
109         "                       if( wgSize > 64 )\n"
110         "                       {\n"
111         "                               sorterSharedMemory[idx] += sorterSharedMemory[idx-64];\n"
112         "                               GROUP_MEM_FENCE;\n"
113         "                       }\n"
114         "                       sorterSharedMemory[idx-1] += sorterSharedMemory[idx-2];\n"
115         "                       GROUP_MEM_FENCE;\n"
116         "               }\n"
117         "#else\n"
118         "               if( lIdx < 64 )\n"
119         "               {\n"
120         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-1];\n"
121         "                       GROUP_MEM_FENCE;\n"
122         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-2];                   \n"
123         "                       GROUP_MEM_FENCE;\n"
124         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-4];\n"
125         "                       GROUP_MEM_FENCE;\n"
126         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-8];\n"
127         "                       GROUP_MEM_FENCE;\n"
128         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-16];\n"
129         "                       GROUP_MEM_FENCE;\n"
130         "                       sorterSharedMemory[idx] += sorterSharedMemory[idx-32];\n"
131         "                       GROUP_MEM_FENCE;\n"
132         "                       if( wgSize > 64 )\n"
133         "                       {\n"
134         "                               sorterSharedMemory[idx] += sorterSharedMemory[idx-64];\n"
135         "                               GROUP_MEM_FENCE;\n"
136         "                       }\n"
137         "                       sorterSharedMemory[idx-1] += sorterSharedMemory[idx-2];\n"
138         "                       GROUP_MEM_FENCE;\n"
139         "               }\n"
140         "#endif\n"
141         "       }\n"
142         "       GROUP_LDS_BARRIER;\n"
143         "       *totalSum = sorterSharedMemory[wgSize*2-1];\n"
144         "       u32 addValue = sorterSharedMemory[lIdx+wgSize-1];\n"
145         "       return addValue;\n"
146         "}\n"
147         "//__attribute__((reqd_work_group_size(128,1,1)))\n"
148         "uint4 localPrefixSum128V( uint4 pData, uint lIdx, uint* totalSum, __local u32* sorterSharedMemory )\n"
149         "{\n"
150         "       u32 s4 = prefixScanVectorEx( &pData );\n"
151         "       u32 rank = localPrefixSum( s4, lIdx, totalSum, sorterSharedMemory, 128 );\n"
152         "       return pData + make_uint4( rank, rank, rank, rank );\n"
153         "}\n"
154         "//__attribute__((reqd_work_group_size(64,1,1)))\n"
155         "uint4 localPrefixSum64V( uint4 pData, uint lIdx, uint* totalSum, __local u32* sorterSharedMemory )\n"
156         "{\n"
157         "       u32 s4 = prefixScanVectorEx( &pData );\n"
158         "       u32 rank = localPrefixSum( s4, lIdx, totalSum, sorterSharedMemory, 64 );\n"
159         "       return pData + make_uint4( rank, rank, rank, rank );\n"
160         "}\n"
161         "u32 unpack4Key( u32 key, int keyIdx ){ return (key>>(keyIdx*8)) & 0xff;}\n"
162         "u32 bit8Scan(u32 v)\n"
163         "{\n"
164         "       return (v<<8) + (v<<16) + (v<<24);\n"
165         "}\n"
166         "//===\n"
167         "#define MY_HISTOGRAM(idx) localHistogramMat[(idx)*WG_SIZE+lIdx]\n"
168         "__kernel\n"
169         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
170         "void StreamCountKernel( __global u32* gSrc, __global u32* histogramOut, int4 cb )\n"
171         "{\n"
172         "       __local u32 localHistogramMat[NUM_BUCKET*WG_SIZE];\n"
173         "       u32 gIdx = GET_GLOBAL_IDX;\n"
174         "       u32 lIdx = GET_LOCAL_IDX;\n"
175         "       u32 wgIdx = GET_GROUP_IDX;\n"
176         "       u32 wgSize = GET_GROUP_SIZE;\n"
177         "       const int startBit = cb.m_startBit;\n"
178         "       const int n = cb.m_n;\n"
179         "       const int nWGs = cb.m_nWGs;\n"
180         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
181         "       for(int i=0; i<NUM_BUCKET; i++)\n"
182         "       {\n"
183         "               MY_HISTOGRAM(i) = 0;\n"
184         "       }\n"
185         "       GROUP_LDS_BARRIER;\n"
186         "       const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
187         "       u32 localKey;\n"
188         "       int nBlocks = (n)/blockSize - nBlocksPerWG*wgIdx;\n"
189         "       int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
190         "       for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++, addr+=blockSize)\n"
191         "       {\n"
192         "               //      MY_HISTOGRAM( localKeys.x ) ++ is much expensive than atomic add as it requires read and write while atomics can just add on AMD\n"
193         "               //      Using registers didn't perform well. It seems like use localKeys to address requires a lot of alu ops\n"
194         "               //      AMD: AtomInc performs better while NV prefers ++\n"
195         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
196         "               {\n"
197         "#if defined(CHECK_BOUNDARY)\n"
198         "                       if( addr+i < n )\n"
199         "#endif\n"
200         "                       {\n"
201         "                               localKey = (gSrc[addr+i]>>startBit) & 0xf;\n"
202         "#if defined(NV_GPU)\n"
203         "                               MY_HISTOGRAM( localKey )++;\n"
204         "#else\n"
205         "                               AtomInc( MY_HISTOGRAM( localKey ) );\n"
206         "#endif\n"
207         "                       }\n"
208         "               }\n"
209         "       }\n"
210         "       GROUP_LDS_BARRIER;\n"
211         "       \n"
212         "       if( lIdx < NUM_BUCKET )\n"
213         "       {\n"
214         "               u32 sum = 0;\n"
215         "               for(int i=0; i<GET_GROUP_SIZE; i++)\n"
216         "               {\n"
217         "                       sum += localHistogramMat[lIdx*WG_SIZE+(i+lIdx)%GET_GROUP_SIZE];\n"
218         "               }\n"
219         "               histogramOut[lIdx*nWGs+wgIdx] = sum;\n"
220         "       }\n"
221         "}\n"
222         "__kernel\n"
223         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
224         "void StreamCountSortDataKernel( __global SortDataCL* gSrc, __global u32* histogramOut, int4  cb )\n"
225         "{\n"
226         "       __local u32 localHistogramMat[NUM_BUCKET*WG_SIZE];\n"
227         "       u32 gIdx = GET_GLOBAL_IDX;\n"
228         "       u32 lIdx = GET_LOCAL_IDX;\n"
229         "       u32 wgIdx = GET_GROUP_IDX;\n"
230         "       u32 wgSize = GET_GROUP_SIZE;\n"
231         "       const int startBit = cb.m_startBit;\n"
232         "       const int n = cb.m_n;\n"
233         "       const int nWGs = cb.m_nWGs;\n"
234         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
235         "       for(int i=0; i<NUM_BUCKET; i++)\n"
236         "       {\n"
237         "               MY_HISTOGRAM(i) = 0;\n"
238         "       }\n"
239         "       GROUP_LDS_BARRIER;\n"
240         "       const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
241         "       u32 localKey;\n"
242         "       int nBlocks = (n)/blockSize - nBlocksPerWG*wgIdx;\n"
243         "       int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
244         "       for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++, addr+=blockSize)\n"
245         "       {\n"
246         "               //      MY_HISTOGRAM( localKeys.x ) ++ is much expensive than atomic add as it requires read and write while atomics can just add on AMD\n"
247         "               //      Using registers didn't perform well. It seems like use localKeys to address requires a lot of alu ops\n"
248         "               //      AMD: AtomInc performs better while NV prefers ++\n"
249         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
250         "               {\n"
251         "#if defined(CHECK_BOUNDARY)\n"
252         "                       if( addr+i < n )\n"
253         "#endif\n"
254         "                       {\n"
255         "                               localKey = (gSrc[addr+i].m_key>>startBit) & 0xf;\n"
256         "#if defined(NV_GPU)\n"
257         "                               MY_HISTOGRAM( localKey )++;\n"
258         "#else\n"
259         "                               AtomInc( MY_HISTOGRAM( localKey ) );\n"
260         "#endif\n"
261         "                       }\n"
262         "               }\n"
263         "       }\n"
264         "       GROUP_LDS_BARRIER;\n"
265         "       \n"
266         "       if( lIdx < NUM_BUCKET )\n"
267         "       {\n"
268         "               u32 sum = 0;\n"
269         "               for(int i=0; i<GET_GROUP_SIZE; i++)\n"
270         "               {\n"
271         "                       sum += localHistogramMat[lIdx*WG_SIZE+(i+lIdx)%GET_GROUP_SIZE];\n"
272         "               }\n"
273         "               histogramOut[lIdx*nWGs+wgIdx] = sum;\n"
274         "       }\n"
275         "}\n"
276         "#define nPerLane (nPerWI/4)\n"
277         "//     NUM_BUCKET*nWGs < 128*nPerWI\n"
278         "__kernel\n"
279         "__attribute__((reqd_work_group_size(128,1,1)))\n"
280         "void PrefixScanKernel( __global u32* wHistogram1, int4  cb )\n"
281         "{\n"
282         "       __local u32 ldsTopScanData[128*2];\n"
283         "       u32 lIdx = GET_LOCAL_IDX;\n"
284         "       u32 wgIdx = GET_GROUP_IDX;\n"
285         "       const int nWGs = cb.m_nWGs;\n"
286         "       u32 data[nPerWI];\n"
287         "       for(int i=0; i<nPerWI; i++)\n"
288         "       {\n"
289         "               data[i] = 0;\n"
290         "               if( (nPerWI*lIdx+i) < NUM_BUCKET*nWGs )\n"
291         "                       data[i] = wHistogram1[nPerWI*lIdx+i];\n"
292         "       }\n"
293         "       uint4 myData = make_uint4(0,0,0,0);\n"
294         "       for(int i=0; i<nPerLane; i++)\n"
295         "       {\n"
296         "               myData.x += data[nPerLane*0+i];\n"
297         "               myData.y += data[nPerLane*1+i];\n"
298         "               myData.z += data[nPerLane*2+i];\n"
299         "               myData.w += data[nPerLane*3+i];\n"
300         "       }\n"
301         "       uint totalSum;\n"
302         "       uint4 scanned = localPrefixSum128V( myData, lIdx, &totalSum, ldsTopScanData );\n"
303         "//     for(int j=0; j<4; j++) //       somehow it introduces a lot of branches\n"
304         "       {       int j = 0;\n"
305         "               u32 sum = 0;\n"
306         "               for(int i=0; i<nPerLane; i++)\n"
307         "               {\n"
308         "                       u32 tmp = data[nPerLane*j+i];\n"
309         "                       data[nPerLane*j+i] = sum;\n"
310         "                       sum += tmp;\n"
311         "               }\n"
312         "       }\n"
313         "       {       int j = 1;\n"
314         "               u32 sum = 0;\n"
315         "               for(int i=0; i<nPerLane; i++)\n"
316         "               {\n"
317         "                       u32 tmp = data[nPerLane*j+i];\n"
318         "                       data[nPerLane*j+i] = sum;\n"
319         "                       sum += tmp;\n"
320         "               }\n"
321         "       }\n"
322         "       {       int j = 2;\n"
323         "               u32 sum = 0;\n"
324         "               for(int i=0; i<nPerLane; i++)\n"
325         "               {\n"
326         "                       u32 tmp = data[nPerLane*j+i];\n"
327         "                       data[nPerLane*j+i] = sum;\n"
328         "                       sum += tmp;\n"
329         "               }\n"
330         "       }\n"
331         "       {       int j = 3;\n"
332         "               u32 sum = 0;\n"
333         "               for(int i=0; i<nPerLane; i++)\n"
334         "               {\n"
335         "                       u32 tmp = data[nPerLane*j+i];\n"
336         "                       data[nPerLane*j+i] = sum;\n"
337         "                       sum += tmp;\n"
338         "               }\n"
339         "       }\n"
340         "       for(int i=0; i<nPerLane; i++)\n"
341         "       {\n"
342         "               data[nPerLane*0+i] += scanned.x;\n"
343         "               data[nPerLane*1+i] += scanned.y;\n"
344         "               data[nPerLane*2+i] += scanned.z;\n"
345         "               data[nPerLane*3+i] += scanned.w;\n"
346         "       }\n"
347         "       for(int i=0; i<nPerWI; i++)\n"
348         "       {\n"
349         "               int index = nPerWI*lIdx+i;\n"
350         "               if (index < NUM_BUCKET*nWGs)\n"
351         "                       wHistogram1[nPerWI*lIdx+i] = data[i];\n"
352         "       }\n"
353         "}\n"
354         "//     4 scan, 4 exchange\n"
355         "void sort4Bits(u32 sortData[4], int startBit, int lIdx, __local u32* ldsSortData)\n"
356         "{\n"
357         "       for(int bitIdx=0; bitIdx<BITS_PER_PASS; bitIdx++)\n"
358         "       {\n"
359         "               u32 mask = (1<<bitIdx);\n"
360         "               uint4 cmpResult = make_uint4( (sortData[0]>>startBit) & mask, (sortData[1]>>startBit) & mask, (sortData[2]>>startBit) & mask, (sortData[3]>>startBit) & mask );\n"
361         "               uint4 prefixSum = SELECT_UINT4( make_uint4(1,1,1,1), make_uint4(0,0,0,0), cmpResult != make_uint4(0,0,0,0) );\n"
362         "               u32 total;\n"
363         "               prefixSum = localPrefixSum64V( prefixSum, lIdx, &total, ldsSortData );\n"
364         "               {\n"
365         "                       uint4 localAddr = make_uint4(lIdx*4+0,lIdx*4+1,lIdx*4+2,lIdx*4+3);\n"
366         "                       uint4 dstAddr = localAddr - prefixSum + make_uint4( total, total, total, total );\n"
367         "                       dstAddr = SELECT_UINT4( prefixSum, dstAddr, cmpResult != make_uint4(0, 0, 0, 0) );\n"
368         "                       GROUP_LDS_BARRIER;\n"
369         "                       ldsSortData[dstAddr.x] = sortData[0];\n"
370         "                       ldsSortData[dstAddr.y] = sortData[1];\n"
371         "                       ldsSortData[dstAddr.z] = sortData[2];\n"
372         "                       ldsSortData[dstAddr.w] = sortData[3];\n"
373         "                       GROUP_LDS_BARRIER;\n"
374         "                       sortData[0] = ldsSortData[localAddr.x];\n"
375         "                       sortData[1] = ldsSortData[localAddr.y];\n"
376         "                       sortData[2] = ldsSortData[localAddr.z];\n"
377         "                       sortData[3] = ldsSortData[localAddr.w];\n"
378         "                       GROUP_LDS_BARRIER;\n"
379         "               }\n"
380         "       }\n"
381         "}\n"
382         "//     2 scan, 2 exchange\n"
383         "void sort4Bits1(u32 sortData[4], int startBit, int lIdx, __local u32* ldsSortData)\n"
384         "{\n"
385         "       for(uint ibit=0; ibit<BITS_PER_PASS; ibit+=2)\n"
386         "       {\n"
387         "               uint4 b = make_uint4((sortData[0]>>(startBit+ibit)) & 0x3, \n"
388         "                       (sortData[1]>>(startBit+ibit)) & 0x3, \n"
389         "                       (sortData[2]>>(startBit+ibit)) & 0x3, \n"
390         "                       (sortData[3]>>(startBit+ibit)) & 0x3);\n"
391         "               u32 key4;\n"
392         "               u32 sKeyPacked[4] = { 0, 0, 0, 0 };\n"
393         "               {\n"
394         "                       sKeyPacked[0] |= 1<<(8*b.x);\n"
395         "                       sKeyPacked[1] |= 1<<(8*b.y);\n"
396         "                       sKeyPacked[2] |= 1<<(8*b.z);\n"
397         "                       sKeyPacked[3] |= 1<<(8*b.w);\n"
398         "                       key4 = sKeyPacked[0] + sKeyPacked[1] + sKeyPacked[2] + sKeyPacked[3];\n"
399         "               }\n"
400         "               u32 rankPacked;\n"
401         "               u32 sumPacked;\n"
402         "               {\n"
403         "                       rankPacked = localPrefixSum( key4, lIdx, &sumPacked, ldsSortData, WG_SIZE );\n"
404         "               }\n"
405         "               GROUP_LDS_BARRIER;\n"
406         "               u32 newOffset[4] = { 0,0,0,0 };\n"
407         "               {\n"
408         "                       u32 sumScanned = bit8Scan( sumPacked );\n"
409         "                       u32 scannedKeys[4];\n"
410         "                       scannedKeys[0] = 1<<(8*b.x);\n"
411         "                       scannedKeys[1] = 1<<(8*b.y);\n"
412         "                       scannedKeys[2] = 1<<(8*b.z);\n"
413         "                       scannedKeys[3] = 1<<(8*b.w);\n"
414         "                       {       //      4 scans at once\n"
415         "                               u32 sum4 = 0;\n"
416         "                               for(int ie=0; ie<4; ie++)\n"
417         "                               {\n"
418         "                                       u32 tmp = scannedKeys[ie];\n"
419         "                                       scannedKeys[ie] = sum4;\n"
420         "                                       sum4 += tmp;\n"
421         "                               }\n"
422         "                       }\n"
423         "                       {\n"
424         "                               u32 sumPlusRank = sumScanned + rankPacked;\n"
425         "                               {       u32 ie = b.x;\n"
426         "                                       scannedKeys[0] += sumPlusRank;\n"
427         "                                       newOffset[0] = unpack4Key( scannedKeys[0], ie );\n"
428         "                               }\n"
429         "                               {       u32 ie = b.y;\n"
430         "                                       scannedKeys[1] += sumPlusRank;\n"
431         "                                       newOffset[1] = unpack4Key( scannedKeys[1], ie );\n"
432         "                               }\n"
433         "                               {       u32 ie = b.z;\n"
434         "                                       scannedKeys[2] += sumPlusRank;\n"
435         "                                       newOffset[2] = unpack4Key( scannedKeys[2], ie );\n"
436         "                               }\n"
437         "                               {       u32 ie = b.w;\n"
438         "                                       scannedKeys[3] += sumPlusRank;\n"
439         "                                       newOffset[3] = unpack4Key( scannedKeys[3], ie );\n"
440         "                               }\n"
441         "                       }\n"
442         "               }\n"
443         "               GROUP_LDS_BARRIER;\n"
444         "               {\n"
445         "                       ldsSortData[newOffset[0]] = sortData[0];\n"
446         "                       ldsSortData[newOffset[1]] = sortData[1];\n"
447         "                       ldsSortData[newOffset[2]] = sortData[2];\n"
448         "                       ldsSortData[newOffset[3]] = sortData[3];\n"
449         "                       GROUP_LDS_BARRIER;\n"
450         "                       u32 dstAddr = 4*lIdx;\n"
451         "                       sortData[0] = ldsSortData[dstAddr+0];\n"
452         "                       sortData[1] = ldsSortData[dstAddr+1];\n"
453         "                       sortData[2] = ldsSortData[dstAddr+2];\n"
454         "                       sortData[3] = ldsSortData[dstAddr+3];\n"
455         "                       GROUP_LDS_BARRIER;\n"
456         "               }\n"
457         "       }\n"
458         "}\n"
459         "#define SET_HISTOGRAM(setIdx, key) ldsSortData[(setIdx)*NUM_BUCKET+key]\n"
460         "__kernel\n"
461         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
462         "void SortAndScatterKernel( __global const u32* restrict gSrc, __global const u32* rHistogram, __global u32* restrict gDst, int4  cb )\n"
463         "{\n"
464         "       __local u32 ldsSortData[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n"
465         "       __local u32 localHistogramToCarry[NUM_BUCKET];\n"
466         "       __local u32 localHistogram[NUM_BUCKET*2];\n"
467         "       u32 gIdx = GET_GLOBAL_IDX;\n"
468         "       u32 lIdx = GET_LOCAL_IDX;\n"
469         "       u32 wgIdx = GET_GROUP_IDX;\n"
470         "       u32 wgSize = GET_GROUP_SIZE;\n"
471         "       const int n = cb.m_n;\n"
472         "       const int nWGs = cb.m_nWGs;\n"
473         "       const int startBit = cb.m_startBit;\n"
474         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
475         "       if( lIdx < (NUM_BUCKET) )\n"
476         "       {\n"
477         "               localHistogramToCarry[lIdx] = rHistogram[lIdx*nWGs + wgIdx];\n"
478         "       }\n"
479         "       GROUP_LDS_BARRIER;\n"
480         "       const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
481         "       int nBlocks = n/blockSize - nBlocksPerWG*wgIdx;\n"
482         "       int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
483         "       for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++, addr+=blockSize)\n"
484         "       {\n"
485         "               u32 myHistogram = 0;\n"
486         "               u32 sortData[ELEMENTS_PER_WORK_ITEM];\n"
487         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
488         "#if defined(CHECK_BOUNDARY)\n"
489         "                       sortData[i] = ( addr+i < n )? gSrc[ addr+i ] : 0xffffffff;\n"
490         "#else\n"
491         "                       sortData[i] = gSrc[ addr+i ];\n"
492         "#endif\n"
493         "               sort4Bits(sortData, startBit, lIdx, ldsSortData);\n"
494         "               u32 keys[ELEMENTS_PER_WORK_ITEM];\n"
495         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
496         "                       keys[i] = (sortData[i]>>startBit) & 0xf;\n"
497         "               {       //      create histogram\n"
498         "                       u32 setIdx = lIdx/16;\n"
499         "                       if( lIdx < NUM_BUCKET )\n"
500         "                       {\n"
501         "                               localHistogram[lIdx] = 0;\n"
502         "                       }\n"
503         "                       ldsSortData[lIdx] = 0;\n"
504         "                       GROUP_LDS_BARRIER;\n"
505         "                       for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
506         "#if defined(CHECK_BOUNDARY)\n"
507         "                               if( addr+i < n )\n"
508         "#endif\n"
509         "#if defined(NV_GPU)\n"
510         "                               SET_HISTOGRAM( setIdx, keys[i] )++;\n"
511         "#else\n"
512         "                               AtomInc( SET_HISTOGRAM( setIdx, keys[i] ) );\n"
513         "#endif\n"
514         "                       \n"
515         "                       GROUP_LDS_BARRIER;\n"
516         "                       \n"
517         "                       uint hIdx = NUM_BUCKET+lIdx;\n"
518         "                       if( lIdx < NUM_BUCKET )\n"
519         "                       {\n"
520         "                               u32 sum = 0;\n"
521         "                               for(int i=0; i<WG_SIZE/16; i++)\n"
522         "                               {\n"
523         "                                       sum += SET_HISTOGRAM( i, lIdx );\n"
524         "                               }\n"
525         "                               myHistogram = sum;\n"
526         "                               localHistogram[hIdx] = sum;\n"
527         "                       }\n"
528         "                       GROUP_LDS_BARRIER;\n"
529         "#if defined(USE_2LEVEL_REDUCE)\n"
530         "                       if( lIdx < NUM_BUCKET )\n"
531         "                       {\n"
532         "                               localHistogram[hIdx] = localHistogram[hIdx-1];\n"
533         "                               GROUP_MEM_FENCE;\n"
534         "                               u32 u0, u1, u2;\n"
535         "                               u0 = localHistogram[hIdx-3];\n"
536         "                               u1 = localHistogram[hIdx-2];\n"
537         "                               u2 = localHistogram[hIdx-1];\n"
538         "                               AtomAdd( localHistogram[hIdx], u0 + u1 + u2 );\n"
539         "                               GROUP_MEM_FENCE;\n"
540         "                               u0 = localHistogram[hIdx-12];\n"
541         "                               u1 = localHistogram[hIdx-8];\n"
542         "                               u2 = localHistogram[hIdx-4];\n"
543         "                               AtomAdd( localHistogram[hIdx], u0 + u1 + u2 );\n"
544         "                               GROUP_MEM_FENCE;\n"
545         "                       }\n"
546         "#else\n"
547         "                       if( lIdx < NUM_BUCKET )\n"
548         "                       {\n"
549         "                               localHistogram[hIdx] = localHistogram[hIdx-1];\n"
550         "                               GROUP_MEM_FENCE;\n"
551         "                               localHistogram[hIdx] += localHistogram[hIdx-1];\n"
552         "                               GROUP_MEM_FENCE;\n"
553         "                               localHistogram[hIdx] += localHistogram[hIdx-2];\n"
554         "                               GROUP_MEM_FENCE;\n"
555         "                               localHistogram[hIdx] += localHistogram[hIdx-4];\n"
556         "                               GROUP_MEM_FENCE;\n"
557         "                               localHistogram[hIdx] += localHistogram[hIdx-8];\n"
558         "                               GROUP_MEM_FENCE;\n"
559         "                       }\n"
560         "#endif\n"
561         "                       GROUP_LDS_BARRIER;\n"
562         "               }\n"
563         "               {\n"
564         "                       for(int ie=0; ie<ELEMENTS_PER_WORK_ITEM; ie++)\n"
565         "                       {\n"
566         "                               int dataIdx = ELEMENTS_PER_WORK_ITEM*lIdx+ie;\n"
567         "                               int binIdx = keys[ie];\n"
568         "                               int groupOffset = localHistogramToCarry[binIdx];\n"
569         "                               int myIdx = dataIdx - localHistogram[NUM_BUCKET+binIdx];\n"
570         "#if defined(CHECK_BOUNDARY)\n"
571         "                               if( addr+ie < n )\n"
572         "#endif\n"
573         "                               gDst[ groupOffset + myIdx ] = sortData[ie];\n"
574         "                       }\n"
575         "               }\n"
576         "               GROUP_LDS_BARRIER;\n"
577         "               if( lIdx < NUM_BUCKET )\n"
578         "               {\n"
579         "                       localHistogramToCarry[lIdx] += myHistogram;\n"
580         "               }\n"
581         "               GROUP_LDS_BARRIER;\n"
582         "       }\n"
583         "}\n"
584         "//     2 scan, 2 exchange\n"
585         "void sort4Bits1KeyValue(u32 sortData[4], int sortVal[4], int startBit, int lIdx, __local u32* ldsSortData, __local int *ldsSortVal)\n"
586         "{\n"
587         "       for(uint ibit=0; ibit<BITS_PER_PASS; ibit+=2)\n"
588         "       {\n"
589         "               uint4 b = make_uint4((sortData[0]>>(startBit+ibit)) & 0x3, \n"
590         "                       (sortData[1]>>(startBit+ibit)) & 0x3, \n"
591         "                       (sortData[2]>>(startBit+ibit)) & 0x3, \n"
592         "                       (sortData[3]>>(startBit+ibit)) & 0x3);\n"
593         "               u32 key4;\n"
594         "               u32 sKeyPacked[4] = { 0, 0, 0, 0 };\n"
595         "               {\n"
596         "                       sKeyPacked[0] |= 1<<(8*b.x);\n"
597         "                       sKeyPacked[1] |= 1<<(8*b.y);\n"
598         "                       sKeyPacked[2] |= 1<<(8*b.z);\n"
599         "                       sKeyPacked[3] |= 1<<(8*b.w);\n"
600         "                       key4 = sKeyPacked[0] + sKeyPacked[1] + sKeyPacked[2] + sKeyPacked[3];\n"
601         "               }\n"
602         "               u32 rankPacked;\n"
603         "               u32 sumPacked;\n"
604         "               {\n"
605         "                       rankPacked = localPrefixSum( key4, lIdx, &sumPacked, ldsSortData, WG_SIZE );\n"
606         "               }\n"
607         "               GROUP_LDS_BARRIER;\n"
608         "               u32 newOffset[4] = { 0,0,0,0 };\n"
609         "               {\n"
610         "                       u32 sumScanned = bit8Scan( sumPacked );\n"
611         "                       u32 scannedKeys[4];\n"
612         "                       scannedKeys[0] = 1<<(8*b.x);\n"
613         "                       scannedKeys[1] = 1<<(8*b.y);\n"
614         "                       scannedKeys[2] = 1<<(8*b.z);\n"
615         "                       scannedKeys[3] = 1<<(8*b.w);\n"
616         "                       {       //      4 scans at once\n"
617         "                               u32 sum4 = 0;\n"
618         "                               for(int ie=0; ie<4; ie++)\n"
619         "                               {\n"
620         "                                       u32 tmp = scannedKeys[ie];\n"
621         "                                       scannedKeys[ie] = sum4;\n"
622         "                                       sum4 += tmp;\n"
623         "                               }\n"
624         "                       }\n"
625         "                       {\n"
626         "                               u32 sumPlusRank = sumScanned + rankPacked;\n"
627         "                               {       u32 ie = b.x;\n"
628         "                                       scannedKeys[0] += sumPlusRank;\n"
629         "                                       newOffset[0] = unpack4Key( scannedKeys[0], ie );\n"
630         "                               }\n"
631         "                               {       u32 ie = b.y;\n"
632         "                                       scannedKeys[1] += sumPlusRank;\n"
633         "                                       newOffset[1] = unpack4Key( scannedKeys[1], ie );\n"
634         "                               }\n"
635         "                               {       u32 ie = b.z;\n"
636         "                                       scannedKeys[2] += sumPlusRank;\n"
637         "                                       newOffset[2] = unpack4Key( scannedKeys[2], ie );\n"
638         "                               }\n"
639         "                               {       u32 ie = b.w;\n"
640         "                                       scannedKeys[3] += sumPlusRank;\n"
641         "                                       newOffset[3] = unpack4Key( scannedKeys[3], ie );\n"
642         "                               }\n"
643         "                       }\n"
644         "               }\n"
645         "               GROUP_LDS_BARRIER;\n"
646         "               {\n"
647         "                       ldsSortData[newOffset[0]] = sortData[0];\n"
648         "                       ldsSortData[newOffset[1]] = sortData[1];\n"
649         "                       ldsSortData[newOffset[2]] = sortData[2];\n"
650         "                       ldsSortData[newOffset[3]] = sortData[3];\n"
651         "                       ldsSortVal[newOffset[0]] = sortVal[0];\n"
652         "                       ldsSortVal[newOffset[1]] = sortVal[1];\n"
653         "                       ldsSortVal[newOffset[2]] = sortVal[2];\n"
654         "                       ldsSortVal[newOffset[3]] = sortVal[3];\n"
655         "                       GROUP_LDS_BARRIER;\n"
656         "                       u32 dstAddr = 4*lIdx;\n"
657         "                       sortData[0] = ldsSortData[dstAddr+0];\n"
658         "                       sortData[1] = ldsSortData[dstAddr+1];\n"
659         "                       sortData[2] = ldsSortData[dstAddr+2];\n"
660         "                       sortData[3] = ldsSortData[dstAddr+3];\n"
661         "                       sortVal[0] = ldsSortVal[dstAddr+0];\n"
662         "                       sortVal[1] = ldsSortVal[dstAddr+1];\n"
663         "                       sortVal[2] = ldsSortVal[dstAddr+2];\n"
664         "                       sortVal[3] = ldsSortVal[dstAddr+3];\n"
665         "                       GROUP_LDS_BARRIER;\n"
666         "               }\n"
667         "       }\n"
668         "}\n"
669         "__kernel\n"
670         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
671         "void SortAndScatterSortDataKernel( __global const SortDataCL* restrict gSrc, __global const u32* rHistogram, __global SortDataCL* restrict gDst, int4 cb)\n"
672         "{\n"
673         "       __local int ldsSortData[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n"
674         "       __local int ldsSortVal[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n"
675         "       __local u32 localHistogramToCarry[NUM_BUCKET];\n"
676         "       __local u32 localHistogram[NUM_BUCKET*2];\n"
677         "       u32 gIdx = GET_GLOBAL_IDX;\n"
678         "       u32 lIdx = GET_LOCAL_IDX;\n"
679         "       u32 wgIdx = GET_GROUP_IDX;\n"
680         "       u32 wgSize = GET_GROUP_SIZE;\n"
681         "       const int n = cb.m_n;\n"
682         "       const int nWGs = cb.m_nWGs;\n"
683         "       const int startBit = cb.m_startBit;\n"
684         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
685         "       if( lIdx < (NUM_BUCKET) )\n"
686         "       {\n"
687         "               localHistogramToCarry[lIdx] = rHistogram[lIdx*nWGs + wgIdx];\n"
688         "       }\n"
689         "       GROUP_LDS_BARRIER;\n"
690         "    \n"
691         "       const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
692         "       int nBlocks = n/blockSize - nBlocksPerWG*wgIdx;\n"
693         "       int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
694         "       for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++, addr+=blockSize)\n"
695         "       {\n"
696         "               u32 myHistogram = 0;\n"
697         "               int sortData[ELEMENTS_PER_WORK_ITEM];\n"
698         "               int sortVal[ELEMENTS_PER_WORK_ITEM];\n"
699         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
700         "#if defined(CHECK_BOUNDARY)\n"
701         "               {\n"
702         "                       sortData[i] = ( addr+i < n )? gSrc[ addr+i ].m_key : 0xffffffff;\n"
703         "                       sortVal[i] = ( addr+i < n )? gSrc[ addr+i ].m_value : 0xffffffff;\n"
704         "               }\n"
705         "#else\n"
706         "               {\n"
707         "                       sortData[i] = gSrc[ addr+i ].m_key;\n"
708         "                       sortVal[i] = gSrc[ addr+i ].m_value;\n"
709         "               }\n"
710         "#endif\n"
711         "               sort4Bits1KeyValue(sortData, sortVal, startBit, lIdx, ldsSortData, ldsSortVal);\n"
712         "               u32 keys[ELEMENTS_PER_WORK_ITEM];\n"
713         "               for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
714         "                       keys[i] = (sortData[i]>>startBit) & 0xf;\n"
715         "               {       //      create histogram\n"
716         "                       u32 setIdx = lIdx/16;\n"
717         "                       if( lIdx < NUM_BUCKET )\n"
718         "                       {\n"
719         "                               localHistogram[lIdx] = 0;\n"
720         "                       }\n"
721         "                       ldsSortData[lIdx] = 0;\n"
722         "                       GROUP_LDS_BARRIER;\n"
723         "                       for(int i=0; i<ELEMENTS_PER_WORK_ITEM; i++)\n"
724         "#if defined(CHECK_BOUNDARY)\n"
725         "                               if( addr+i < n )\n"
726         "#endif\n"
727         "#if defined(NV_GPU)\n"
728         "                               SET_HISTOGRAM( setIdx, keys[i] )++;\n"
729         "#else\n"
730         "                               AtomInc( SET_HISTOGRAM( setIdx, keys[i] ) );\n"
731         "#endif\n"
732         "                       \n"
733         "                       GROUP_LDS_BARRIER;\n"
734         "                       \n"
735         "                       uint hIdx = NUM_BUCKET+lIdx;\n"
736         "                       if( lIdx < NUM_BUCKET )\n"
737         "                       {\n"
738         "                               u32 sum = 0;\n"
739         "                               for(int i=0; i<WG_SIZE/16; i++)\n"
740         "                               {\n"
741         "                                       sum += SET_HISTOGRAM( i, lIdx );\n"
742         "                               }\n"
743         "                               myHistogram = sum;\n"
744         "                               localHistogram[hIdx] = sum;\n"
745         "                       }\n"
746         "                       GROUP_LDS_BARRIER;\n"
747         "#if defined(USE_2LEVEL_REDUCE)\n"
748         "                       if( lIdx < NUM_BUCKET )\n"
749         "                       {\n"
750         "                               localHistogram[hIdx] = localHistogram[hIdx-1];\n"
751         "                               GROUP_MEM_FENCE;\n"
752         "                               u32 u0, u1, u2;\n"
753         "                               u0 = localHistogram[hIdx-3];\n"
754         "                               u1 = localHistogram[hIdx-2];\n"
755         "                               u2 = localHistogram[hIdx-1];\n"
756         "                               AtomAdd( localHistogram[hIdx], u0 + u1 + u2 );\n"
757         "                               GROUP_MEM_FENCE;\n"
758         "                               u0 = localHistogram[hIdx-12];\n"
759         "                               u1 = localHistogram[hIdx-8];\n"
760         "                               u2 = localHistogram[hIdx-4];\n"
761         "                               AtomAdd( localHistogram[hIdx], u0 + u1 + u2 );\n"
762         "                               GROUP_MEM_FENCE;\n"
763         "                       }\n"
764         "#else\n"
765         "                       if( lIdx < NUM_BUCKET )\n"
766         "                       {\n"
767         "                               localHistogram[hIdx] = localHistogram[hIdx-1];\n"
768         "                               GROUP_MEM_FENCE;\n"
769         "                               localHistogram[hIdx] += localHistogram[hIdx-1];\n"
770         "                               GROUP_MEM_FENCE;\n"
771         "                               localHistogram[hIdx] += localHistogram[hIdx-2];\n"
772         "                               GROUP_MEM_FENCE;\n"
773         "                               localHistogram[hIdx] += localHistogram[hIdx-4];\n"
774         "                               GROUP_MEM_FENCE;\n"
775         "                               localHistogram[hIdx] += localHistogram[hIdx-8];\n"
776         "                               GROUP_MEM_FENCE;\n"
777         "                       }\n"
778         "#endif\n"
779         "                       GROUP_LDS_BARRIER;\n"
780         "               }\n"
781         "       {\n"
782         "                       for(int ie=0; ie<ELEMENTS_PER_WORK_ITEM; ie++)\n"
783         "                       {\n"
784         "                               int dataIdx = ELEMENTS_PER_WORK_ITEM*lIdx+ie;\n"
785         "                               int binIdx = keys[ie];\n"
786         "                               int groupOffset = localHistogramToCarry[binIdx];\n"
787         "                               int myIdx = dataIdx - localHistogram[NUM_BUCKET+binIdx];\n"
788         "#if defined(CHECK_BOUNDARY)\n"
789         "                               if( addr+ie < n )\n"
790         "                               {\n"
791         "                    if ((groupOffset + myIdx)<n)\n"
792         "                    {\n"
793         "                        if (sortData[ie]==sortVal[ie])\n"
794         "                        {\n"
795         "                            \n"
796         "                            SortDataCL tmp;\n"
797         "                            tmp.m_key = sortData[ie];\n"
798         "                            tmp.m_value = sortVal[ie];\n"
799         "                            if (tmp.m_key == tmp.m_value)\n"
800         "                                gDst[groupOffset + myIdx ] = tmp;\n"
801         "                        }\n"
802         "                        \n"
803         "                    }\n"
804         "                               }\n"
805         "#else\n"
806         "                if ((groupOffset + myIdx)<n)\n"
807         "                {\n"
808         "                    gDst[ groupOffset + myIdx ].m_key = sortData[ie];\n"
809         "                    gDst[ groupOffset + myIdx ].m_value = sortVal[ie];\n"
810         "                }\n"
811         "#endif\n"
812         "                       }\n"
813         "               }\n"
814         "               GROUP_LDS_BARRIER;\n"
815         "               if( lIdx < NUM_BUCKET )\n"
816         "               {\n"
817         "                       localHistogramToCarry[lIdx] += myHistogram;\n"
818         "               }\n"
819         "               GROUP_LDS_BARRIER;\n"
820         "       }\n"
821         "}\n"
822         "__kernel\n"
823         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
824         "void SortAndScatterSortDataKernelSerial( __global const SortDataCL* restrict gSrc, __global const u32* rHistogram, __global SortDataCL* restrict gDst, int4 cb)\n"
825         "{\n"
826         "    \n"
827         "       u32 gIdx = GET_GLOBAL_IDX;\n"
828         "       u32 realLocalIdx = GET_LOCAL_IDX;\n"
829         "       u32 wgIdx = GET_GROUP_IDX;\n"
830         "       u32 wgSize = GET_GROUP_SIZE;\n"
831         "       const int startBit = cb.m_startBit;\n"
832         "       const int n = cb.m_n;\n"
833         "       const int nWGs = cb.m_nWGs;\n"
834         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
835         "    int counter[NUM_BUCKET];\n"
836         "    \n"
837         "    if (realLocalIdx>0)\n"
838         "        return;\n"
839         "    \n"
840         "    for (int c=0;c<NUM_BUCKET;c++)\n"
841         "        counter[c]=0;\n"
842         "    const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
843         "       \n"
844         "       int nBlocks = (n)/blockSize - nBlocksPerWG*wgIdx;\n"
845         "   for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++)\n"
846         "  {\n"
847         "     for (int lIdx=0;lIdx<WG_SIZE;lIdx++)\n"
848         "       {\n"
849         "        int addr2 = iblock*blockSize + blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
850         "        \n"
851         "               for(int j=0; j<ELEMENTS_PER_WORK_ITEM; j++)\n"
852         "               {\n"
853         "            int i = addr2+j;\n"
854         "                       if( i < n )\n"
855         "                       {\n"
856         "                int tableIdx;\n"
857         "                               tableIdx = (gSrc[i].m_key>>startBit) & 0xf;//0xf = NUM_TABLES-1\n"
858         "                gDst[rHistogram[tableIdx*nWGs+wgIdx] + counter[tableIdx]] = gSrc[i];\n"
859         "                counter[tableIdx] ++;\n"
860         "                       }\n"
861         "               }\n"
862         "       }\n"
863         "  }\n"
864         "    \n"
865         "}\n"
866         "__kernel\n"
867         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
868         "void SortAndScatterKernelSerial( __global const u32* restrict gSrc, __global const u32* rHistogram, __global u32* restrict gDst, int4  cb )\n"
869         "{\n"
870         "    \n"
871         "       u32 gIdx = GET_GLOBAL_IDX;\n"
872         "       u32 realLocalIdx = GET_LOCAL_IDX;\n"
873         "       u32 wgIdx = GET_GROUP_IDX;\n"
874         "       u32 wgSize = GET_GROUP_SIZE;\n"
875         "       const int startBit = cb.m_startBit;\n"
876         "       const int n = cb.m_n;\n"
877         "       const int nWGs = cb.m_nWGs;\n"
878         "       const int nBlocksPerWG = cb.m_nBlocksPerWG;\n"
879         "    int counter[NUM_BUCKET];\n"
880         "    \n"
881         "    if (realLocalIdx>0)\n"
882         "        return;\n"
883         "    \n"
884         "    for (int c=0;c<NUM_BUCKET;c++)\n"
885         "        counter[c]=0;\n"
886         "    const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n"
887         "       \n"
888         "       int nBlocks = (n)/blockSize - nBlocksPerWG*wgIdx;\n"
889         "   for(int iblock=0; iblock<min(nBlocksPerWG, nBlocks); iblock++)\n"
890         "  {\n"
891         "     for (int lIdx=0;lIdx<WG_SIZE;lIdx++)\n"
892         "       {\n"
893         "        int addr2 = iblock*blockSize + blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n"
894         "        \n"
895         "               for(int j=0; j<ELEMENTS_PER_WORK_ITEM; j++)\n"
896         "               {\n"
897         "            int i = addr2+j;\n"
898         "                       if( i < n )\n"
899         "                       {\n"
900         "                int tableIdx;\n"
901         "                               tableIdx = (gSrc[i]>>startBit) & 0xf;//0xf = NUM_TABLES-1\n"
902         "                gDst[rHistogram[tableIdx*nWGs+wgIdx] + counter[tableIdx]] = gSrc[i];\n"
903         "                counter[tableIdx] ++;\n"
904         "                       }\n"
905         "               }\n"
906         "       }\n"
907         "  }\n"
908         "    \n"
909         "}\n";