stencil-kernels.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "stencil.h"
  18. #include <sys/time.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. #ifndef timersub
  21. #define timersub(x, y, res) \
  22. do \
  23. { \
  24. (res)->tv_sec = (x)->tv_sec - (y)->tv_sec; \
  25. (res)->tv_usec = (x)->tv_usec - (y)->tv_usec; \
  26. if ((res)->tv_usec < 0) \
  27. { \
  28. (res)->tv_sec--; \
  29. (res)->tv_usec += 1000000; \
  30. } \
  31. } while (0)
  32. #endif
  33. #ifndef timeradd
  34. #define timeradd(x, y, res) \
  35. do \
  36. { \
  37. (res)->tv_sec = (x)->tv_sec + (y)->tv_sec; \
  38. (res)->tv_usec = (x)->tv_usec + (y)->tv_usec; \
  39. if ((res)->tv_usec >= 1000000) \
  40. { \
  41. (res)->tv_sec++; \
  42. (res)->tv_usec -= 1000000; \
  43. } \
  44. } while (0)
  45. #endif
  46. /* Computation Kernels */
  47. /*
  48. * There are three codeletets:
  49. *
  50. * - cl_update, which takes a block and the boundaries of its neighbours, loads
  51. * the boundaries into the block and perform some update loops:
  52. *
  53. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  54. * | ... |
  55. * | | +------------------+ +------------------+
  56. * | #N+1 | | #N+1 bottom copy====>#N+1 bottom copy |
  57. * +-------------+ +------------------+ +------------------+
  58. * | #N top copy | | #N top copy | | |
  59. * +-------------+ +------------------+ | |
  60. * | #N |
  61. * ...
  62. * | | +----------------+ +----------------------+
  63. * | | | #N bottom copy | | block #N bottom copy |
  64. * ^ +------------------+ +----------------+ +----------------------+
  65. * | | #N-1 top copy <====#N-1 top copy | | block #N-1 |
  66. * | +------------------+ +----------------+ | |
  67. * Z ...
  68. *
  69. * - save_cl_top, which take a block and its top boundary, and saves the top of
  70. * the block into the boundary (to be given as bottom of the neighbour above
  71. * this block).
  72. *
  73. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  74. * | ... |
  75. * | | +------------------+ +------------------+
  76. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  77. * +-------------+ +------------------+ +------------------+
  78. * | #N top copy | | #N top copy <==== |
  79. * +-------------+ +------------------+ |..................|
  80. * | #N |
  81. * ...
  82. * | | +----------------+ +----------------------+
  83. * | | | #N bottom copy | | block #N bottom copy |
  84. * ^ +------------------+ +----------------+ +----------------------+
  85. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  86. * | +------------------+ +----------------+ | |
  87. * Z ...
  88. *
  89. * - save_cl_bottom, same for the bottom
  90. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  91. * | ... |
  92. * | | +------------------+ +------------------+
  93. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  94. * +-------------+ +------------------+ +------------------+
  95. * | #N top copy | | #N top copy | | |
  96. * +-------------+ +------------------+ | |
  97. * | #N |
  98. * ...
  99. * |..................| +----------------+ +----------------------+
  100. * | ====>#N bottom copy | | block #N bottom copy |
  101. * ^ +------------------+ +----------------+ +----------------------+
  102. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  103. * | +------------------+ +----------------+ | |
  104. * Z ...
  105. *
  106. * The idea is that the computation buffers thus don't have to move, only their
  107. * boundaries are copied to buffers that do move (be it CPU/GPU, GPU/GPU or via
  108. * MPI)
  109. *
  110. * For each of the buffers above, there are two (0/1) buffers to make new/old switch costless.
  111. */
  112. #if 0
  113. # define DEBUG(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
  114. #else
  115. # define DEBUG(fmt, ...) (void) 0
  116. #endif
  117. /* Record which GPU ran which block, for nice pictures */
  118. int who_runs_what_len;
  119. int *who_runs_what;
  120. int *who_runs_what_index;
  121. struct timeval *last_tick;
  122. /* Achieved iterations */
  123. static int achieved_iter;
  124. /* Record how many updates each worker performed */
  125. unsigned update_per_worker[STARPU_NMAXWORKERS];
  126. static void record_who_runs_what(struct block_description *block)
  127. {
  128. struct timeval tv, tv2, diff, delta = {.tv_sec = 0, .tv_usec = get_ticks() * 1000};
  129. int workerid = starpu_worker_get_id();
  130. gettimeofday(&tv, NULL);
  131. timersub(&tv, &start, &tv2);
  132. timersub(&tv2, &last_tick[block->bz], &diff);
  133. while (timercmp(&diff, &delta, >=))
  134. {
  135. timeradd(&last_tick[block->bz], &delta, &last_tick[block->bz]);
  136. timersub(&tv2, &last_tick[block->bz], &diff);
  137. if (who_runs_what_index[block->bz] < who_runs_what_len)
  138. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
  139. }
  140. if (who_runs_what_index[block->bz] < who_runs_what_len)
  141. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);
  142. }
  143. static void check_load(struct starpu_block_interface *block, struct starpu_block_interface *boundary)
  144. {
  145. /* Sanity checks */
  146. STARPU_ASSERT(block->nx == boundary->nx);
  147. STARPU_ASSERT(block->ny == boundary->ny);
  148. STARPU_ASSERT(boundary->nz == K);
  149. /* NB: this is not fully garanteed ... but it's *very* likely and that
  150. * makes our life much simpler */
  151. STARPU_ASSERT(block->ldy == boundary->ldy);
  152. STARPU_ASSERT(block->ldz == boundary->ldz);
  153. }
  154. /*
  155. * Load a neighbour's boundary into block, CPU version
  156. */
  157. static void load_subblock_from_buffer_cpu(void *_block,
  158. void *_boundary,
  159. unsigned firstz)
  160. {
  161. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  162. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  163. check_load(block, boundary);
  164. /* We do a contiguous memory transfer */
  165. size_t boundary_size = K*block->ldz*block->elemsize;
  166. unsigned offset = firstz*block->ldz;
  167. TYPE *block_data = (TYPE *)block->ptr;
  168. TYPE *boundary_data = (TYPE *)boundary->ptr;
  169. memcpy(&block_data[offset], boundary_data, boundary_size);
  170. }
  171. /*
  172. * Load a neighbour's boundary into block, CUDA version
  173. */
  174. #ifdef STARPU_USE_CUDA
  175. static void load_subblock_from_buffer_cuda(void *_block,
  176. void *_boundary,
  177. unsigned firstz)
  178. {
  179. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  180. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  181. check_load(block, boundary);
  182. /* We do a contiguous memory transfer */
  183. size_t boundary_size = K*block->ldz*block->elemsize;
  184. unsigned offset = firstz*block->ldz;
  185. TYPE *block_data = (TYPE *)block->ptr;
  186. TYPE *boundary_data = (TYPE *)boundary->ptr;
  187. cudaMemcpyAsync(&block_data[offset], boundary_data, boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  188. }
  189. /*
  190. * cl_update (CUDA version)
  191. */
  192. static void update_func_cuda(void *descr[], void *arg)
  193. {
  194. struct block_description *block = arg;
  195. int workerid = starpu_worker_get_id();
  196. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  197. if (block->bz == 0)
  198. fprintf(stderr,"!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  199. else
  200. DEBUG( "!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  201. #ifdef STARPU_USE_MPI
  202. int rank = 0;
  203. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  204. DEBUG( "!!! RANK %d !!!\n", rank);
  205. #endif
  206. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  207. unsigned block_size_z = get_block_size(block->bz);
  208. unsigned i;
  209. update_per_worker[workerid]++;
  210. record_who_runs_what(block);
  211. /*
  212. * Load neighbours' boundaries : TOP
  213. */
  214. /* The offset along the z axis is (block_size_z + K) */
  215. load_subblock_from_buffer_cuda(descr[0], descr[2], block_size_z+K);
  216. load_subblock_from_buffer_cuda(descr[1], descr[3], block_size_z+K);
  217. /*
  218. * Load neighbours' boundaries : BOTTOM
  219. */
  220. load_subblock_from_buffer_cuda(descr[0], descr[4], 0);
  221. load_subblock_from_buffer_cuda(descr[1], descr[5], 0);
  222. /*
  223. * Stencils ... do the actual work here :) TODO
  224. */
  225. for (i=1; i<=K; i++)
  226. {
  227. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  228. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  229. /* Shadow data */
  230. cuda_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  231. /* And perform actual computation */
  232. #ifdef LIFE
  233. cuda_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  234. #else
  235. cudaMemcpyAsync(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  236. #endif /* LIFE */
  237. }
  238. cudaError_t cures;
  239. if ((cures = cudaStreamSynchronize(starpu_cuda_get_local_stream())) != cudaSuccess)
  240. STARPU_CUDA_REPORT_ERROR(cures);
  241. if (block->bz == 0)
  242. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  243. }
  244. #endif /* STARPU_USE_CUDA */
  245. /*
  246. * Load a neighbour's boundary into block, OpenCL version
  247. */
  248. #ifdef STARPU_USE_OPENCL
  249. static void load_subblock_from_buffer_opencl(struct starpu_block_interface *block,
  250. struct starpu_block_interface *boundary,
  251. unsigned firstz)
  252. {
  253. check_load(block, boundary);
  254. /* We do a contiguous memory transfer */
  255. size_t boundary_size = K*block->ldz*block->elemsize;
  256. unsigned offset = firstz*block->ldz;
  257. cl_mem block_data = (cl_mem)block->dev_handle;
  258. cl_mem boundary_data = (cl_mem)boundary->dev_handle;
  259. cl_event event;
  260. cl_command_queue cq;
  261. starpu_opencl_get_current_queue(&cq);
  262. cl_int ret = clEnqueueCopyBuffer(cq, boundary_data, block_data, 0, offset, boundary_size, 0, NULL, &event);
  263. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  264. clWaitForEvents(1, &event);
  265. clReleaseEvent(event);
  266. }
  267. /*
  268. * cl_update (OpenCL version)
  269. */
  270. static void update_func_opencl(void *descr[], void *arg)
  271. {
  272. struct block_description *block = arg;
  273. int workerid = starpu_worker_get_id();
  274. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  275. if (block->bz == 0)
  276. fprintf(stderr,"!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  277. else
  278. DEBUG( "!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  279. #ifdef STARPU_USE_MPI
  280. int rank = 0;
  281. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  282. DEBUG( "!!! RANK %d !!!\n", rank);
  283. #endif
  284. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  285. unsigned block_size_z = get_block_size(block->bz);
  286. unsigned i;
  287. update_per_worker[workerid]++;
  288. record_who_runs_what(block);
  289. cl_command_queue cq;
  290. starpu_opencl_get_current_queue(&cq);
  291. /*
  292. * Load neighbours' boundaries : TOP
  293. */
  294. /* The offset along the z axis is (block_size_z + K) */
  295. load_subblock_from_buffer_opencl(descr[0], descr[2], block_size_z+K);
  296. load_subblock_from_buffer_opencl(descr[1], descr[3], block_size_z+K);
  297. /*
  298. * Load neighbours' boundaries : BOTTOM
  299. */
  300. load_subblock_from_buffer_opencl(descr[0], descr[4], 0);
  301. load_subblock_from_buffer_opencl(descr[1], descr[5], 0);
  302. /*
  303. * Stencils ... do the actual work here :) TODO
  304. */
  305. for (i=1; i<=K; i++)
  306. {
  307. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  308. TYPE *old = (void*) oldb->dev_handle, *newer = (void*) newb->dev_handle;
  309. /* Shadow data */
  310. opencl_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  311. /* And perform actual computation */
  312. #ifdef LIFE
  313. opencl_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  314. #else
  315. cl_event event;
  316. cl_int ret = clEnqueueCopyBuffer(cq, old, newer, 0, 0, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), 0, NULL, &event);
  317. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  318. clWaitForEvents(1, &event);
  319. clReleaseEvent(event);
  320. #endif /* LIFE */
  321. }
  322. #ifndef LIFE
  323. cl_int err;
  324. if ((err = clFinish(cq)))
  325. STARPU_OPENCL_REPORT_ERROR(err);
  326. #endif
  327. if (block->bz == 0)
  328. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  329. }
  330. #endif /* STARPU_USE_OPENCL */
  331. /*
  332. * cl_update (CPU version)
  333. */
  334. void update_func_cpu(void *descr[], void *arg)
  335. {
  336. struct block_description *block = (struct block_description *) arg;
  337. int workerid = starpu_worker_get_id();
  338. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  339. if (block->bz == 0)
  340. FPRINTF(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  341. else
  342. DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  343. #ifdef STARPU_USE_MPI
  344. int rank = 0;
  345. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  346. DEBUG( "!!! RANK %d !!!\n", rank);
  347. #endif
  348. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  349. unsigned block_size_z = get_block_size(block->bz);
  350. unsigned i;
  351. update_per_worker[workerid]++;
  352. record_who_runs_what(block);
  353. /*
  354. * Load neighbours' boundaries : TOP
  355. */
  356. /* The offset along the z axis is (block_size_z + K) */
  357. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  358. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  359. /*
  360. * Load neighbours' boundaries : BOTTOM
  361. */
  362. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  363. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  364. /*
  365. * Stencils ... do the actual work here :) TODO
  366. */
  367. for (i=1; i<=K; i++)
  368. {
  369. struct starpu_block_interface *oldb = (struct starpu_block_interface *) descr[i%2], *newb = (struct starpu_block_interface *) descr[(i+1)%2];
  370. TYPE *old = (TYPE*) oldb->ptr, *newer = (TYPE*) newb->ptr;
  371. /* Shadow data */
  372. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  373. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  374. unsigned x, y, z;
  375. unsigned stepx = 1;
  376. unsigned stepy = 1;
  377. unsigned stepz = 1;
  378. unsigned idx = 0;
  379. unsigned idy = 0;
  380. unsigned idz = 0;
  381. TYPE *ptr = old;
  382. # include "shadow.h"
  383. /* And perform actual computation */
  384. #ifdef LIFE
  385. life_update(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  386. #else
  387. memcpy(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer));
  388. #endif /* LIFE */
  389. }
  390. if (block->bz == 0)
  391. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  392. }
  393. /* Performance model and codelet structure */
  394. static struct starpu_perfmodel cl_update_model =
  395. {
  396. .type = STARPU_HISTORY_BASED,
  397. .symbol = "cl_update"
  398. };
  399. struct starpu_codelet cl_update =
  400. {
  401. .cpu_funcs = {update_func_cpu, NULL},
  402. .cpu_funcs_name = {"update_func_cpu", NULL},
  403. #ifdef STARPU_USE_CUDA
  404. .cuda_funcs = {update_func_cuda, NULL},
  405. #endif
  406. #ifdef STARPU_USE_OPENCL
  407. .opencl_funcs = {update_func_opencl, NULL},
  408. #endif
  409. .model = &cl_update_model,
  410. .nbuffers = 6,
  411. .modes = {STARPU_RW, STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  412. };
  413. /*
  414. * Save the block internal boundaries to give them to our neighbours.
  415. */
  416. /* CPU version */
  417. static void load_subblock_into_buffer_cpu(void *_block,
  418. void *_boundary,
  419. unsigned firstz)
  420. {
  421. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  422. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  423. check_load(block, boundary);
  424. /* We do a contiguous memory transfer */
  425. size_t boundary_size = K*block->ldz*block->elemsize;
  426. unsigned offset = firstz*block->ldz;
  427. TYPE *block_data = (TYPE *)block->ptr;
  428. TYPE *boundary_data = (TYPE *)boundary->ptr;
  429. memcpy(boundary_data, &block_data[offset], boundary_size);
  430. }
  431. /* CUDA version */
  432. #ifdef STARPU_USE_CUDA
  433. static void load_subblock_into_buffer_cuda(void *_block,
  434. void *_boundary,
  435. unsigned firstz)
  436. {
  437. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  438. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  439. check_load(block, boundary);
  440. /* We do a contiguous memory transfer */
  441. size_t boundary_size = K*block->ldz*block->elemsize;
  442. unsigned offset = firstz*block->ldz;
  443. TYPE *block_data = (TYPE *)block->ptr;
  444. TYPE *boundary_data = (TYPE *)boundary->ptr;
  445. cudaMemcpyAsync(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  446. }
  447. #endif /* STARPU_USE_CUDA */
  448. /* OPENCL version */
  449. #ifdef STARPU_USE_OPENCL
  450. static void load_subblock_into_buffer_opencl(struct starpu_block_interface *block,
  451. struct starpu_block_interface *boundary,
  452. unsigned firstz)
  453. {
  454. check_load(block, boundary);
  455. /* We do a contiguous memory transfer */
  456. size_t boundary_size = K*block->ldz*block->elemsize;
  457. unsigned offset = firstz*block->ldz;
  458. cl_mem block_data = (cl_mem)block->dev_handle;
  459. cl_mem boundary_data = (cl_mem)boundary->dev_handle;
  460. cl_command_queue cq;
  461. starpu_opencl_get_current_queue(&cq);
  462. cl_event event;
  463. cl_int ret = clEnqueueCopyBuffer(cq, block_data, boundary_data, offset, 0, boundary_size, 0, NULL, &event);
  464. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  465. clWaitForEvents(1, &event);
  466. clReleaseEvent(event);
  467. }
  468. #endif /* STARPU_USE_OPENCL */
  469. /* Record how many top/bottom saves each worker performed */
  470. unsigned top_per_worker[STARPU_NMAXWORKERS];
  471. unsigned bottom_per_worker[STARPU_NMAXWORKERS];
  472. /* top save, CPU version */
  473. void dummy_func_top_cpu(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  474. {
  475. struct block_description *block = (struct block_description *) arg;
  476. int workerid = starpu_worker_get_id();
  477. top_per_worker[workerid]++;
  478. DEBUG( "DO SAVE Bottom block %d\n", block->bz);
  479. /* The offset along the z axis is (block_size_z + K)- K */
  480. unsigned block_size_z = get_block_size(block->bz);
  481. load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
  482. load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
  483. }
  484. /* bottom save, CPU version */
  485. void dummy_func_bottom_cpu(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  486. {
  487. struct block_description *block = (struct block_description *) arg;
  488. int workerid = starpu_worker_get_id();
  489. bottom_per_worker[workerid]++;
  490. DEBUG( "DO SAVE Top block %d\n", block->bz);
  491. load_subblock_into_buffer_cpu(descr[0], descr[2], K);
  492. load_subblock_into_buffer_cpu(descr[1], descr[3], K);
  493. }
  494. /* top save, CUDA version */
  495. #ifdef STARPU_USE_CUDA
  496. static void dummy_func_top_cuda(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  497. {
  498. struct block_description *block = (struct block_description *) arg;
  499. int workerid = starpu_worker_get_id();
  500. top_per_worker[workerid]++;
  501. DEBUG( "DO SAVE Top block %d\n", block->bz);
  502. /* The offset along the z axis is (block_size_z + K)- K */
  503. unsigned block_size_z = get_block_size(block->bz);
  504. load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
  505. load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
  506. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  507. }
  508. /* bottom save, CUDA version */
  509. static void dummy_func_bottom_cuda(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  510. {
  511. struct block_description *block = (struct block_description *) arg;
  512. int workerid = starpu_worker_get_id();
  513. bottom_per_worker[workerid]++;
  514. DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);
  515. load_subblock_into_buffer_cuda(descr[0], descr[2], K);
  516. load_subblock_into_buffer_cuda(descr[1], descr[3], K);
  517. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  518. }
  519. #endif /* STARPU_USE_CUDA */
  520. /* top save, OpenCL version */
  521. #ifdef STARPU_USE_OPENCL
  522. static void dummy_func_top_opencl(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  523. {
  524. struct block_description *block = (struct block_description *) arg;
  525. int workerid = starpu_worker_get_id();
  526. top_per_worker[workerid]++;
  527. DEBUG( "DO SAVE Top block %d\n", block->bz);
  528. /* The offset along the z axis is (block_size_z + K)- K */
  529. unsigned block_size_z = get_block_size(block->bz);
  530. load_subblock_into_buffer_opencl(descr[0], descr[2], block_size_z);
  531. load_subblock_into_buffer_opencl(descr[1], descr[3], block_size_z);
  532. cl_command_queue cq;
  533. starpu_opencl_get_current_queue(&cq);
  534. clFinish(cq);
  535. }
  536. /* bottom save, OPENCL version */
  537. static void dummy_func_bottom_opencl(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  538. {
  539. struct block_description *block = (struct block_description *) arg;
  540. int workerid = starpu_worker_get_id();
  541. bottom_per_worker[workerid]++;
  542. DEBUG( "DO SAVE Bottom block %d on OPENCL\n", block->bz);
  543. load_subblock_into_buffer_opencl(descr[0], descr[2], K);
  544. load_subblock_into_buffer_opencl(descr[1], descr[3], K);
  545. cl_command_queue cq;
  546. starpu_opencl_get_current_queue(&cq);
  547. clFinish(cq);
  548. }
  549. #endif /* STARPU_USE_OPENCL */
  550. /* Performance models and codelet for save */
  551. static struct starpu_perfmodel save_cl_bottom_model =
  552. {
  553. .type = STARPU_HISTORY_BASED,
  554. .symbol = "save_cl_bottom"
  555. };
  556. static struct starpu_perfmodel save_cl_top_model =
  557. {
  558. .type = STARPU_HISTORY_BASED,
  559. .symbol = "save_cl_top"
  560. };
  561. struct starpu_codelet save_cl_bottom =
  562. {
  563. .cpu_funcs = {dummy_func_bottom_cpu, NULL},
  564. .cpu_funcs_name = {"dummy_func_bottom_cpu", NULL},
  565. #ifdef STARPU_USE_CUDA
  566. .cuda_funcs = {dummy_func_bottom_cuda, NULL},
  567. #endif
  568. #ifdef STARPU_USE_OPENCL
  569. .opencl_funcs = {dummy_func_bottom_opencl, NULL},
  570. #endif
  571. .model = &save_cl_bottom_model,
  572. .nbuffers = 4,
  573. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  574. };
  575. struct starpu_codelet save_cl_top =
  576. {
  577. .cpu_funcs = {dummy_func_top_cpu, NULL},
  578. .cpu_funcs_name = {"dummy_func_top_cpu", NULL},
  579. #ifdef STARPU_USE_CUDA
  580. .cuda_funcs = {dummy_func_top_cuda, NULL},
  581. #endif
  582. #ifdef STARPU_USE_OPENCL
  583. .opencl_funcs = {dummy_func_top_opencl, NULL},
  584. #endif
  585. .model = &save_cl_top_model,
  586. .nbuffers = 4,
  587. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  588. };