stencil-kernels.c 24 KB

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