master.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // #ifdef __cplusplus
  2. // extern "C" {
  3. // #endif
  4. //========================================================================================================================================================================================================200
  5. // DEFINE / INCLUDE
  6. //========================================================================================================================================================================================================200
  7. //======================================================================================================================================================150
  8. // COMMON
  9. //======================================================================================================================================================150
  10. #include "../common.h" // (in path provided here)
  11. //======================================================================================================================================================150
  12. // KERNEL
  13. //======================================================================================================================================================150
  14. #include "./kernel_fin.c" // (in path provided here)
  15. #include "../util/opencl/opencl.h" // (in path provided here)
  16. //======================================================================================================================================================150
  17. // LIBRARIES
  18. //======================================================================================================================================================150
  19. #include <stdio.h> // (in path known to compiler) needed by printf
  20. #include <CL/cl.h> // (in path provided to compiler) needed by OpenCL types and functions
  21. //======================================================================================================================================================150
  22. // END
  23. //======================================================================================================================================================150
  24. //========================================================================================================================================================================================================200
  25. // MAIN FUNCTION
  26. //========================================================================================================================================================================================================200
  27. void
  28. master( fp timeinst,
  29. fp *initvalu,
  30. fp *parameter,
  31. fp *finavalu,
  32. fp *com,
  33. cl_mem d_initvalu,
  34. cl_mem d_finavalu,
  35. cl_mem d_params,
  36. cl_mem d_com,
  37. cl_command_queue command_queue,
  38. cl_kernel kernel,
  39. long long *timecopyin,
  40. long long *timekernel,
  41. long long *timecopyout)
  42. {
  43. //======================================================================================================================================================150
  44. // VARIABLES
  45. //======================================================================================================================================================150
  46. //timer
  47. long long time0;
  48. long long time1;
  49. long long time2;
  50. long long time3;
  51. // counters
  52. int i;
  53. // offset pointers
  54. int initvalu_offset_ecc; // 46 points
  55. int initvalu_offset_Dyad; // 15 points
  56. int initvalu_offset_SL; // 15 points
  57. int initvalu_offset_Cyt; // 15 poitns
  58. // common variables
  59. cl_int error;
  60. time0 = get_time();
  61. //======================================================================================================================================================150
  62. // COPY DATA TO GPU MEMORY
  63. //======================================================================================================================================================150
  64. //====================================================================================================100
  65. // initvalu
  66. //====================================================================================================100
  67. int d_initvalu_mem;
  68. d_initvalu_mem = EQUATIONS * sizeof(fp);
  69. error = clEnqueueWriteBuffer( command_queue, // command queue
  70. d_initvalu, // destination
  71. 1, // block the source from access until this copy operation complates (1=yes, 0=no)
  72. 0, // offset in destination to write to
  73. d_initvalu_mem, // size to be copied
  74. initvalu, // source
  75. 0, // # of events in the list of events to wait for
  76. NULL, // list of events to wait for
  77. NULL); // ID of this operation to be used by waiting operations
  78. if (error != CL_SUCCESS)
  79. fatal_CL(error, __LINE__);
  80. //====================================================================================================100
  81. // parameter
  82. //====================================================================================================100
  83. int d_params_mem;
  84. d_params_mem = PARAMETERS * sizeof(fp);
  85. error = clEnqueueWriteBuffer( command_queue,
  86. d_params,
  87. 1,
  88. 0,
  89. d_params_mem,
  90. parameter,
  91. 0,
  92. NULL,
  93. NULL);
  94. if (error != CL_SUCCESS)
  95. fatal_CL(error, __LINE__);
  96. //====================================================================================================100
  97. // END
  98. //====================================================================================================100
  99. time1 = get_time();
  100. //======================================================================================================================================================150
  101. // GPU: KERNEL
  102. //======================================================================================================================================================150
  103. //====================================================================================================100
  104. // KERNEL EXECUTION PARAMETERS
  105. //====================================================================================================100
  106. size_t local_work_size[1];
  107. local_work_size[0] = NUMBER_THREADS;
  108. size_t global_work_size[1];
  109. global_work_size[0] = 2*NUMBER_THREADS;
  110. // printf("# of blocks = %d, # of threads/block = %d (ensure that device can handle)\n", (int)global_work_size[0]/(int)local_work_size[0], (int)local_work_size[0]);
  111. //====================================================================================================100
  112. // KERNEL ARGUMENTS
  113. //====================================================================================================100
  114. clSetKernelArg( kernel,
  115. 0,
  116. sizeof(int),
  117. (void *) &timeinst);
  118. clSetKernelArg( kernel,
  119. 1,
  120. sizeof(cl_mem),
  121. (void *) &d_initvalu);
  122. clSetKernelArg( kernel,
  123. 2,
  124. sizeof(cl_mem),
  125. (void *) &d_finavalu);
  126. clSetKernelArg( kernel,
  127. 3,
  128. sizeof(cl_mem),
  129. (void *) &d_params);
  130. clSetKernelArg( kernel,
  131. 4,
  132. sizeof(cl_mem),
  133. (void *) &d_com);
  134. //====================================================================================================100
  135. // KERNEL
  136. //====================================================================================================100
  137. error = clEnqueueNDRangeKernel( command_queue,
  138. kernel,
  139. 1,
  140. NULL,
  141. global_work_size,
  142. local_work_size,
  143. 0,
  144. NULL,
  145. NULL);
  146. if (error != CL_SUCCESS)
  147. fatal_CL(error, __LINE__);
  148. // Wait for all operations to finish, much like synchronizing threads in CUDA
  149. error = clFinish(command_queue);
  150. if (error != CL_SUCCESS)
  151. fatal_CL(error, __LINE__);
  152. time2 = get_time();
  153. //======================================================================================================================================================150
  154. // COPY DATA TO SYSTEM MEMORY
  155. //======================================================================================================================================================150
  156. //====================================================================================================100
  157. // finavalu
  158. //====================================================================================================100
  159. int d_finavalu_mem;
  160. d_finavalu_mem = EQUATIONS * sizeof(fp);
  161. error = clEnqueueReadBuffer(command_queue, // The command queue.
  162. d_finavalu, // The image on the device.
  163. CL_TRUE, // Blocking? (ie. Wait at this line until read has finished?)
  164. 0, // Offset. None in this case.
  165. d_finavalu_mem, // Size to copy.
  166. finavalu, // The pointer to the image on the host.
  167. 0, // Number of events in wait list. Not used.
  168. NULL, // Event wait list. Not used.
  169. NULL); // Event object for determining status. Not used.
  170. if (error != CL_SUCCESS)
  171. fatal_CL(error, __LINE__);
  172. //====================================================================================================100
  173. // com
  174. //====================================================================================================100
  175. int d_com_mem;
  176. d_com_mem = 3 * sizeof(fp);
  177. error = clEnqueueReadBuffer(command_queue,
  178. d_com,
  179. CL_TRUE,
  180. 0,
  181. d_com_mem,
  182. com,
  183. 0,
  184. NULL,
  185. NULL);
  186. if (error != CL_SUCCESS)
  187. fatal_CL(error, __LINE__);
  188. //====================================================================================================100
  189. // END
  190. //====================================================================================================100
  191. time3 = get_time();
  192. //======================================================================================================================================================150
  193. // CPU: FINAL KERNEL
  194. //======================================================================================================================================================150
  195. // *copyin_time,
  196. // *kernel_time,
  197. // *copyout_time)
  198. timecopyin[0] = timecopyin[0] + (time1-time0);
  199. timekernel[0] = timekernel[0] + (time2-time1);
  200. timecopyout[0] = timecopyout[0] + (time3-time2);
  201. //======================================================================================================================================================150
  202. // CPU: FINAL KERNEL
  203. //======================================================================================================================================================150
  204. initvalu_offset_ecc = 0; // 46 points
  205. initvalu_offset_Dyad = 46; // 15 points
  206. initvalu_offset_SL = 61; // 15 points
  207. initvalu_offset_Cyt = 76; // 15 poitns
  208. kernel_fin( initvalu,
  209. initvalu_offset_ecc,
  210. initvalu_offset_Dyad,
  211. initvalu_offset_SL,
  212. initvalu_offset_Cyt,
  213. parameter,
  214. finavalu,
  215. com[0],
  216. com[1],
  217. com[2]);
  218. //======================================================================================================================================================150
  219. // COMPENSATION FOR NANs and INFs
  220. //======================================================================================================================================================150
  221. for(i=0; i<EQUATIONS; i++){
  222. if (isnan(finavalu[i]) == 1){
  223. finavalu[i] = 0.0001; // for NAN set rate of change to 0.0001
  224. }
  225. else if (isinf(finavalu[i]) == 1){
  226. finavalu[i] = 0.0001; // for INF set rate of change to 0.0001
  227. }
  228. }
  229. //======================================================================================================================================================150
  230. // END
  231. //======================================================================================================================================================150
  232. }
  233. //========================================================================================================================================================================================================200
  234. // END
  235. //========================================================================================================================================================================================================200
  236. // #ifdef __cplusplus
  237. // }
  238. // #endif