1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*
Copyright (c) 2012 Advanced Micro Devices, Inc.
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.
*/
#include <stdio.h>
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3FillCL.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3BoundSearchCL.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3RadixSort32CL.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3PrefixScanCL.h"
#include "Bullet3Common/b3CommandLineArgs.h"
#include "Bullet3Common/b3MinMax.h"
int g_nPassed = 0;
int g_nFailed = 0;
bool g_testFailed = 0;
#define TEST_INIT g_testFailed = 0;
#define TEST_ASSERT(x) \
if (!(x)) \
{ \
g_testFailed = 1; \
}
#define TEST_REPORT(testName) \
printf("[%s] %s\n", (g_testFailed) ? "X" : "O", testName); \
if (g_testFailed) \
g_nFailed++; \
else \
g_nPassed++;
#define NEXTMULTIPLEOF(num, alignment) (((num) / (alignment) + (((num) % (alignment) == 0) ? 0 : 1)) * (alignment))
cl_context g_context = 0;
cl_device_id g_device = 0;
cl_command_queue g_queue = 0;
const char* g_deviceName = 0;
void initCL(int preferredDeviceIndex, int preferredPlatformIndex)
{
//void* glCtx=0;
//void* glDC = 0;
int ciErrNum = 0;
//bound search and radix sort only work on GPU right now (assume 32 or 64 width workgroup without barriers)
cl_device_type deviceType = CL_DEVICE_TYPE_ALL;
g_context = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, 0, 0, preferredDeviceIndex, preferredPlatformIndex);
oclCHECKERROR(ciErrNum, CL_SUCCESS);
int numDev = b3OpenCLUtils::getNumDevices(g_context);
if (numDev > 0)
{
b3OpenCLDeviceInfo info;
g_device = b3OpenCLUtils::getDevice(g_context, 0);
g_queue = clCreateCommandQueue(g_context, g_device, 0, &ciErrNum);
oclCHECKERROR(ciErrNum, CL_SUCCESS);
b3OpenCLUtils::printDeviceInfo(g_device);
b3OpenCLUtils::getDeviceInfo(g_device, &info);
g_deviceName = info.m_deviceName;
}
}
void exitCL()
{
clReleaseCommandQueue(g_queue);
clReleaseContext(g_context);
}
inline void fillIntTest()
{
TEST_INIT;
b3FillCL* fillCL = new b3FillCL(g_context, g_device, g_queue);
int maxSize = 1024 * 256;
b3OpenCLArray<int> intBuffer(g_context, g_queue, maxSize);
intBuffer.resize(maxSize);
#define NUM_TESTS 7
int dx = maxSize / NUM_TESTS;
for (int iter = 0; iter < NUM_TESTS; iter++)
{
int size = b3Min(11 + dx * iter, maxSize);
int value = 2;
int offset = 0;
fillCL->execute(intBuffer, value, size, offset);
b3AlignedObjectArray<int> hostBuf2;
hostBuf2.resize(size);
fillCL->executeHost(hostBuf2, value, size, offset);
b3AlignedObjectArray<int> hostBuf;
intBuffer.copyToHost(hostBuf);
for (int i = 0; i < size; i++)
{
TEST_ASSERT(hostBuf[i] == hostBuf2[i]);
TEST_ASSERT(hostBuf[i] == hostBuf2[i]);
}
}
delete fillCL;
TEST_REPORT("fillIntTest");
}
__inline void seedRandom(int seed)
{
srand(seed);
}
template <typename T>
__inline T getRandom(const T& minV, const T& maxV)
{
float r = (rand() % 10000) / 10000.f;
T range = maxV - minV;
return (T)(minV + r * range);
}
struct b3SortDataCompare
{
inline bool operator()(const b3SortData& first, const b3SortData& second) const
{
return (first.m_key < second.m_key) || (first.m_key == second.m_key && first.m_value < second.m_value);
}
};
void boundSearchTest()
{
TEST_INIT;
int maxSize = 1024 * 256;
int bucketSize = 256;
b3OpenCLArray<b3SortData> srcCL(g_context, g_queue, maxSize);
b3OpenCLArray<unsigned int> upperCL(g_context, g_queue, maxSize);
b3OpenCLArray<unsigned int> lowerCL(g_context, g_queue, maxSize);
b3AlignedObjectArray<b3SortData> srcHost;
b3AlignedObjectArray<unsigned int> upperHost;
b3AlignedObjectArray<unsigned int> lowerHost;
b3AlignedObjectArray<unsigned int> upperHostCompare;
b3AlignedObjectArray<unsigned int> lowerHostCompare;
b3BoundSearchCL* search = new b3BoundSearchCL(g_context, g_device, g_queue, maxSize);
int dx = maxSize / NUM_TESTS;
for (int iter = 0; iter < NUM_TESTS; iter++)
{
int size = b3Min(128 + dx * iter, maxSize);
upperHost.resize(bucketSize);
lowerHost.resize(bucketSize);
upperHostCompare.resize(bucketSize);
lowerHostCompare.resize(bucketSize);
srcHost.resize(size);
for (int i = 0; i < size; i++)
{
b3SortData v;
// v.m_key = i<2? 0 : 5;
v.m_key = getRandom(0, bucketSize);
v.m_value = i;
srcHost.at(i) = v;
}
srcHost.quickSort(b3SortDataCompare());
srcCL.copyFromHost(srcHost);
{
for (int i = 0; i < bucketSize; i++)
{
lowerHost[i] = -1;
lowerHostCompare[i] = -1;
upperHost[i] = -1;
upperHostCompare[i] = -1;
}
upperCL.copyFromHost(upperHost);
lowerCL.copyFromHost(lowerHost);
}
search->execute(srcCL, size, upperCL, bucketSize, b3BoundSearchCL::BOUND_UPPER);
search->execute(srcCL, size, lowerCL, bucketSize, b3BoundSearchCL::BOUND_LOWER);
search->executeHost(srcHost, size, upperHostCompare, bucketSize, b3BoundSearchCL::BOUND_UPPER);
search->executeHost(srcHost, size, lowerHostCompare, bucketSize, b3BoundSearchCL::BOUND_LOWER);
lowerCL.copyToHost(lowerHost);
upperCL.copyToHost(upperHost);
for (int i = 0; i < bucketSize; i++)
{
TEST_ASSERT(upperHostCompare[i] == upperHost[i]);
TEST_ASSERT(lowerHostCompare[i] == lowerHost[i]);
}
/*
for(int i=1; i<bucketSize; i++)
{
int lhi_1 = lowerHost[i-1];
int lhi = lowerHost[i];
for(int j=lhi_1; j<lhi; j++)
//for(int j=lowerHost[i-1]; j<lowerHost[i]; j++)
{
TEST_ASSERT( srcHost[j].m_key < i );
}
}
for(int i=0; i<bucketSize; i++)
{
int jMin = (i==0)?0:upperHost[i-1];
for(int j=jMin; j<upperHost[i]; j++)
{
TEST_ASSERT( srcHost[j].m_key <= i );
}
}
*/
for (int i = 0; i < bucketSize; i++)
{
int lhi = lowerHost[i];
int uhi = upperHost[i];
for (int j = lhi; j < uhi; j++)
{
if (srcHost[j].m_key != i)
{
printf("error %d != %d\n", srcHost[j].m_key, i);
}
TEST_ASSERT(srcHost[j].m_key == i);
}
}
}
delete search;
TEST_REPORT("boundSearchTest");
}
void prefixScanTest()
{
TEST_INIT;
int maxSize = 1024 * 256;
b3AlignedObjectArray<unsigned int> buf0Host;
b3AlignedObjectArray<unsigned int> buf1Host;
b3OpenCLArray<unsigned int> buf2CL(g_context, g_queue, maxSize);
b3OpenCLArray<unsigned int> buf3CL(g_context, g_queue, maxSize);
b3PrefixScanCL* scan = new b3PrefixScanCL(g_context, g_device, g_queue, maxSize);
int dx = maxSize / NUM_TESTS;
for (int iter = 0; iter < NUM_TESTS; iter++)
{
int size = b3Min(128 + dx * iter, maxSize);
buf0Host.resize(size);
buf1Host.resize(size);
for (int i = 0; i < size; i++)
buf0Host[i] = 1;
buf2CL.copyFromHost(buf0Host);
unsigned int sumHost, sumGPU;
scan->executeHost(buf0Host, buf1Host, size, &sumHost);
scan->execute(buf2CL, buf3CL, size, &sumGPU);
buf3CL.copyToHost(buf0Host);
TEST_ASSERT(sumHost == sumGPU);
for (int i = 0; i < size; i++)
TEST_ASSERT(buf1Host[i] == buf0Host[i]);
}
delete scan;
TEST_REPORT("scanTest");
}
bool radixSortTest()
{
TEST_INIT;
int maxSize = 1024 * 256;
b3AlignedObjectArray<b3SortData> buf0Host;
buf0Host.resize(maxSize);
b3AlignedObjectArray<b3SortData> buf1Host;
buf1Host.resize(maxSize);
b3OpenCLArray<b3SortData> buf2CL(g_context, g_queue, maxSize);
b3RadixSort32CL* sort = new b3RadixSort32CL(g_context, g_device, g_queue, maxSize);
int dx = maxSize / NUM_TESTS;
for (int iter = 0; iter < NUM_TESTS; iter++)
{
int size = b3Min(128 + dx * iter, maxSize - 512);
size = NEXTMULTIPLEOF(size, 512); //not necessary
buf0Host.resize(size);
for (int i = 0; i < size; i++)
{
b3SortData v;
v.m_key = getRandom(0, 0xff);
v.m_value = i;
buf0Host[i] = v;
}
buf2CL.copyFromHost(buf0Host);
sort->executeHost(buf0Host);
sort->execute(buf2CL);
buf2CL.copyToHost(buf1Host);
for (int i = 0; i < size; i++)
{
TEST_ASSERT(buf0Host[i].m_value == buf1Host[i].m_value && buf0Host[i].m_key == buf1Host[i].m_key);
}
}
delete sort;
TEST_REPORT("radixSort");
return g_testFailed;
}
int main(int argc, char** argv)
{
int preferredDeviceIndex = -1;
int preferredPlatformIndex = -1;
b3CommandLineArgs args(argc, argv);
args.GetCmdLineArgument("deviceId", preferredDeviceIndex);
args.GetCmdLineArgument("platformId", preferredPlatformIndex);
initCL(preferredDeviceIndex, preferredPlatformIndex);
fillIntTest();
boundSearchTest();
prefixScanTest();
radixSortTest();
exitCL();
printf("%d tests passed, %d tests failed\n", g_nPassed, g_nFailed);
printf("End, press <enter>\n");
getchar();
}
|