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
15 //Host-code rewritten by Erwin Coumans
17 #define BOUNDSEARCH_PATH "src/Bullet3OpenCL/ParallelPrimitives/kernels/BoundSearchKernels.cl"
18 #define KERNEL0 "SearchSortDataLowerKernel"
19 #define KERNEL1 "SearchSortDataUpperKernel"
20 #define KERNEL2 "SubtractKernel"
22 #include "b3BoundSearchCL.h"
23 #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
24 #include "b3LauncherCL.h"
25 #include "kernels/BoundSearchKernelsCL.h"
27 b3BoundSearchCL::b3BoundSearchCL(cl_context ctx, cl_device_id device, cl_command_queue queue, int maxSize)
32 const char* additionalMacros = "";
33 //const char* srcFileNameForCaching="";
36 const char* kernelSource = boundSearchKernelsCL;
38 cl_program boundSearchProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, kernelSource, &pErrNum, additionalMacros, BOUNDSEARCH_PATH);
39 b3Assert(boundSearchProg);
41 m_lowerSortDataKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, kernelSource, "SearchSortDataLowerKernel", &pErrNum, boundSearchProg, additionalMacros);
42 b3Assert(m_lowerSortDataKernel);
44 m_upperSortDataKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, kernelSource, "SearchSortDataUpperKernel", &pErrNum, boundSearchProg, additionalMacros);
45 b3Assert(m_upperSortDataKernel);
51 m_subtractKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, kernelSource, "SubtractKernel", &pErrNum, boundSearchProg, additionalMacros);
52 b3Assert(m_subtractKernel);
55 //m_constBuffer = new b3OpenCLArray<b3Int4>( device, 1, BufferBase::BUFFER_CONST );
57 m_lower = (maxSize == 0) ? 0 : new b3OpenCLArray<unsigned int>(ctx, queue, maxSize);
58 m_upper = (maxSize == 0) ? 0 : new b3OpenCLArray<unsigned int>(ctx, queue, maxSize);
60 m_filler = new b3FillCL(ctx, device, queue);
63 b3BoundSearchCL::~b3BoundSearchCL()
69 clReleaseKernel(m_lowerSortDataKernel);
70 clReleaseKernel(m_upperSortDataKernel);
71 clReleaseKernel(m_subtractKernel);
74 void b3BoundSearchCL::execute(b3OpenCLArray<b3SortData>& src, int nSrc, b3OpenCLArray<unsigned int>& dst, int nDst, Option option)
80 if (option == BOUND_LOWER)
82 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(src.getBufferCL(), true), b3BufferInfoCL(dst.getBufferCL())};
84 b3LauncherCL launcher(m_queue, m_lowerSortDataKernel, "m_lowerSortDataKernel");
85 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
86 launcher.setConst(nSrc);
87 launcher.setConst(nDst);
89 launcher.launch1D(nSrc, 64);
91 else if (option == BOUND_UPPER)
93 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(src.getBufferCL(), true), b3BufferInfoCL(dst.getBufferCL())};
95 b3LauncherCL launcher(m_queue, m_upperSortDataKernel, "m_upperSortDataKernel");
96 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
97 launcher.setConst(nSrc);
98 launcher.setConst(nDst);
100 launcher.launch1D(nSrc, 64);
102 else if (option == COUNT)
106 b3Assert(m_lower->capacity() <= (int)nDst);
107 b3Assert(m_upper->capacity() <= (int)nDst);
110 m_filler->execute(*m_lower, zero, nDst);
111 m_filler->execute(*m_upper, zero, nDst);
113 execute(src, nSrc, *m_lower, nDst, BOUND_LOWER);
114 execute(src, nSrc, *m_upper, nDst, BOUND_UPPER);
117 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(m_upper->getBufferCL(), true), b3BufferInfoCL(m_lower->getBufferCL(), true), b3BufferInfoCL(dst.getBufferCL())};
119 b3LauncherCL launcher(m_queue, m_subtractKernel, "m_subtractKernel");
120 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
121 launcher.setConst(nSrc);
122 launcher.setConst(nDst);
124 launcher.launch1D(nDst, 64);
133 void b3BoundSearchCL::executeHost(b3AlignedObjectArray<b3SortData>& src, int nSrc,
134 b3AlignedObjectArray<unsigned int>& dst, int nDst, Option option)
136 for (int i = 0; i < nSrc - 1; i++)
137 b3Assert(src[i].m_key <= src[i + 1].m_key);
139 b3SortData minData, zeroData, maxData;
141 minData.m_value = -1;
143 zeroData.m_value = 0;
144 maxData.m_key = nDst;
145 maxData.m_value = nDst;
147 if (option == BOUND_LOWER)
149 for (int i = 0; i < nSrc; i++)
151 b3SortData& iData = (i == 0) ? minData : src[i - 1];
152 b3SortData& jData = (i == nSrc) ? maxData : src[i];
154 if (iData.m_key != jData.m_key)
163 else if (option == BOUND_UPPER)
165 for (int i = 1; i < nSrc + 1; i++)
167 b3SortData& iData = src[i - 1];
168 b3SortData& jData = (i == nSrc) ? maxData : src[i];
170 if (iData.m_key != jData.m_key)
179 else if (option == COUNT)
181 b3AlignedObjectArray<unsigned int> lower;
183 b3AlignedObjectArray<unsigned int> upper;
186 for (int i = 0; i < nDst; i++)
188 lower[i] = upper[i] = 0;
191 executeHost(src, nSrc, lower, nDst, BOUND_LOWER);
192 executeHost(src, nSrc, upper, nDst, BOUND_UPPER);
194 for (int i = 0; i < nDst; i++)
196 dst[i] = upper[i] - lower[i];