kernel_gpu_opencl.cl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // #ifdef __cplusplus
  2. // extern "C" {
  3. // #endif
  4. //========================================================================================================================================================================================================200
  5. // DEFINE/INCLUDE
  6. //========================================================================================================================================================================================================200
  7. //======================================================================================================================================================150
  8. // DEFINE
  9. //======================================================================================================================================================150
  10. // double precision support (switch between as needed for NVIDIA/AMD)
  11. #ifdef AMDAPP
  12. #pragma OPENCL EXTENSION cl_amd_fp64 : enable
  13. #else
  14. #pragma OPENCL EXTENSION cl_khr_fp64 : enable
  15. #endif
  16. // clBuildProgram compiler cannot link this file for some reason, so had to redefine constants and structures below
  17. // #include ../common.h // (in directory specified to compiler) main function header
  18. //======================================================================================================================================================150
  19. // DEFINE (had to bring from ../common.h here because feature of including headers in clBuildProgram does not work for some reason)
  20. //======================================================================================================================================================150
  21. // change to double if double precision needed
  22. #define fp float
  23. //#define DEFAULT_ORDER 256
  24. //======================================================================================================================================================150
  25. // STRUCTURES (had to bring from ../common.h here because feature of including headers in clBuildProgram does not work for some reason)
  26. //======================================================================================================================================================150
  27. // Type representing the record to which a given key refers. In a real B+ tree system, the record would hold data (in a database) or a file (in an operating system) or some other information.
  28. // Users can rewrite this part of the code to change the type and content of the value field.
  29. typedef struct record {
  30. int value;
  31. } record;
  32. // ???
  33. typedef struct knode {
  34. int location;
  35. int indices [DEFAULT_ORDER + 1];
  36. int keys [DEFAULT_ORDER + 1];
  37. bool is_leaf;
  38. int num_keys;
  39. } knode;
  40. //========================================================================================================================================================================================================200
  41. // findK function
  42. //========================================================================================================================================================================================================200
  43. __kernel void
  44. findK( long height,
  45. __global knode *knodesD,
  46. long knodes_elem,
  47. __global record *recordsD,
  48. __global long *currKnodeD,
  49. __global long *offsetD,
  50. __global int *keysD,
  51. __global record *ansD)
  52. {
  53. // private thread IDs
  54. int thid = get_local_id(0);
  55. int bid = get_group_id(0);
  56. // processtree levels
  57. int i;
  58. for(i = 0; i < height; i++){
  59. // if value is between the two keys
  60. if((knodesD[currKnodeD[bid]].keys[thid]) <= keysD[bid] && (knodesD[currKnodeD[bid]].keys[thid+1] > keysD[bid])){
  61. // this conditional statement is inserted to avoid crush due to but in original code
  62. // "offset[bid]" calculated below that addresses knodes[] in the next iteration goes outside of its bounds cause segmentation fault
  63. // more specifically, values saved into knodes->indices in the main function are out of bounds of knodes that they address
  64. if(knodesD[offsetD[bid]].indices[thid] < knodes_elem){
  65. offsetD[bid] = knodesD[offsetD[bid]].indices[thid];
  66. }
  67. }
  68. //__syncthreads();
  69. barrier(CLK_LOCAL_MEM_FENCE);
  70. // set for next tree level
  71. if(thid==0){
  72. currKnodeD[bid] = offsetD[bid];
  73. }
  74. //__syncthreads();
  75. barrier(CLK_LOCAL_MEM_FENCE);
  76. }
  77. //At this point, we have a candidate leaf node which may contain
  78. //the target record. Check each key to hopefully find the record
  79. if(knodesD[currKnodeD[bid]].keys[thid] == keysD[bid]){
  80. ansD[bid].value = recordsD[knodesD[currKnodeD[bid]].indices[thid]].value;
  81. }
  82. }
  83. //========================================================================================================================================================================================================200
  84. // End
  85. //========================================================================================================================================================================================================200
  86. // #ifdef __cplusplus
  87. // }
  88. // #endif