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