stencil-kernels.c 24 KB

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