bucketsort.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #define BUCKET_WARP_LOG_SIZE 5
  2. #define BUCKET_WARP_N 1
  3. #ifdef BUCKET_WG_SIZE_1
  4. #define BUCKET_THREAD_N BUCKET_WG_SIZE_1
  5. #else
  6. #define BUCKET_THREAD_N (BUCKET_WARP_N << BUCKET_WARP_LOG_SIZE)
  7. #endif
  8. #define BUCKET_BLOCK_MEMORY (DIVISIONS * BUCKET_WARP_N)
  9. #define BUCKET_BAND 128
  10. #define SIZE (1 << 22)
  11. #define DATA_SIZE (1024)
  12. #define MAX_SOURCE_SIZE (0x100000)
  13. #define HISTOGRAM_SIZE (1024 * sizeof(unsigned int))
  14. #include <fcntl.h>
  15. #include <float.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <CL/cl.h>
  24. #include "bucketsort.h"
  25. #include <time.h>
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // Forward declarations
  28. ////////////////////////////////////////////////////////////////////////////////
  29. void calcPivotPoints(float *histogram, int histosize, int listsize,
  30. int divisions, float min, float max, float *pivotPoints,
  31. float histo_width);
  32. ////////////////////////////////////////////////////////////////////////////////
  33. // Globals
  34. ////////////////////////////////////////////////////////////////////////////////
  35. const int histosize = 1024;
  36. unsigned int* h_offsets = NULL;
  37. unsigned int* d_offsets = NULL;
  38. cl_mem d_offsets_buff;
  39. int *d_indice = NULL;
  40. cl_mem d_indice_buff;
  41. cl_mem d_input_buff;
  42. cl_mem d_indice_input_buff;
  43. float *pivotPoints = NULL;
  44. float *historesult = NULL;
  45. float *l_pivotpoints = NULL;
  46. cl_mem l_pivotpoints_buff;
  47. unsigned int *d_prefixoffsets = NULL;
  48. unsigned int *d_prefixoffsets_altered = NULL;
  49. cl_mem d_prefixoffsets_buff;
  50. cl_mem d_prefixoffsets_input_buff;
  51. unsigned int *l_offsets = NULL;
  52. cl_mem l_offsets_buff;
  53. unsigned int *d_Result1024;
  54. cl_device_id device_id; // compute device id
  55. cl_context bucketContext; // compute context
  56. cl_context histoContext;
  57. cl_command_queue bucketCommands; // compute command queue
  58. cl_command_queue histoCommands;
  59. cl_program bucketProgram; // compute program
  60. cl_program histoProgram;
  61. cl_kernel bucketcountKernel; // compute kernel
  62. cl_kernel histoKernel;
  63. cl_kernel bucketprefixKernel;
  64. cl_kernel bucketsortKernel;
  65. cl_mem histoInput;
  66. cl_mem histoOutput;
  67. cl_mem bucketOutput;
  68. cl_int err;
  69. cl_uint num_platforms;
  70. cl_event histoEvent;
  71. cl_event bucketCountEvent;
  72. cl_event bucketPrefixEvent;
  73. cl_event bucketSortEvent;
  74. double sum = 0;
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // Initialize the bucketsort algorithm
  77. ////////////////////////////////////////////////////////////////////////////////
  78. void init_bucketsort(int listsize, int platform_id, int device_id, int use_gpu)
  79. {
  80. cl_uint num = 0;
  81. clGetPlatformIDs(0, NULL, &num);
  82. cl_platform_id platformID[num];
  83. clGetPlatformIDs(num, platformID, NULL);
  84. // Selector for CPU/GPU
  85. cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
  86. clGetDeviceIDs(platformID[platform_id],device_type,0,NULL,&num);
  87. cl_device_id devices[num];
  88. err = clGetDeviceIDs(platformID[platform_id],device_type,num,devices,NULL);
  89. // int gpu = 1;
  90. // err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 2, &device_id, NULL);
  91. if (err != CL_SUCCESS)
  92. {
  93. printf("Error: Failed to create a device group!\n");
  94. exit(1);
  95. }
  96. char name[128];
  97. clGetDeviceInfo(devices[device_id],CL_DEVICE_NAME,128,name,NULL);
  98. bucketContext = clCreateContext(0, 1, &devices[device_id], NULL, NULL, &err);
  99. bucketCommands = clCreateCommandQueue(bucketContext, devices[device_id], CL_QUEUE_PROFILING_ENABLE, &err);
  100. h_offsets = (unsigned int *) malloc(DIVISIONS * sizeof(unsigned int));
  101. for(int i = 0; i < DIVISIONS; i++){
  102. h_offsets[i] = 0;
  103. }
  104. d_offsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(unsigned int),NULL,NULL);
  105. pivotPoints = (float *)malloc(DIVISIONS * sizeof(float));
  106. d_indice_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, listsize * sizeof(int),NULL,NULL);
  107. d_indice_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, listsize * sizeof(int),NULL,NULL);
  108. d_indice = (int *)malloc(listsize * sizeof(int));
  109. historesult = (float *)malloc(histosize * sizeof(float));
  110. l_pivotpoints = (float *)malloc(DIVISIONS*sizeof(float));
  111. l_pivotpoints_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(float), NULL, NULL);
  112. l_offsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(unsigned int), NULL, NULL);
  113. int blocks = ((listsize - 1) / (BUCKET_THREAD_N * BUCKET_BAND)) + 1;
  114. d_prefixoffsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), NULL, NULL);
  115. d_prefixoffsets = (unsigned int *)malloc(blocks*BUCKET_BLOCK_MEMORY*sizeof(int));
  116. d_prefixoffsets_altered = (unsigned int *)malloc(blocks*BUCKET_BLOCK_MEMORY*sizeof(int));
  117. d_prefixoffsets_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), NULL, NULL);
  118. bucketOutput = clCreateBuffer(bucketContext, CL_MEM_READ_WRITE, (listsize + (DIVISIONS*4))*sizeof(float), NULL, NULL);
  119. FILE *fp;
  120. const char fileName[]="./bucketsort_kernels.cl";
  121. size_t source_size;
  122. char *source_str;
  123. fp = fopen(fileName, "r");
  124. if (!fp) {
  125. fprintf(stderr, "Failed to load bucket kernel.\n");
  126. exit(1);
  127. }
  128. source_str = (char *)malloc(MAX_SOURCE_SIZE);
  129. source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
  130. fclose(fp);
  131. bucketProgram = clCreateProgramWithSource(bucketContext, 1, (const char **) &source_str, (const size_t)&source_size, &err);
  132. if (!bucketProgram)
  133. {
  134. printf("Error: Failed to create bucket compute program!\n");
  135. exit(1);
  136. }
  137. err = clBuildProgram(bucketProgram, 0, NULL, NULL, NULL, NULL);
  138. if (err != CL_SUCCESS)
  139. {
  140. size_t len;
  141. char buffer[2048];
  142. printf("Error: Failed to build bucket program executable!\n");
  143. clGetProgramBuildInfo(bucketProgram, devices[device_id], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  144. printf("%s\n", buffer);
  145. exit(1);
  146. }
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. // Uninitialize the bucketsort algorithm
  150. ////////////////////////////////////////////////////////////////////////////////
  151. void finish_bucketsort()
  152. {
  153. clReleaseMemObject(d_offsets_buff);
  154. clReleaseMemObject(d_indice_buff);
  155. clReleaseMemObject(l_pivotpoints_buff);
  156. clReleaseMemObject(l_offsets_buff);
  157. clReleaseMemObject(d_prefixoffsets_buff);
  158. clReleaseMemObject(d_input_buff);
  159. clReleaseMemObject(d_indice_input_buff);
  160. clReleaseMemObject(bucketOutput);
  161. clReleaseProgram(bucketProgram);
  162. clReleaseKernel(bucketcountKernel);
  163. clReleaseKernel(bucketprefixKernel);
  164. clReleaseKernel(bucketsortKernel);
  165. clReleaseCommandQueue(bucketCommands);
  166. clReleaseContext(bucketContext);
  167. free(pivotPoints);
  168. free(h_offsets);
  169. free(historesult);
  170. }
  171. void histogramInit(int listsize, int platform_id, int device_id, int use_gpu) {
  172. cl_uint num = 0;
  173. clGetPlatformIDs(0, NULL, &num);
  174. cl_platform_id platformID[num];
  175. clGetPlatformIDs(num, platformID, NULL);
  176. // Selector for CPU/GPU
  177. cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
  178. clGetDeviceIDs(platformID[platform_id],device_type,0,NULL,&num);
  179. num = 2;
  180. char name[128];
  181. clGetPlatformInfo(platformID[platform_id], CL_PLATFORM_PROFILE,128,name,NULL);
  182. cl_device_id devices[num];
  183. err = clGetDeviceIDs(platformID[1],device_type,num,devices,NULL);
  184. // int gpu = 1;
  185. // err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 2, &device_id, NULL);
  186. if (err != CL_SUCCESS)
  187. {
  188. printf("Error: Failed to create a device group!\n");
  189. exit(1);
  190. }
  191. clGetDeviceInfo(devices[device_id],CL_DEVICE_NAME,128,name,NULL);
  192. printf("%s \n", name);
  193. cl_context_properties contextProperties[] =
  194. {
  195. CL_CONTEXT_PLATFORM,
  196. (cl_context_properties)platformID[num],
  197. 0
  198. };
  199. histoContext = clCreateContext(0, 1, &devices[device_id], NULL, NULL, &err);
  200. histoCommands = clCreateCommandQueue(histoContext, devices[device_id], CL_QUEUE_PROFILING_ENABLE, &err);
  201. histoInput = clCreateBuffer(histoContext, CL_MEM_READ_ONLY, listsize*(sizeof(float)), NULL, NULL);
  202. histoOutput = clCreateBuffer(histoContext, CL_MEM_READ_WRITE, 1024 * sizeof(unsigned int), NULL, NULL);
  203. FILE *fp;
  204. const char fileName[]="./histogram1024.cl";
  205. size_t source_size;
  206. char *source_str;
  207. fp = fopen(fileName, "r");
  208. if (!fp) {
  209. fprintf(stderr, "Failed to load kernel.\n");
  210. exit(1);
  211. }
  212. source_str = (char *)malloc(MAX_SOURCE_SIZE);
  213. source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
  214. fclose(fp);
  215. histoProgram = clCreateProgramWithSource(histoContext, 1, (const char **) &source_str, (const size_t)&source_size, &err);
  216. if (!histoProgram)
  217. {
  218. printf("Error: Failed to create compute program!\n");
  219. exit(1);
  220. }
  221. err = clBuildProgram(histoProgram, 0, NULL, NULL, NULL, NULL);
  222. if (err != CL_SUCCESS)
  223. {
  224. size_t len;
  225. char buffer[2048];
  226. printf("Error: Failed to build program executable!\n");
  227. clGetProgramBuildInfo(histoProgram, devices[device_id], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  228. printf("%s\n", buffer);
  229. exit(1);
  230. }
  231. histoKernel = clCreateKernel(histoProgram, "histogram1024Kernel", &err);
  232. if (!histoKernel || err != CL_SUCCESS)
  233. {
  234. printf("Error: Failed to create compute kernel!\n");
  235. exit(1);
  236. }
  237. }
  238. void histogram1024GPU(unsigned int *h_Result, float *d_Data, float minimum, float maximum,int listsize){
  239. err = clEnqueueWriteBuffer(histoCommands, histoInput, CL_TRUE, 0, listsize*sizeof(float), d_Data, 0, NULL, NULL);
  240. if (err != CL_SUCCESS)
  241. {
  242. printf("Error: Failed to write to source array!\n");
  243. exit(1);
  244. }
  245. err = clEnqueueWriteBuffer(histoCommands, histoOutput, CL_TRUE, 0, DIVISIONS*sizeof(unsigned int), h_Result, 0, NULL, NULL);
  246. if (err != CL_SUCCESS)
  247. {
  248. printf("Error: Failed to write to source array!\n");
  249. exit(1);
  250. }
  251. err = 0;
  252. err = clSetKernelArg(histoKernel, 0, sizeof(cl_mem), &histoOutput);
  253. err = clSetKernelArg(histoKernel, 1, sizeof(cl_mem), &histoInput);
  254. err = clSetKernelArg(histoKernel, 2, sizeof(float), &minimum);
  255. err = clSetKernelArg(histoKernel, 3, sizeof(float), &maximum);
  256. err = clSetKernelArg(histoKernel, 4, sizeof(int), &listsize);
  257. if (err != CL_SUCCESS)
  258. {
  259. printf("Error: Failed to set kernel arguments! %d\n", err);
  260. exit(1);
  261. }
  262. size_t global = 6144;
  263. size_t local;
  264. #ifdef HISTO_WG_SIZE_0
  265. local = HISTO_WG_SIZE_0;
  266. #else
  267. local = 96;
  268. #endif
  269. err = clEnqueueNDRangeKernel(histoCommands, histoKernel, 1, NULL, &global, &local, 0, NULL, &histoEvent);
  270. if (err)
  271. {
  272. printf("Error: Failed to execute histogram kernel!\n");
  273. exit(1);
  274. }
  275. clWaitForEvents(1 , &histoEvent);
  276. clFinish(histoCommands);
  277. err = clEnqueueReadBuffer( histoCommands, histoOutput, CL_TRUE, 0, 1024 * sizeof(unsigned int), h_Result, 0, NULL, NULL );
  278. if (err != CL_SUCCESS)
  279. {
  280. printf("Error: Failed to read histo output array! %d\n", err);
  281. exit(1);
  282. }
  283. clFinish(histoCommands);
  284. cl_ulong time_start, time_end;
  285. double total_time;
  286. clGetEventProfilingInfo(histoEvent, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
  287. clGetEventProfilingInfo(histoEvent, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
  288. total_time = time_end - time_start;
  289. sum+= total_time/1000000.0;
  290. printf("Histogram Kernel Time: %0.3f \n", total_time/1000000);
  291. }
  292. void finish_histogram() {
  293. clReleaseProgram(histoProgram);
  294. clReleaseKernel(histoKernel);
  295. clReleaseCommandQueue(histoCommands);
  296. clReleaseContext(histoContext);
  297. clReleaseMemObject((histoInput));
  298. clReleaseMemObject((histoOutput));
  299. }
  300. ////////////////////////////////////////////////////////////////////////////////
  301. // Given the input array of floats and the min and max of the distribution,
  302. // sort the elements into float4 aligned buckets of roughly equal size
  303. ////////////////////////////////////////////////////////////////////////////////
  304. void bucketSort(float *d_input, float *d_output, int listsize,
  305. int *sizes, int *nullElements, float minimum, float maximum,
  306. unsigned int *origOffsets, int platform_id, int device_id, int use_gpu)
  307. {
  308. // ////////////////////////////////////////////////////////////////////////////
  309. // // First pass - Create 1024 bin histogram
  310. // ////////////////////////////////////////////////////////////////////////////
  311. histogramInit(listsize, platform_id, device_id, use_gpu);
  312. histogram1024GPU(h_offsets, d_input, minimum, maximum, listsize);
  313. finish_histogram();
  314. for(int i=0; i<histosize; i++) historesult[i] = (float)h_offsets[i];
  315. // ///////////////////////////////////////////////////////////////////////////
  316. // // Calculate pivot points (CPU algorithm)
  317. // ///////////////////////////////////////////////////////////////////////////
  318. calcPivotPoints(historesult, histosize, listsize, DIVISIONS,
  319. minimum, maximum, pivotPoints,
  320. (maximum - minimum)/(float)histosize);
  321. //
  322. // ///////////////////////////////////////////////////////////////////////////
  323. // // Count the bucket sizes in new divisions
  324. // ///////////////////////////////////////////////////////////////////////////
  325. bucketcountKernel = clCreateKernel(bucketProgram, "bucketcount", &err);
  326. if (!bucketcountKernel || err != CL_SUCCESS)
  327. {
  328. printf("Error: Failed to create bucketsort compute kernel!\n");
  329. exit(1);
  330. }
  331. err = clEnqueueWriteBuffer(bucketCommands, l_pivotpoints_buff, CL_TRUE, 0, DIVISIONS*sizeof(float), pivotPoints, 0, NULL, NULL);
  332. if (err != CL_SUCCESS)
  333. {
  334. printf("Error: Failed to write to l_pivotpoints source array!\n");
  335. exit(1);
  336. }
  337. d_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, (listsize + (DIVISIONS*4))*sizeof(float),NULL,NULL);
  338. err = clEnqueueWriteBuffer(bucketCommands, d_input_buff, CL_TRUE, 0, (listsize + (DIVISIONS*4))*sizeof(float), d_input, 0, NULL, NULL);
  339. if (err != CL_SUCCESS)
  340. {
  341. printf("Error: Failed to write to d_input_buff source array!\n");
  342. exit(1);
  343. }
  344. err = 0;
  345. err = clSetKernelArg(bucketcountKernel, 0, sizeof(cl_mem), &d_input_buff);
  346. err = clSetKernelArg(bucketcountKernel, 1, sizeof(cl_mem), &d_indice_buff);
  347. err = clSetKernelArg(bucketcountKernel, 2, sizeof(cl_mem), &d_prefixoffsets_buff);
  348. err = clSetKernelArg(bucketcountKernel, 3, sizeof(cl_int), &listsize);
  349. err = clSetKernelArg(bucketcountKernel, 4, sizeof(cl_mem), &l_pivotpoints_buff);
  350. if (err != CL_SUCCESS)
  351. {
  352. printf("Error: Failed to set kernel arguments! %d\n", err);
  353. exit(1);
  354. }
  355. int blocks =((listsize -1) / (BUCKET_THREAD_N*BUCKET_BAND)) + 1;
  356. size_t global[] = {blocks*BUCKET_THREAD_N,1,1};
  357. size_t local[] = {BUCKET_THREAD_N,1,1};
  358. err = clEnqueueNDRangeKernel(bucketCommands, bucketcountKernel, 3, NULL, global, local, 0, NULL, &bucketCountEvent);
  359. if (err)
  360. {
  361. printf("Error: Failed to execute bucket count kernel!\n");
  362. exit(1);
  363. }
  364. clWaitForEvents(1 , &bucketCountEvent);
  365. clFinish(bucketCommands);
  366. err = clEnqueueReadBuffer( bucketCommands, d_prefixoffsets_buff, CL_TRUE, 0, blocks * BUCKET_BLOCK_MEMORY * sizeof(unsigned int), d_prefixoffsets, 0, NULL, NULL );
  367. if (err != CL_SUCCESS)
  368. {
  369. printf("Error: Failed to read prefix output array! %d\n", err);
  370. exit(1);
  371. }
  372. err = clEnqueueReadBuffer( bucketCommands, d_indice_buff, CL_TRUE, 0, listsize * sizeof(int), d_indice, 0, NULL, NULL );
  373. if (err != CL_SUCCESS)
  374. {
  375. printf("Error: Failed to read indice output array! %d\n", err);
  376. exit(1);
  377. }
  378. clFinish(bucketCommands);
  379. cl_ulong time_start, time_end;
  380. double total_time;
  381. clGetEventProfilingInfo(bucketCountEvent, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
  382. clGetEventProfilingInfo(bucketCountEvent, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
  383. total_time = time_end - time_start;
  384. sum+= total_time/1000000;
  385. printf("Bucket Count Kernel Time: %0.3f \n", total_time/1000000);
  386. //
  387. // ///////////////////////////////////////////////////////////////////////////
  388. // // Prefix scan offsets and align each division to float4 (required by
  389. // // mergesort)
  390. // ///////////////////////////////////////////////////////////////////////////
  391. #ifdef BUCKET_WG_SIZE_0
  392. size_t localpre[] = {BUCKET_WG_SIZE_0,1,1};
  393. #else
  394. size_t localpre[] = {128,1,1};
  395. #endif
  396. size_t globalpre[] = {(DIVISIONS),1,1};
  397. bucketprefixKernel = clCreateKernel(bucketProgram, "bucketprefixoffset", &err);
  398. if (!bucketprefixKernel || err != CL_SUCCESS)
  399. {
  400. printf("Error: Failed to create bucket prefix compute kernel!\n");
  401. exit(1);
  402. }
  403. err = clEnqueueWriteBuffer(bucketCommands, d_prefixoffsets_buff, CL_TRUE, 0, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), d_prefixoffsets, 0, NULL, NULL);
  404. if (err != CL_SUCCESS)
  405. {
  406. printf("Error: Failed to write to prefix offsets source array!\n");
  407. exit(1);
  408. }
  409. err = 0;
  410. err = clSetKernelArg(bucketprefixKernel, 0, sizeof(cl_mem), &d_prefixoffsets_buff);
  411. err = clSetKernelArg(bucketprefixKernel, 1, sizeof(cl_mem), &d_offsets_buff);
  412. err = clSetKernelArg(bucketprefixKernel, 2, sizeof(cl_int), &blocks);
  413. if (err != CL_SUCCESS)
  414. {
  415. printf("Error: Failed to set kernel arguments! %d\n", err);
  416. exit(1);
  417. }
  418. err = clEnqueueNDRangeKernel(bucketCommands, bucketprefixKernel, 3, NULL, globalpre, localpre, 0, NULL, &bucketPrefixEvent);
  419. if (err)
  420. {
  421. printf("%d Error: Failed to execute bucket prefix kernel!\n", err);
  422. exit(1);
  423. }
  424. clWaitForEvents(1 , &bucketPrefixEvent);
  425. clFinish(bucketCommands);
  426. err = clEnqueueReadBuffer( bucketCommands, d_offsets_buff, CL_TRUE, 0, DIVISIONS * sizeof(unsigned int), h_offsets, 0, NULL, NULL );
  427. if (err != CL_SUCCESS)
  428. {
  429. printf("Error: Failed to read d_offsets output array! %d\n", err);
  430. exit(1);
  431. }
  432. err = clEnqueueReadBuffer( bucketCommands, d_prefixoffsets_buff, CL_TRUE, 0, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), d_prefixoffsets_altered, 0, NULL, NULL );
  433. if (err != CL_SUCCESS)
  434. {
  435. printf("Error: Failed to read d_offsets output array! %d\n", err);
  436. exit(1);
  437. }
  438. clFinish(bucketCommands);
  439. clGetEventProfilingInfo(bucketPrefixEvent, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
  440. clGetEventProfilingInfo(bucketPrefixEvent, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
  441. total_time = time_end - time_start;
  442. sum+= total_time/1000000;
  443. printf("Bucket Prefix Kernel Time: %0.3f \n", total_time/1000000);
  444. // // copy the sizes from device to host
  445. origOffsets[0] = 0;
  446. for(int i=0; i<DIVISIONS; i++){
  447. origOffsets[i+1] = h_offsets[i] + origOffsets[i];
  448. if((h_offsets[i] % 4) != 0){
  449. nullElements[i] = (h_offsets[i] & ~3) + 4 - h_offsets[i];
  450. }
  451. else nullElements[i] = 0;
  452. }
  453. for(int i=0; i<DIVISIONS; i++) sizes[i] = (h_offsets[i] + nullElements[i])/4;
  454. for(int i=0; i<DIVISIONS; i++) {
  455. if((h_offsets[i] % 4) != 0) h_offsets[i] = (h_offsets[i] & ~3) + 4;
  456. }
  457. for(int i=1; i<DIVISIONS; i++) h_offsets[i] = h_offsets[i-1] + h_offsets[i];
  458. for(int i=DIVISIONS - 1; i>0; i--) h_offsets[i] = h_offsets[i-1];
  459. h_offsets[0] = 0;
  460. // ///////////////////////////////////////////////////////////////////////////
  461. // // Finally, sort the lot
  462. // ///////////////////////////////////////////////////////////////////////////
  463. bucketsortKernel = clCreateKernel(bucketProgram, "bucketsort", &err);
  464. if (!bucketsortKernel|| err != CL_SUCCESS)
  465. {
  466. printf("Error: Failed to create bucketsort compute kernel!\n");
  467. exit(1);
  468. }
  469. err = clEnqueueWriteBuffer(bucketCommands, l_offsets_buff, CL_TRUE, 0, DIVISIONS * sizeof(unsigned int), h_offsets, 0, NULL, NULL);
  470. if (err != CL_SUCCESS)
  471. {
  472. printf("Error: Failed to write to l_offsets source array!\n");
  473. exit(1);
  474. }
  475. err = clEnqueueWriteBuffer(bucketCommands, d_input_buff, CL_TRUE, 0, (listsize + (DIVISIONS*4))*sizeof(float), d_input, 0, NULL, NULL);
  476. if (err != CL_SUCCESS)
  477. {
  478. printf("Error: Failed to write to d_input_buff source array!\n");
  479. exit(1);
  480. }
  481. err = clEnqueueWriteBuffer(bucketCommands, d_indice_input_buff, CL_TRUE, 0, listsize*sizeof(int), d_indice, 0, NULL, NULL);
  482. if (err != CL_SUCCESS)
  483. {
  484. printf("Error: Failed to write to d_input_buff source array!\n");
  485. exit(1);
  486. }
  487. err = clEnqueueWriteBuffer(bucketCommands, d_prefixoffsets_input_buff, CL_TRUE, 0, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), d_prefixoffsets_altered, 0, NULL, NULL);
  488. if (err != CL_SUCCESS)
  489. {
  490. printf("Error: Failed to write to prefix offsets source array!\n");
  491. exit(1);
  492. }
  493. err = clEnqueueWriteBuffer(bucketCommands, bucketOutput, CL_TRUE, 0, (listsize + (DIVISIONS*4))*sizeof(float), d_output, 0, NULL, NULL);
  494. if (err != CL_SUCCESS)
  495. {
  496. printf("Error: Failed to write to source array!\n");
  497. exit(1);
  498. }
  499. size_t localfinal[] = {BUCKET_THREAD_N,1,1};
  500. blocks = ((listsize - 1) / (BUCKET_THREAD_N * BUCKET_BAND)) + 1;
  501. size_t globalfinal[] = {blocks*BUCKET_THREAD_N,1,1};
  502. err = 0;
  503. err = clSetKernelArg(bucketsortKernel, 0, sizeof(cl_mem), &d_input_buff);
  504. err = clSetKernelArg(bucketsortKernel, 1, sizeof(cl_mem), &d_indice_input_buff);
  505. err = clSetKernelArg(bucketsortKernel, 2, sizeof(cl_mem), &bucketOutput);
  506. err = clSetKernelArg(bucketsortKernel, 3, sizeof(cl_int), &listsize);
  507. err = clSetKernelArg(bucketsortKernel, 4, sizeof(cl_mem), &d_prefixoffsets_input_buff);
  508. err = clSetKernelArg(bucketsortKernel, 5, sizeof(cl_mem), &l_offsets_buff);
  509. if (err != CL_SUCCESS)
  510. {
  511. printf("Error: Failed to set kernel arguments! %d\n", err);
  512. exit(1);
  513. }
  514. err = clEnqueueNDRangeKernel(bucketCommands, bucketsortKernel, 3, NULL, globalfinal, localfinal, 0, NULL, &bucketSortEvent);
  515. if (err)
  516. {
  517. printf("%d Error: Failed to execute bucketsort kernel!\n", err);
  518. }
  519. err = clEnqueueReadBuffer( bucketCommands, bucketOutput, CL_TRUE, 0, (listsize + (DIVISIONS*4))*sizeof(float), d_output, 0, NULL, NULL );
  520. if (err != CL_SUCCESS)
  521. {
  522. printf("Error: Failed to read d_output array! %d\n", err);
  523. }
  524. clWaitForEvents(1 , &bucketSortEvent);
  525. clFinish(bucketCommands);
  526. clGetEventProfilingInfo(bucketSortEvent, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
  527. clGetEventProfilingInfo(bucketSortEvent, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
  528. total_time = time_end - time_start;
  529. sum+= total_time/1000000;
  530. printf("Bucket Sort Kernel Time: %0.3f \n", total_time/1000000);
  531. }
  532. double getBucketTime() {
  533. return sum;
  534. }
  535. ////////////////////////////////////////////////////////////////////////////////
  536. // Given a histogram of the list, figure out suitable pivotpoints that divide
  537. // the list into approximately listsize/divisions elements each
  538. ////////////////////////////////////////////////////////////////////////////////
  539. void calcPivotPoints(float *histogram, int histosize, int listsize,
  540. int divisions, float min, float max, float *pivotPoints, float histo_width)
  541. {
  542. float elemsPerSlice = listsize/(float)divisions;
  543. float startsAt = min;
  544. float endsAt = min + histo_width;
  545. float we_need = elemsPerSlice;
  546. int p_idx = 0;
  547. for(int i=0; i<histosize; i++)
  548. {
  549. if(i == histosize - 1){
  550. if(!(p_idx < divisions)){
  551. pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
  552. }
  553. break;
  554. }
  555. while(histogram[i] > we_need){
  556. if(!(p_idx < divisions)){
  557. printf("i=%d, p_idx = %d, divisions = %d\n", i, p_idx, divisions);
  558. exit(0);
  559. }
  560. pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
  561. startsAt += (we_need/histogram[i]) * histo_width;
  562. histogram[i] -= we_need;
  563. we_need = elemsPerSlice;
  564. }
  565. // grab what we can from what remains of it
  566. we_need -= histogram[i];
  567. startsAt = endsAt;
  568. endsAt += histo_width;
  569. }
  570. while(p_idx < divisions){
  571. pivotPoints[p_idx] = pivotPoints[p_idx-1];
  572. p_idx++;
  573. }
  574. }