stencil-kernels.c 24 KB

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