stencil-kernels.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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(struct starpu_block_interface *block, struct starpu_block_interface *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(void *_block,
  154. void *_boundary,
  155. unsigned firstz)
  156. {
  157. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  158. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  159. check_load(block, boundary);
  160. /* We do a contiguous memory transfer */
  161. size_t boundary_size = K*block->ldz*block->elemsize;
  162. unsigned offset = firstz*block->ldz;
  163. TYPE *block_data = (TYPE *)block->ptr;
  164. TYPE *boundary_data = (TYPE *)boundary->ptr;
  165. memcpy(&block_data[offset], boundary_data, boundary_size);
  166. }
  167. /*
  168. * Load a neighbour's boundary into block, CUDA version
  169. */
  170. #ifdef STARPU_USE_CUDA
  171. static void load_subblock_from_buffer_cuda(void *_block,
  172. void *_boundary,
  173. unsigned firstz)
  174. {
  175. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  176. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  177. check_load(block, boundary);
  178. /* We do a contiguous memory transfer */
  179. size_t boundary_size = K*block->ldz*block->elemsize;
  180. unsigned offset = firstz*block->ldz;
  181. TYPE *block_data = (TYPE *)block->ptr;
  182. TYPE *boundary_data = (TYPE *)boundary->ptr;
  183. cudaMemcpyAsync(&block_data[offset], boundary_data, boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  184. }
  185. /*
  186. * cl_update (CUDA version)
  187. */
  188. static void update_func_cuda(void *descr[], void *arg)
  189. {
  190. struct block_description *block = arg;
  191. int workerid = starpu_worker_get_id();
  192. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  193. if (block->bz == 0)
  194. fprintf(stderr,"!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  195. else
  196. DEBUG( "!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  197. #ifdef STARPU_USE_MPI
  198. int rank = 0;
  199. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  200. DEBUG( "!!! RANK %d !!!\n", rank);
  201. #endif
  202. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  203. unsigned block_size_z = get_block_size(block->bz);
  204. unsigned i;
  205. update_per_worker[workerid]++;
  206. record_who_runs_what(block);
  207. /*
  208. * Load neighbours' boundaries : TOP
  209. */
  210. /* The offset along the z axis is (block_size_z + K) */
  211. load_subblock_from_buffer_cuda(descr[0], descr[2], block_size_z+K);
  212. load_subblock_from_buffer_cuda(descr[1], descr[3], block_size_z+K);
  213. /*
  214. * Load neighbours' boundaries : BOTTOM
  215. */
  216. load_subblock_from_buffer_cuda(descr[0], descr[4], 0);
  217. load_subblock_from_buffer_cuda(descr[1], descr[5], 0);
  218. /*
  219. * Stencils ... do the actual work here :) TODO
  220. */
  221. for (i=1; i<=K; i++)
  222. {
  223. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  224. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  225. /* Shadow data */
  226. cuda_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  227. /* And perform actual computation */
  228. #ifdef LIFE
  229. cuda_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  230. #else
  231. cudaMemcpyAsync(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  232. #endif /* LIFE */
  233. }
  234. cudaError_t cures;
  235. if ((cures = cudaStreamSynchronize(starpu_cuda_get_local_stream())) != cudaSuccess)
  236. STARPU_CUDA_REPORT_ERROR(cures);
  237. if (block->bz == 0)
  238. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  239. }
  240. #endif /* STARPU_USE_CUDA */
  241. /*
  242. * Load a neighbour's boundary into block, OpenCL version
  243. */
  244. #ifdef STARPU_USE_OPENCL
  245. static void load_subblock_from_buffer_opencl(struct starpu_block_interface *block,
  246. struct starpu_block_interface *boundary,
  247. unsigned firstz)
  248. {
  249. check_load(block, boundary);
  250. /* We do a contiguous memory transfer */
  251. size_t boundary_size = K*block->ldz*block->elemsize;
  252. unsigned offset = firstz*block->ldz;
  253. cl_mem block_data = (cl_mem)block->ptr;
  254. cl_mem boundary_data = (cl_mem)boundary->ptr;
  255. cl_command_queue cq;
  256. starpu_opencl_get_current_queue(&cq);
  257. clEnqueueCopyBuffer(cq, boundary_data, block_data, 0, offset, boundary_size, 0, NULL, NULL);
  258. }
  259. /*
  260. * cl_update (OpenCL version)
  261. */
  262. static void update_func_opencl(void *descr[], void *arg)
  263. {
  264. struct block_description *block = arg;
  265. int workerid = starpu_worker_get_id();
  266. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  267. if (block->bz == 0)
  268. fprintf(stderr,"!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  269. else
  270. DEBUG( "!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  271. #ifdef STARPU_USE_MPI
  272. int rank = 0;
  273. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  274. DEBUG( "!!! RANK %d !!!\n", rank);
  275. #endif
  276. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  277. unsigned block_size_z = get_block_size(block->bz);
  278. unsigned i;
  279. update_per_worker[workerid]++;
  280. record_who_runs_what(block);
  281. cl_command_queue cq;
  282. starpu_opencl_get_current_queue(&cq);
  283. /*
  284. * Load neighbours' boundaries : TOP
  285. */
  286. /* The offset along the z axis is (block_size_z + K) */
  287. load_subblock_from_buffer_opencl(descr[0], descr[2], block_size_z+K);
  288. load_subblock_from_buffer_opencl(descr[1], descr[3], block_size_z+K);
  289. /*
  290. * Load neighbours' boundaries : BOTTOM
  291. */
  292. load_subblock_from_buffer_opencl(descr[0], descr[4], 0);
  293. load_subblock_from_buffer_opencl(descr[1], descr[5], 0);
  294. /*
  295. * Stencils ... do the actual work here :) TODO
  296. */
  297. for (i=1; i<=K; i++)
  298. {
  299. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  300. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  301. /* Shadow data */
  302. opencl_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  303. /* And perform actual computation */
  304. #ifdef LIFE
  305. opencl_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  306. #else
  307. clEnqueueCopyBuffer(cq, old, newer, 0, 0, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), 0, NULL, NULL);
  308. #endif /* LIFE */
  309. }
  310. cl_int err;
  311. if ((err = clFinish(cq)))
  312. STARPU_OPENCL_REPORT_ERROR(err);
  313. if (block->bz == 0)
  314. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  315. }
  316. #endif /* STARPU_USE_OPENCL */
  317. /*
  318. * cl_update (CPU version)
  319. */
  320. static void update_func_cpu(void *descr[], void *arg)
  321. {
  322. struct block_description *block = (struct block_description *) arg;
  323. int workerid = starpu_worker_get_id();
  324. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  325. if (block->bz == 0)
  326. fprintf(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  327. else
  328. DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  329. #ifdef STARPU_USE_MPI
  330. int rank = 0;
  331. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  332. DEBUG( "!!! RANK %d !!!\n", rank);
  333. #endif
  334. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  335. unsigned block_size_z = get_block_size(block->bz);
  336. unsigned i;
  337. update_per_worker[workerid]++;
  338. record_who_runs_what(block);
  339. /*
  340. * Load neighbours' boundaries : TOP
  341. */
  342. /* The offset along the z axis is (block_size_z + K) */
  343. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  344. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  345. /*
  346. * Load neighbours' boundaries : BOTTOM
  347. */
  348. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  349. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  350. /*
  351. * Stencils ... do the actual work here :) TODO
  352. */
  353. for (i=1; i<=K; i++)
  354. {
  355. struct starpu_block_interface *oldb = (struct starpu_block_interface *) descr[i%2], *newb = (struct starpu_block_interface *) descr[(i+1)%2];
  356. TYPE *old = (TYPE*) oldb->ptr, *newer = (TYPE*) newb->ptr;
  357. /* Shadow data */
  358. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  359. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  360. unsigned x, y, z;
  361. unsigned stepx = 1;
  362. unsigned stepy = 1;
  363. unsigned stepz = 1;
  364. unsigned idx = 0;
  365. unsigned idy = 0;
  366. unsigned idz = 0;
  367. TYPE *ptr = old;
  368. # include "shadow.h"
  369. /* And perform actual computation */
  370. #ifdef LIFE
  371. life_update(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  372. #else
  373. memcpy(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer));
  374. #endif /* LIFE */
  375. }
  376. if (block->bz == 0)
  377. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  378. }
  379. /* Performance model and codelet structure */
  380. static struct starpu_perfmodel cl_update_model = {
  381. .type = STARPU_HISTORY_BASED,
  382. .symbol = "cl_update"
  383. };
  384. struct starpu_codelet cl_update = {
  385. .where = 0 |
  386. #ifdef STARPU_USE_CUDA
  387. STARPU_CUDA|
  388. #endif
  389. #ifdef STARPU_USE_OPENCL
  390. STARPU_OPENCL|
  391. #endif
  392. STARPU_CPU,
  393. .cpu_funcs = {update_func_cpu, NULL},
  394. #ifdef STARPU_USE_CUDA
  395. .cuda_funcs = {update_func_cuda, NULL},
  396. #endif
  397. #ifdef STARPU_USE_OPENCL
  398. .opencl_funcs = {update_func_opencl, NULL},
  399. #endif
  400. .model = &cl_update_model,
  401. .nbuffers = 6
  402. };
  403. /*
  404. * Save the block internal boundaries to give them to our neighbours.
  405. */
  406. /* CPU version */
  407. static void load_subblock_into_buffer_cpu(void *_block,
  408. void *_boundary,
  409. unsigned firstz)
  410. {
  411. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  412. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  413. check_load(block, boundary);
  414. /* We do a contiguous memory transfer */
  415. size_t boundary_size = K*block->ldz*block->elemsize;
  416. unsigned offset = firstz*block->ldz;
  417. TYPE *block_data = (TYPE *)block->ptr;
  418. TYPE *boundary_data = (TYPE *)boundary->ptr;
  419. memcpy(boundary_data, &block_data[offset], boundary_size);
  420. }
  421. /* CUDA version */
  422. #ifdef STARPU_USE_CUDA
  423. static void load_subblock_into_buffer_cuda(void *_block,
  424. void *_boundary,
  425. unsigned firstz)
  426. {
  427. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  428. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  429. check_load(block, boundary);
  430. /* We do a contiguous memory transfer */
  431. size_t boundary_size = K*block->ldz*block->elemsize;
  432. unsigned offset = firstz*block->ldz;
  433. TYPE *block_data = (TYPE *)block->ptr;
  434. TYPE *boundary_data = (TYPE *)boundary->ptr;
  435. cudaMemcpyAsync(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  436. }
  437. #endif /* STARPU_USE_CUDA */
  438. /* OPENCL version */
  439. #ifdef STARPU_USE_OPENCL
  440. static void load_subblock_into_buffer_opencl(struct starpu_block_interface *block,
  441. struct starpu_block_interface *boundary,
  442. unsigned firstz)
  443. {
  444. check_load(block, boundary);
  445. /* We do a contiguous memory transfer */
  446. size_t boundary_size = K*block->ldz*block->elemsize;
  447. unsigned offset = firstz*block->ldz;
  448. cl_mem block_data = (cl_mem)block->ptr;
  449. cl_mem boundary_data = (cl_mem)boundary->ptr;
  450. cl_command_queue cq;
  451. starpu_opencl_get_current_queue(&cq);
  452. clEnqueueCopyBuffer(cq, block_data, boundary_data, offset, 0, boundary_size, 0, NULL, NULL);
  453. }
  454. #endif /* STARPU_USE_OPENCL */
  455. /* Record how many top/bottom saves each worker performed */
  456. unsigned top_per_worker[STARPU_NMAXWORKERS];
  457. unsigned bottom_per_worker[STARPU_NMAXWORKERS];
  458. /* top save, CPU version */
  459. static void dummy_func_top_cpu(void *descr[] __attribute__((unused)), void *arg)
  460. {
  461. struct block_description *block = (struct block_description *) arg;
  462. int workerid = starpu_worker_get_id();
  463. top_per_worker[workerid]++;
  464. DEBUG( "DO SAVE Bottom block %d\n", block->bz);
  465. /* The offset along the z axis is (block_size_z + K)- K */
  466. unsigned block_size_z = get_block_size(block->bz);
  467. load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
  468. load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
  469. }
  470. /* bottom save, CPU version */
  471. static void dummy_func_bottom_cpu(void *descr[] __attribute__((unused)), void *arg)
  472. {
  473. struct block_description *block = (struct block_description *) arg;
  474. int workerid = starpu_worker_get_id();
  475. bottom_per_worker[workerid]++;
  476. DEBUG( "DO SAVE Top block %d\n", block->bz);
  477. load_subblock_into_buffer_cpu(descr[0], descr[2], K);
  478. load_subblock_into_buffer_cpu(descr[1], descr[3], K);
  479. }
  480. /* top save, CUDA version */
  481. #ifdef STARPU_USE_CUDA
  482. static void dummy_func_top_cuda(void *descr[] __attribute__((unused)), void *arg)
  483. {
  484. struct block_description *block = (struct block_description *) arg;
  485. int workerid = starpu_worker_get_id();
  486. top_per_worker[workerid]++;
  487. DEBUG( "DO SAVE Top block %d\n", block->bz);
  488. /* The offset along the z axis is (block_size_z + K)- K */
  489. unsigned block_size_z = get_block_size(block->bz);
  490. load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
  491. load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
  492. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  493. }
  494. /* bottom save, CUDA version */
  495. static void dummy_func_bottom_cuda(void *descr[] __attribute__((unused)), void *arg)
  496. {
  497. struct block_description *block = (struct block_description *) arg;
  498. int workerid = starpu_worker_get_id();
  499. bottom_per_worker[workerid]++;
  500. DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);
  501. load_subblock_into_buffer_cuda(descr[0], descr[2], K);
  502. load_subblock_into_buffer_cuda(descr[1], descr[3], K);
  503. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  504. }
  505. #endif /* STARPU_USE_CUDA */
  506. /* top save, OpenCL version */
  507. #ifdef STARPU_USE_OPENCL
  508. static void dummy_func_top_opencl(void *descr[] __attribute__((unused)), void *arg)
  509. {
  510. struct block_description *block = (struct block_description *) arg;
  511. int workerid = starpu_worker_get_id();
  512. top_per_worker[workerid]++;
  513. DEBUG( "DO SAVE Top block %d\n", block->bz);
  514. /* The offset along the z axis is (block_size_z + K)- K */
  515. unsigned block_size_z = get_block_size(block->bz);
  516. load_subblock_into_buffer_opencl(descr[0], descr[2], block_size_z);
  517. load_subblock_into_buffer_opencl(descr[1], descr[3], block_size_z);
  518. cl_command_queue cq;
  519. starpu_opencl_get_current_queue(&cq);
  520. clFinish(cq);
  521. }
  522. /* bottom save, OPENCL version */
  523. static void dummy_func_bottom_opencl(void *descr[] __attribute__((unused)), void *arg)
  524. {
  525. struct block_description *block = (struct block_description *) arg;
  526. int workerid = starpu_worker_get_id();
  527. bottom_per_worker[workerid]++;
  528. DEBUG( "DO SAVE Bottom block %d on OPENCL\n", block->bz);
  529. load_subblock_into_buffer_opencl(descr[0], descr[2], K);
  530. load_subblock_into_buffer_opencl(descr[1], descr[3], K);
  531. cl_command_queue cq;
  532. starpu_opencl_get_current_queue(&cq);
  533. clFinish(cq);
  534. }
  535. #endif /* STARPU_USE_OPENCL */
  536. /* Performance models and codelet for save */
  537. static struct starpu_perfmodel save_cl_bottom_model = {
  538. .type = STARPU_HISTORY_BASED,
  539. .symbol = "save_cl_bottom"
  540. };
  541. static struct starpu_perfmodel save_cl_top_model = {
  542. .type = STARPU_HISTORY_BASED,
  543. .symbol = "save_cl_top"
  544. };
  545. struct starpu_codelet save_cl_bottom = {
  546. .where = 0 |
  547. #ifdef STARPU_USE_CUDA
  548. STARPU_CUDA|
  549. #endif
  550. #ifdef STARPU_USE_OPENCL
  551. STARPU_OPENCL|
  552. #endif
  553. STARPU_CPU,
  554. .cpu_funcs = {dummy_func_bottom_cpu, NULL},
  555. #ifdef STARPU_USE_CUDA
  556. .cuda_funcs = {dummy_func_bottom_cuda, NULL},
  557. #endif
  558. #ifdef STARPU_USE_OPENCL
  559. .opencl_funcs = {dummy_func_bottom_opencl, NULL},
  560. #endif
  561. .model = &save_cl_bottom_model,
  562. .nbuffers = 4
  563. };
  564. struct starpu_codelet save_cl_top = {
  565. .where = 0|
  566. #ifdef STARPU_USE_CUDA
  567. STARPU_CUDA|
  568. #endif
  569. #ifdef STARPU_USE_OPENCL
  570. STARPU_OPENCL|
  571. #endif
  572. STARPU_CPU,
  573. .cpu_funcs = {dummy_func_top_cpu, NULL},
  574. #ifdef STARPU_USE_CUDA
  575. .cuda_funcs = {dummy_func_top_cuda, NULL},
  576. #endif
  577. #ifdef STARPU_USE_OPENCL
  578. .opencl_funcs = {dummy_func_top_opencl, NULL},
  579. #endif
  580. .model = &save_cl_top_model,
  581. .nbuffers = 4
  582. };