stencil-kernels.c 23 KB

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