b3PrefixScanFloat4CL.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "b3PrefixScanFloat4CL.h"
  2. #include "b3FillCL.h"
  3. #define B3_PREFIXSCAN_FLOAT4_PROG_PATH "src/Bullet3OpenCL/ParallelPrimitives/kernels/PrefixScanFloat4Kernels.cl"
  4. #include "b3LauncherCL.h"
  5. #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
  6. #include "kernels/PrefixScanKernelsFloat4CL.h"
  7. b3PrefixScanFloat4CL::b3PrefixScanFloat4CL(cl_context ctx, cl_device_id device, cl_command_queue queue, int size)
  8. :m_commandQueue(queue)
  9. {
  10. const char* scanKernelSource = prefixScanKernelsFloat4CL;
  11. cl_int pErrNum;
  12. char* additionalMacros=0;
  13. m_workBuffer = new b3OpenCLArray<b3Vector3>(ctx,queue,size);
  14. cl_program scanProg = b3OpenCLUtils::compileCLProgramFromString( ctx, device, scanKernelSource, &pErrNum,additionalMacros, B3_PREFIXSCAN_FLOAT4_PROG_PATH);
  15. b3Assert(scanProg);
  16. m_localScanKernel = b3OpenCLUtils::compileCLKernelFromString( ctx, device, scanKernelSource, "LocalScanKernel", &pErrNum, scanProg,additionalMacros );
  17. b3Assert(m_localScanKernel );
  18. m_blockSumKernel = b3OpenCLUtils::compileCLKernelFromString( ctx, device, scanKernelSource, "TopLevelScanKernel", &pErrNum, scanProg,additionalMacros );
  19. b3Assert(m_blockSumKernel );
  20. m_propagationKernel = b3OpenCLUtils::compileCLKernelFromString( ctx, device, scanKernelSource, "AddOffsetKernel", &pErrNum, scanProg,additionalMacros );
  21. b3Assert(m_propagationKernel );
  22. }
  23. b3PrefixScanFloat4CL::~b3PrefixScanFloat4CL()
  24. {
  25. delete m_workBuffer;
  26. clReleaseKernel(m_localScanKernel);
  27. clReleaseKernel(m_blockSumKernel);
  28. clReleaseKernel(m_propagationKernel);
  29. }
  30. template<class T>
  31. T b3NextPowerOf2(T n)
  32. {
  33. n -= 1;
  34. for(int i=0; i<sizeof(T)*8; i++)
  35. n = n | (n>>i);
  36. return n+1;
  37. }
  38. void b3PrefixScanFloat4CL::execute(b3OpenCLArray<b3Vector3>& src, b3OpenCLArray<b3Vector3>& dst, int n, b3Vector3* sum)
  39. {
  40. // b3Assert( data->m_option == EXCLUSIVE );
  41. const unsigned int numBlocks = (const unsigned int)( (n+BLOCK_SIZE*2-1)/(BLOCK_SIZE*2) );
  42. dst.resize(src.size());
  43. m_workBuffer->resize(src.size());
  44. b3Int4 constBuffer;
  45. constBuffer.x = n;
  46. constBuffer.y = numBlocks;
  47. constBuffer.z = (int)b3NextPowerOf2( numBlocks );
  48. b3OpenCLArray<b3Vector3>* srcNative = &src;
  49. b3OpenCLArray<b3Vector3>* dstNative = &dst;
  50. {
  51. b3BufferInfoCL bInfo[] = { b3BufferInfoCL( dstNative->getBufferCL() ), b3BufferInfoCL( srcNative->getBufferCL() ), b3BufferInfoCL( m_workBuffer->getBufferCL() ) };
  52. b3LauncherCL launcher( m_commandQueue, m_localScanKernel ,"m_localScanKernel");
  53. launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(b3BufferInfoCL) );
  54. launcher.setConst( constBuffer );
  55. launcher.launch1D( numBlocks*BLOCK_SIZE, BLOCK_SIZE );
  56. }
  57. {
  58. b3BufferInfoCL bInfo[] = { b3BufferInfoCL( m_workBuffer->getBufferCL() ) };
  59. b3LauncherCL launcher( m_commandQueue, m_blockSumKernel ,"m_blockSumKernel");
  60. launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(b3BufferInfoCL) );
  61. launcher.setConst( constBuffer );
  62. launcher.launch1D( BLOCK_SIZE, BLOCK_SIZE );
  63. }
  64. if( numBlocks > 1 )
  65. {
  66. b3BufferInfoCL bInfo[] = { b3BufferInfoCL( dstNative->getBufferCL() ), b3BufferInfoCL( m_workBuffer->getBufferCL() ) };
  67. b3LauncherCL launcher( m_commandQueue, m_propagationKernel ,"m_propagationKernel");
  68. launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(b3BufferInfoCL) );
  69. launcher.setConst( constBuffer );
  70. launcher.launch1D( (numBlocks-1)*BLOCK_SIZE, BLOCK_SIZE );
  71. }
  72. if( sum )
  73. {
  74. clFinish(m_commandQueue);
  75. dstNative->copyToHostPointer(sum,1,n-1,true);
  76. }
  77. }
  78. void b3PrefixScanFloat4CL::executeHost(b3AlignedObjectArray<b3Vector3>& src, b3AlignedObjectArray<b3Vector3>& dst, int n, b3Vector3* sum)
  79. {
  80. b3Vector3 s=b3MakeVector3(0,0,0);
  81. //if( data->m_option == EXCLUSIVE )
  82. {
  83. for(int i=0; i<n; i++)
  84. {
  85. dst[i] = s;
  86. s += src[i];
  87. }
  88. }
  89. /*else
  90. {
  91. for(int i=0; i<n; i++)
  92. {
  93. s += hSrc[i];
  94. hDst[i] = s;
  95. }
  96. }
  97. */
  98. if( sum )
  99. {
  100. *sum = dst[n-1];
  101. }
  102. }