stencil-kernels.c 24 KB

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