stencil-kernels.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2012 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->ptr;
  257. cl_mem boundary_data = (cl_mem)boundary->ptr;
  258. cl_event event;
  259. cl_command_queue cq;
  260. starpu_opencl_get_current_queue(&cq);
  261. clEnqueueCopyBuffer(cq, boundary_data, block_data, 0, offset, boundary_size, 0, NULL, &event);
  262. clWaitForEvents(1, &event);
  263. clReleaseEvent(event);
  264. }
  265. /*
  266. * cl_update (OpenCL version)
  267. */
  268. static void update_func_opencl(void *descr[], void *arg)
  269. {
  270. struct block_description *block = arg;
  271. int workerid = starpu_worker_get_id();
  272. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  273. if (block->bz == 0)
  274. fprintf(stderr,"!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  275. else
  276. DEBUG( "!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  277. #ifdef STARPU_USE_MPI
  278. int rank = 0;
  279. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  280. DEBUG( "!!! RANK %d !!!\n", rank);
  281. #endif
  282. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  283. unsigned block_size_z = get_block_size(block->bz);
  284. unsigned i;
  285. update_per_worker[workerid]++;
  286. record_who_runs_what(block);
  287. cl_command_queue cq;
  288. starpu_opencl_get_current_queue(&cq);
  289. /*
  290. * Load neighbours' boundaries : TOP
  291. */
  292. /* The offset along the z axis is (block_size_z + K) */
  293. load_subblock_from_buffer_opencl(descr[0], descr[2], block_size_z+K);
  294. load_subblock_from_buffer_opencl(descr[1], descr[3], block_size_z+K);
  295. /*
  296. * Load neighbours' boundaries : BOTTOM
  297. */
  298. load_subblock_from_buffer_opencl(descr[0], descr[4], 0);
  299. load_subblock_from_buffer_opencl(descr[1], descr[5], 0);
  300. /*
  301. * Stencils ... do the actual work here :) TODO
  302. */
  303. for (i=1; i<=K; i++)
  304. {
  305. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  306. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  307. /* Shadow data */
  308. opencl_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  309. /* And perform actual computation */
  310. #ifdef LIFE
  311. opencl_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  312. #else
  313. cl_event event;
  314. clEnqueueCopyBuffer(cq, old, newer, 0, 0, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), 0, NULL, &event);
  315. clWaitForEvents(1, &event);
  316. clReleaseEvent(event);
  317. #endif /* LIFE */
  318. }
  319. cl_int err;
  320. if ((err = clFinish(cq)))
  321. STARPU_OPENCL_REPORT_ERROR(err);
  322. if (block->bz == 0)
  323. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  324. }
  325. #endif /* STARPU_USE_OPENCL */
  326. /*
  327. * cl_update (CPU version)
  328. */
  329. void update_func_cpu(void *descr[], void *arg)
  330. {
  331. struct block_description *block = (struct block_description *) arg;
  332. int workerid = starpu_worker_get_id();
  333. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  334. if (block->bz == 0)
  335. fprintf(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  336. else
  337. DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  338. #ifdef STARPU_USE_MPI
  339. int rank = 0;
  340. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  341. DEBUG( "!!! RANK %d !!!\n", rank);
  342. #endif
  343. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  344. unsigned block_size_z = get_block_size(block->bz);
  345. unsigned i;
  346. update_per_worker[workerid]++;
  347. record_who_runs_what(block);
  348. /*
  349. * Load neighbours' boundaries : TOP
  350. */
  351. /* The offset along the z axis is (block_size_z + K) */
  352. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  353. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  354. /*
  355. * Load neighbours' boundaries : BOTTOM
  356. */
  357. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  358. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  359. /*
  360. * Stencils ... do the actual work here :) TODO
  361. */
  362. for (i=1; i<=K; i++)
  363. {
  364. struct starpu_block_interface *oldb = (struct starpu_block_interface *) descr[i%2], *newb = (struct starpu_block_interface *) descr[(i+1)%2];
  365. TYPE *old = (TYPE*) oldb->ptr, *newer = (TYPE*) newb->ptr;
  366. /* Shadow data */
  367. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  368. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  369. unsigned x, y, z;
  370. unsigned stepx = 1;
  371. unsigned stepy = 1;
  372. unsigned stepz = 1;
  373. unsigned idx = 0;
  374. unsigned idy = 0;
  375. unsigned idz = 0;
  376. TYPE *ptr = old;
  377. # include "shadow.h"
  378. /* And perform actual computation */
  379. #ifdef LIFE
  380. life_update(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  381. #else
  382. memcpy(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer));
  383. #endif /* LIFE */
  384. }
  385. if (block->bz == 0)
  386. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  387. }
  388. /* Performance model and codelet structure */
  389. static struct starpu_perfmodel cl_update_model =
  390. {
  391. .type = STARPU_HISTORY_BASED,
  392. .symbol = "cl_update"
  393. };
  394. struct starpu_codelet cl_update =
  395. {
  396. .cpu_funcs = {update_func_cpu, NULL},
  397. .cpu_funcs_name = {"update_func_cpu", NULL},
  398. #ifdef STARPU_USE_CUDA
  399. .cuda_funcs = {update_func_cuda, NULL},
  400. #endif
  401. #ifdef STARPU_USE_OPENCL
  402. .opencl_funcs = {update_func_opencl, NULL},
  403. #endif
  404. .model = &cl_update_model,
  405. .nbuffers = 6,
  406. .modes = {STARPU_RW, STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  407. };
  408. /*
  409. * Save the block internal boundaries to give them to our neighbours.
  410. */
  411. /* CPU version */
  412. static void load_subblock_into_buffer_cpu(void *_block,
  413. void *_boundary,
  414. unsigned firstz)
  415. {
  416. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  417. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  418. check_load(block, boundary);
  419. /* We do a contiguous memory transfer */
  420. size_t boundary_size = K*block->ldz*block->elemsize;
  421. unsigned offset = firstz*block->ldz;
  422. TYPE *block_data = (TYPE *)block->ptr;
  423. TYPE *boundary_data = (TYPE *)boundary->ptr;
  424. memcpy(boundary_data, &block_data[offset], boundary_size);
  425. }
  426. /* CUDA version */
  427. #ifdef STARPU_USE_CUDA
  428. static void load_subblock_into_buffer_cuda(void *_block,
  429. void *_boundary,
  430. unsigned firstz)
  431. {
  432. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  433. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  434. check_load(block, boundary);
  435. /* We do a contiguous memory transfer */
  436. size_t boundary_size = K*block->ldz*block->elemsize;
  437. unsigned offset = firstz*block->ldz;
  438. TYPE *block_data = (TYPE *)block->ptr;
  439. TYPE *boundary_data = (TYPE *)boundary->ptr;
  440. cudaMemcpyAsync(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  441. }
  442. #endif /* STARPU_USE_CUDA */
  443. /* OPENCL version */
  444. #ifdef STARPU_USE_OPENCL
  445. static void load_subblock_into_buffer_opencl(struct starpu_block_interface *block,
  446. struct starpu_block_interface *boundary,
  447. unsigned firstz)
  448. {
  449. check_load(block, boundary);
  450. /* We do a contiguous memory transfer */
  451. size_t boundary_size = K*block->ldz*block->elemsize;
  452. unsigned offset = firstz*block->ldz;
  453. cl_mem block_data = (cl_mem)block->ptr;
  454. cl_mem boundary_data = (cl_mem)boundary->ptr;
  455. cl_command_queue cq;
  456. starpu_opencl_get_current_queue(&cq);
  457. cl_event event;
  458. clEnqueueCopyBuffer(cq, block_data, boundary_data, offset, 0, boundary_size, 0, NULL, &event);
  459. clWaitForEvents(1, &event);
  460. clReleaseEvent(event);
  461. }
  462. #endif /* STARPU_USE_OPENCL */
  463. /* Record how many top/bottom saves each worker performed */
  464. unsigned top_per_worker[STARPU_NMAXWORKERS];
  465. unsigned bottom_per_worker[STARPU_NMAXWORKERS];
  466. /* top save, CPU version */
  467. void dummy_func_top_cpu(void *descr[] __attribute__((unused)), void *arg)
  468. {
  469. struct block_description *block = (struct block_description *) arg;
  470. int workerid = starpu_worker_get_id();
  471. top_per_worker[workerid]++;
  472. DEBUG( "DO SAVE Bottom block %d\n", block->bz);
  473. /* The offset along the z axis is (block_size_z + K)- K */
  474. unsigned block_size_z = get_block_size(block->bz);
  475. load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
  476. load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
  477. }
  478. /* bottom save, CPU version */
  479. void dummy_func_bottom_cpu(void *descr[] __attribute__((unused)), void *arg)
  480. {
  481. struct block_description *block = (struct block_description *) arg;
  482. int workerid = starpu_worker_get_id();
  483. bottom_per_worker[workerid]++;
  484. DEBUG( "DO SAVE Top block %d\n", block->bz);
  485. load_subblock_into_buffer_cpu(descr[0], descr[2], K);
  486. load_subblock_into_buffer_cpu(descr[1], descr[3], K);
  487. }
  488. /* top save, CUDA version */
  489. #ifdef STARPU_USE_CUDA
  490. static void dummy_func_top_cuda(void *descr[] __attribute__((unused)), void *arg)
  491. {
  492. struct block_description *block = (struct block_description *) arg;
  493. int workerid = starpu_worker_get_id();
  494. top_per_worker[workerid]++;
  495. DEBUG( "DO SAVE Top block %d\n", block->bz);
  496. /* The offset along the z axis is (block_size_z + K)- K */
  497. unsigned block_size_z = get_block_size(block->bz);
  498. load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
  499. load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
  500. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  501. }
  502. /* bottom save, CUDA version */
  503. static void dummy_func_bottom_cuda(void *descr[] __attribute__((unused)), void *arg)
  504. {
  505. struct block_description *block = (struct block_description *) arg;
  506. int workerid = starpu_worker_get_id();
  507. bottom_per_worker[workerid]++;
  508. DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);
  509. load_subblock_into_buffer_cuda(descr[0], descr[2], K);
  510. load_subblock_into_buffer_cuda(descr[1], descr[3], K);
  511. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  512. }
  513. #endif /* STARPU_USE_CUDA */
  514. /* top save, OpenCL version */
  515. #ifdef STARPU_USE_OPENCL
  516. static void dummy_func_top_opencl(void *descr[] __attribute__((unused)), void *arg)
  517. {
  518. struct block_description *block = (struct block_description *) arg;
  519. int workerid = starpu_worker_get_id();
  520. top_per_worker[workerid]++;
  521. DEBUG( "DO SAVE Top block %d\n", block->bz);
  522. /* The offset along the z axis is (block_size_z + K)- K */
  523. unsigned block_size_z = get_block_size(block->bz);
  524. load_subblock_into_buffer_opencl(descr[0], descr[2], block_size_z);
  525. load_subblock_into_buffer_opencl(descr[1], descr[3], block_size_z);
  526. cl_command_queue cq;
  527. starpu_opencl_get_current_queue(&cq);
  528. clFinish(cq);
  529. }
  530. /* bottom save, OPENCL version */
  531. static void dummy_func_bottom_opencl(void *descr[] __attribute__((unused)), void *arg)
  532. {
  533. struct block_description *block = (struct block_description *) arg;
  534. int workerid = starpu_worker_get_id();
  535. bottom_per_worker[workerid]++;
  536. DEBUG( "DO SAVE Bottom block %d on OPENCL\n", block->bz);
  537. load_subblock_into_buffer_opencl(descr[0], descr[2], K);
  538. load_subblock_into_buffer_opencl(descr[1], descr[3], K);
  539. cl_command_queue cq;
  540. starpu_opencl_get_current_queue(&cq);
  541. clFinish(cq);
  542. }
  543. #endif /* STARPU_USE_OPENCL */
  544. /* Performance models and codelet for save */
  545. static struct starpu_perfmodel save_cl_bottom_model =
  546. {
  547. .type = STARPU_HISTORY_BASED,
  548. .symbol = "save_cl_bottom"
  549. };
  550. static struct starpu_perfmodel save_cl_top_model =
  551. {
  552. .type = STARPU_HISTORY_BASED,
  553. .symbol = "save_cl_top"
  554. };
  555. struct starpu_codelet save_cl_bottom =
  556. {
  557. .cpu_funcs = {dummy_func_bottom_cpu, NULL},
  558. .cpu_funcs_name = {"dummy_func_bottom_cpu", NULL},
  559. #ifdef STARPU_USE_CUDA
  560. .cuda_funcs = {dummy_func_bottom_cuda, NULL},
  561. #endif
  562. #ifdef STARPU_USE_OPENCL
  563. .opencl_funcs = {dummy_func_bottom_opencl, NULL},
  564. #endif
  565. .model = &save_cl_bottom_model,
  566. .nbuffers = 4,
  567. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  568. };
  569. struct starpu_codelet save_cl_top =
  570. {
  571. .cpu_funcs = {dummy_func_top_cpu, NULL},
  572. .cpu_funcs_name = {"dummy_func_top_cpu", NULL},
  573. #ifdef STARPU_USE_CUDA
  574. .cuda_funcs = {dummy_func_top_cuda, NULL},
  575. #endif
  576. #ifdef STARPU_USE_OPENCL
  577. .opencl_funcs = {dummy_func_top_opencl, NULL},
  578. #endif
  579. .model = &save_cl_top_model,
  580. .nbuffers = 4,
  581. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  582. };