stencil-kernels.c 22 KB

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