//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project static const char* radixSort32KernelsCL= \ "/*\n" "Bullet Continuous Collision Detection and Physics Library\n" "Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org\n" "This software is provided 'as-is', without any express or implied warranty.\n" "In no event will the authors be held liable for any damages arising from the use of this software.\n" "Permission is granted to anyone to use this software for any purpose, \n" "including commercial applications, and to alter it and redistribute it freely, \n" "subject to the following restrictions:\n" "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" "2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n" "3. This notice may not be removed or altered from any source distribution.\n" "*/\n" "//Author Takahiro Harada\n" "//#pragma OPENCL EXTENSION cl_amd_printf : enable\n" "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n" "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n" "typedef unsigned int u32;\n" "#define GET_GROUP_IDX get_group_id(0)\n" "#define GET_LOCAL_IDX get_local_id(0)\n" "#define GET_GLOBAL_IDX get_global_id(0)\n" "#define GET_GROUP_SIZE get_local_size(0)\n" "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n" "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n" "#define AtomInc(x) atom_inc(&(x))\n" "#define AtomInc1(x, out) out = atom_inc(&(x))\n" "#define AtomAdd(x, value) atom_add(&(x), value)\n" "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n" "#define make_uint4 (uint4)\n" "#define make_uint2 (uint2)\n" "#define make_int2 (int2)\n" "#define WG_SIZE 64\n" "#define ELEMENTS_PER_WORK_ITEM (256/WG_SIZE)\n" "#define BITS_PER_PASS 4\n" "#define NUM_BUCKET (1< 64 )\n" " {\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-64];\n" " GROUP_MEM_FENCE;\n" " }\n" " sorterSharedMemory[idx-1] += sorterSharedMemory[idx-2];\n" " GROUP_MEM_FENCE;\n" " }\n" "#else\n" " if( lIdx < 64 )\n" " {\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-1];\n" " GROUP_MEM_FENCE;\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-2]; \n" " GROUP_MEM_FENCE;\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-4];\n" " GROUP_MEM_FENCE;\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-8];\n" " GROUP_MEM_FENCE;\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-16];\n" " GROUP_MEM_FENCE;\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-32];\n" " GROUP_MEM_FENCE;\n" " if( wgSize > 64 )\n" " {\n" " sorterSharedMemory[idx] += sorterSharedMemory[idx-64];\n" " GROUP_MEM_FENCE;\n" " }\n" " sorterSharedMemory[idx-1] += sorterSharedMemory[idx-2];\n" " GROUP_MEM_FENCE;\n" " }\n" "#endif\n" " }\n" " GROUP_LDS_BARRIER;\n" " *totalSum = sorterSharedMemory[wgSize*2-1];\n" " u32 addValue = sorterSharedMemory[lIdx+wgSize-1];\n" " return addValue;\n" "}\n" "//__attribute__((reqd_work_group_size(128,1,1)))\n" "uint4 localPrefixSum128V( uint4 pData, uint lIdx, uint* totalSum, __local u32* sorterSharedMemory )\n" "{\n" " u32 s4 = prefixScanVectorEx( &pData );\n" " u32 rank = localPrefixSum( s4, lIdx, totalSum, sorterSharedMemory, 128 );\n" " return pData + make_uint4( rank, rank, rank, rank );\n" "}\n" "//__attribute__((reqd_work_group_size(64,1,1)))\n" "uint4 localPrefixSum64V( uint4 pData, uint lIdx, uint* totalSum, __local u32* sorterSharedMemory )\n" "{\n" " u32 s4 = prefixScanVectorEx( &pData );\n" " u32 rank = localPrefixSum( s4, lIdx, totalSum, sorterSharedMemory, 64 );\n" " return pData + make_uint4( rank, rank, rank, rank );\n" "}\n" "u32 unpack4Key( u32 key, int keyIdx ){ return (key>>(keyIdx*8)) & 0xff;}\n" "u32 bit8Scan(u32 v)\n" "{\n" " return (v<<8) + (v<<16) + (v<<24);\n" "}\n" "//===\n" "#define MY_HISTOGRAM(idx) localHistogramMat[(idx)*WG_SIZE+lIdx]\n" "__kernel\n" "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n" "void StreamCountKernel( __global u32* gSrc, __global u32* histogramOut, int4 cb )\n" "{\n" " __local u32 localHistogramMat[NUM_BUCKET*WG_SIZE];\n" " u32 gIdx = GET_GLOBAL_IDX;\n" " u32 lIdx = GET_LOCAL_IDX;\n" " u32 wgIdx = GET_GROUP_IDX;\n" " u32 wgSize = GET_GROUP_SIZE;\n" " const int startBit = cb.m_startBit;\n" " const int n = cb.m_n;\n" " const int nWGs = cb.m_nWGs;\n" " const int nBlocksPerWG = cb.m_nBlocksPerWG;\n" " for(int i=0; i>startBit) & 0xf;\n" "#if defined(NV_GPU)\n" " MY_HISTOGRAM( localKey )++;\n" "#else\n" " AtomInc( MY_HISTOGRAM( localKey ) );\n" "#endif\n" " }\n" " }\n" " }\n" " GROUP_LDS_BARRIER;\n" " \n" " if( lIdx < NUM_BUCKET )\n" " {\n" " u32 sum = 0;\n" " for(int i=0; i>startBit) & 0xf;\n" "#if defined(NV_GPU)\n" " MY_HISTOGRAM( localKey )++;\n" "#else\n" " AtomInc( MY_HISTOGRAM( localKey ) );\n" "#endif\n" " }\n" " }\n" " }\n" " GROUP_LDS_BARRIER;\n" " \n" " if( lIdx < NUM_BUCKET )\n" " {\n" " u32 sum = 0;\n" " for(int i=0; i>startBit) & mask, (sortData[1]>>startBit) & mask, (sortData[2]>>startBit) & mask, (sortData[3]>>startBit) & mask );\n" " uint4 prefixSum = SELECT_UINT4( make_uint4(1,1,1,1), make_uint4(0,0,0,0), cmpResult != make_uint4(0,0,0,0) );\n" " u32 total;\n" " prefixSum = localPrefixSum64V( prefixSum, lIdx, &total, ldsSortData );\n" " {\n" " uint4 localAddr = make_uint4(lIdx*4+0,lIdx*4+1,lIdx*4+2,lIdx*4+3);\n" " uint4 dstAddr = localAddr - prefixSum + make_uint4( total, total, total, total );\n" " dstAddr = SELECT_UINT4( prefixSum, dstAddr, cmpResult != make_uint4(0, 0, 0, 0) );\n" " GROUP_LDS_BARRIER;\n" " ldsSortData[dstAddr.x] = sortData[0];\n" " ldsSortData[dstAddr.y] = sortData[1];\n" " ldsSortData[dstAddr.z] = sortData[2];\n" " ldsSortData[dstAddr.w] = sortData[3];\n" " GROUP_LDS_BARRIER;\n" " sortData[0] = ldsSortData[localAddr.x];\n" " sortData[1] = ldsSortData[localAddr.y];\n" " sortData[2] = ldsSortData[localAddr.z];\n" " sortData[3] = ldsSortData[localAddr.w];\n" " GROUP_LDS_BARRIER;\n" " }\n" " }\n" "}\n" "// 2 scan, 2 exchange\n" "void sort4Bits1(u32 sortData[4], int startBit, int lIdx, __local u32* ldsSortData)\n" "{\n" " for(uint ibit=0; ibit>(startBit+ibit)) & 0x3, \n" " (sortData[1]>>(startBit+ibit)) & 0x3, \n" " (sortData[2]>>(startBit+ibit)) & 0x3, \n" " (sortData[3]>>(startBit+ibit)) & 0x3);\n" " u32 key4;\n" " u32 sKeyPacked[4] = { 0, 0, 0, 0 };\n" " {\n" " sKeyPacked[0] |= 1<<(8*b.x);\n" " sKeyPacked[1] |= 1<<(8*b.y);\n" " sKeyPacked[2] |= 1<<(8*b.z);\n" " sKeyPacked[3] |= 1<<(8*b.w);\n" " key4 = sKeyPacked[0] + sKeyPacked[1] + sKeyPacked[2] + sKeyPacked[3];\n" " }\n" " u32 rankPacked;\n" " u32 sumPacked;\n" " {\n" " rankPacked = localPrefixSum( key4, lIdx, &sumPacked, ldsSortData, WG_SIZE );\n" " }\n" " GROUP_LDS_BARRIER;\n" " u32 newOffset[4] = { 0,0,0,0 };\n" " {\n" " u32 sumScanned = bit8Scan( sumPacked );\n" " u32 scannedKeys[4];\n" " scannedKeys[0] = 1<<(8*b.x);\n" " scannedKeys[1] = 1<<(8*b.y);\n" " scannedKeys[2] = 1<<(8*b.z);\n" " scannedKeys[3] = 1<<(8*b.w);\n" " { // 4 scans at once\n" " u32 sum4 = 0;\n" " for(int ie=0; ie<4; ie++)\n" " {\n" " u32 tmp = scannedKeys[ie];\n" " scannedKeys[ie] = sum4;\n" " sum4 += tmp;\n" " }\n" " }\n" " {\n" " u32 sumPlusRank = sumScanned + rankPacked;\n" " { u32 ie = b.x;\n" " scannedKeys[0] += sumPlusRank;\n" " newOffset[0] = unpack4Key( scannedKeys[0], ie );\n" " }\n" " { u32 ie = b.y;\n" " scannedKeys[1] += sumPlusRank;\n" " newOffset[1] = unpack4Key( scannedKeys[1], ie );\n" " }\n" " { u32 ie = b.z;\n" " scannedKeys[2] += sumPlusRank;\n" " newOffset[2] = unpack4Key( scannedKeys[2], ie );\n" " }\n" " { u32 ie = b.w;\n" " scannedKeys[3] += sumPlusRank;\n" " newOffset[3] = unpack4Key( scannedKeys[3], ie );\n" " }\n" " }\n" " }\n" " GROUP_LDS_BARRIER;\n" " {\n" " ldsSortData[newOffset[0]] = sortData[0];\n" " ldsSortData[newOffset[1]] = sortData[1];\n" " ldsSortData[newOffset[2]] = sortData[2];\n" " ldsSortData[newOffset[3]] = sortData[3];\n" " GROUP_LDS_BARRIER;\n" " u32 dstAddr = 4*lIdx;\n" " sortData[0] = ldsSortData[dstAddr+0];\n" " sortData[1] = ldsSortData[dstAddr+1];\n" " sortData[2] = ldsSortData[dstAddr+2];\n" " sortData[3] = ldsSortData[dstAddr+3];\n" " GROUP_LDS_BARRIER;\n" " }\n" " }\n" "}\n" "#define SET_HISTOGRAM(setIdx, key) ldsSortData[(setIdx)*NUM_BUCKET+key]\n" "__kernel\n" "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n" "void SortAndScatterKernel( __global const u32* restrict gSrc, __global const u32* rHistogram, __global u32* restrict gDst, int4 cb )\n" "{\n" " __local u32 ldsSortData[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n" " __local u32 localHistogramToCarry[NUM_BUCKET];\n" " __local u32 localHistogram[NUM_BUCKET*2];\n" " u32 gIdx = GET_GLOBAL_IDX;\n" " u32 lIdx = GET_LOCAL_IDX;\n" " u32 wgIdx = GET_GROUP_IDX;\n" " u32 wgSize = GET_GROUP_SIZE;\n" " const int n = cb.m_n;\n" " const int nWGs = cb.m_nWGs;\n" " const int startBit = cb.m_startBit;\n" " const int nBlocksPerWG = cb.m_nBlocksPerWG;\n" " if( lIdx < (NUM_BUCKET) )\n" " {\n" " localHistogramToCarry[lIdx] = rHistogram[lIdx*nWGs + wgIdx];\n" " }\n" " GROUP_LDS_BARRIER;\n" " const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n" " int nBlocks = n/blockSize - nBlocksPerWG*wgIdx;\n" " int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n" " for(int iblock=0; iblock>startBit) & 0xf;\n" " { // create histogram\n" " u32 setIdx = lIdx/16;\n" " if( lIdx < NUM_BUCKET )\n" " {\n" " localHistogram[lIdx] = 0;\n" " }\n" " ldsSortData[lIdx] = 0;\n" " GROUP_LDS_BARRIER;\n" " for(int i=0; i>(startBit+ibit)) & 0x3, \n" " (sortData[1]>>(startBit+ibit)) & 0x3, \n" " (sortData[2]>>(startBit+ibit)) & 0x3, \n" " (sortData[3]>>(startBit+ibit)) & 0x3);\n" " u32 key4;\n" " u32 sKeyPacked[4] = { 0, 0, 0, 0 };\n" " {\n" " sKeyPacked[0] |= 1<<(8*b.x);\n" " sKeyPacked[1] |= 1<<(8*b.y);\n" " sKeyPacked[2] |= 1<<(8*b.z);\n" " sKeyPacked[3] |= 1<<(8*b.w);\n" " key4 = sKeyPacked[0] + sKeyPacked[1] + sKeyPacked[2] + sKeyPacked[3];\n" " }\n" " u32 rankPacked;\n" " u32 sumPacked;\n" " {\n" " rankPacked = localPrefixSum( key4, lIdx, &sumPacked, ldsSortData, WG_SIZE );\n" " }\n" " GROUP_LDS_BARRIER;\n" " u32 newOffset[4] = { 0,0,0,0 };\n" " {\n" " u32 sumScanned = bit8Scan( sumPacked );\n" " u32 scannedKeys[4];\n" " scannedKeys[0] = 1<<(8*b.x);\n" " scannedKeys[1] = 1<<(8*b.y);\n" " scannedKeys[2] = 1<<(8*b.z);\n" " scannedKeys[3] = 1<<(8*b.w);\n" " { // 4 scans at once\n" " u32 sum4 = 0;\n" " for(int ie=0; ie<4; ie++)\n" " {\n" " u32 tmp = scannedKeys[ie];\n" " scannedKeys[ie] = sum4;\n" " sum4 += tmp;\n" " }\n" " }\n" " {\n" " u32 sumPlusRank = sumScanned + rankPacked;\n" " { u32 ie = b.x;\n" " scannedKeys[0] += sumPlusRank;\n" " newOffset[0] = unpack4Key( scannedKeys[0], ie );\n" " }\n" " { u32 ie = b.y;\n" " scannedKeys[1] += sumPlusRank;\n" " newOffset[1] = unpack4Key( scannedKeys[1], ie );\n" " }\n" " { u32 ie = b.z;\n" " scannedKeys[2] += sumPlusRank;\n" " newOffset[2] = unpack4Key( scannedKeys[2], ie );\n" " }\n" " { u32 ie = b.w;\n" " scannedKeys[3] += sumPlusRank;\n" " newOffset[3] = unpack4Key( scannedKeys[3], ie );\n" " }\n" " }\n" " }\n" " GROUP_LDS_BARRIER;\n" " {\n" " ldsSortData[newOffset[0]] = sortData[0];\n" " ldsSortData[newOffset[1]] = sortData[1];\n" " ldsSortData[newOffset[2]] = sortData[2];\n" " ldsSortData[newOffset[3]] = sortData[3];\n" " ldsSortVal[newOffset[0]] = sortVal[0];\n" " ldsSortVal[newOffset[1]] = sortVal[1];\n" " ldsSortVal[newOffset[2]] = sortVal[2];\n" " ldsSortVal[newOffset[3]] = sortVal[3];\n" " GROUP_LDS_BARRIER;\n" " u32 dstAddr = 4*lIdx;\n" " sortData[0] = ldsSortData[dstAddr+0];\n" " sortData[1] = ldsSortData[dstAddr+1];\n" " sortData[2] = ldsSortData[dstAddr+2];\n" " sortData[3] = ldsSortData[dstAddr+3];\n" " sortVal[0] = ldsSortVal[dstAddr+0];\n" " sortVal[1] = ldsSortVal[dstAddr+1];\n" " sortVal[2] = ldsSortVal[dstAddr+2];\n" " sortVal[3] = ldsSortVal[dstAddr+3];\n" " GROUP_LDS_BARRIER;\n" " }\n" " }\n" "}\n" "__kernel\n" "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n" "void SortAndScatterSortDataKernel( __global const SortDataCL* restrict gSrc, __global const u32* rHistogram, __global SortDataCL* restrict gDst, int4 cb)\n" "{\n" " __local int ldsSortData[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n" " __local int ldsSortVal[WG_SIZE*ELEMENTS_PER_WORK_ITEM+16];\n" " __local u32 localHistogramToCarry[NUM_BUCKET];\n" " __local u32 localHistogram[NUM_BUCKET*2];\n" " u32 gIdx = GET_GLOBAL_IDX;\n" " u32 lIdx = GET_LOCAL_IDX;\n" " u32 wgIdx = GET_GROUP_IDX;\n" " u32 wgSize = GET_GROUP_SIZE;\n" " const int n = cb.m_n;\n" " const int nWGs = cb.m_nWGs;\n" " const int startBit = cb.m_startBit;\n" " const int nBlocksPerWG = cb.m_nBlocksPerWG;\n" " if( lIdx < (NUM_BUCKET) )\n" " {\n" " localHistogramToCarry[lIdx] = rHistogram[lIdx*nWGs + wgIdx];\n" " }\n" " GROUP_LDS_BARRIER;\n" " \n" " const int blockSize = ELEMENTS_PER_WORK_ITEM*WG_SIZE;\n" " int nBlocks = n/blockSize - nBlocksPerWG*wgIdx;\n" " int addr = blockSize*nBlocksPerWG*wgIdx + ELEMENTS_PER_WORK_ITEM*lIdx;\n" " for(int iblock=0; iblock>startBit) & 0xf;\n" " { // create histogram\n" " u32 setIdx = lIdx/16;\n" " if( lIdx < NUM_BUCKET )\n" " {\n" " localHistogram[lIdx] = 0;\n" " }\n" " ldsSortData[lIdx] = 0;\n" " GROUP_LDS_BARRIER;\n" " for(int i=0; i0)\n" " return;\n" " \n" " for (int c=0;c>startBit) & 0xf;//0xf = NUM_TABLES-1\n" " gDst[rHistogram[tableIdx*nWGs+wgIdx] + counter[tableIdx]] = gSrc[i];\n" " counter[tableIdx] ++;\n" " }\n" " }\n" " }\n" " }\n" " \n" "}\n" "__kernel\n" "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n" "void SortAndScatterKernelSerial( __global const u32* restrict gSrc, __global const u32* rHistogram, __global u32* restrict gDst, int4 cb )\n" "{\n" " \n" " u32 gIdx = GET_GLOBAL_IDX;\n" " u32 realLocalIdx = GET_LOCAL_IDX;\n" " u32 wgIdx = GET_GROUP_IDX;\n" " u32 wgSize = GET_GROUP_SIZE;\n" " const int startBit = cb.m_startBit;\n" " const int n = cb.m_n;\n" " const int nWGs = cb.m_nWGs;\n" " const int nBlocksPerWG = cb.m_nBlocksPerWG;\n" " int counter[NUM_BUCKET];\n" " \n" " if (realLocalIdx>0)\n" " return;\n" " \n" " for (int c=0;c>startBit) & 0xf;//0xf = NUM_TABLES-1\n" " gDst[rHistogram[tableIdx*nWGs+wgIdx] + counter[tableIdx]] = gSrc[i];\n" " counter[tableIdx] ++;\n" " }\n" " }\n" " }\n" " }\n" " \n" "}\n" ;