stencil-kernels.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2012, 2013 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. cl_int ret = clEnqueueCopyBuffer(cq, boundary_data, block_data, 0, offset, boundary_size, 0, NULL, &event);
  263. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  264. clWaitForEvents(1, &event);
  265. clReleaseEvent(event);
  266. }
  267. /*
  268. * cl_update (OpenCL version)
  269. */
  270. static void update_func_opencl(void *descr[], void *arg)
  271. {
  272. struct block_description *block = arg;
  273. int workerid = starpu_worker_get_id();
  274. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  275. if (block->bz == 0)
  276. fprintf(stderr,"!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  277. else
  278. DEBUG( "!!! DO update_func_opencl z %d OPENCL%d !!!\n", block->bz, workerid);
  279. #ifdef STARPU_USE_MPI
  280. int rank = 0;
  281. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  282. DEBUG( "!!! RANK %d !!!\n", rank);
  283. #endif
  284. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  285. unsigned block_size_z = get_block_size(block->bz);
  286. unsigned i;
  287. update_per_worker[workerid]++;
  288. record_who_runs_what(block);
  289. cl_command_queue cq;
  290. starpu_opencl_get_current_queue(&cq);
  291. /*
  292. * Load neighbours' boundaries : TOP
  293. */
  294. /* The offset along the z axis is (block_size_z + K) */
  295. load_subblock_from_buffer_opencl(descr[0], descr[2], block_size_z+K);
  296. load_subblock_from_buffer_opencl(descr[1], descr[3], block_size_z+K);
  297. /*
  298. * Load neighbours' boundaries : BOTTOM
  299. */
  300. load_subblock_from_buffer_opencl(descr[0], descr[4], 0);
  301. load_subblock_from_buffer_opencl(descr[1], descr[5], 0);
  302. /*
  303. * Stencils ... do the actual work here :) TODO
  304. */
  305. for (i=1; i<=K; i++)
  306. {
  307. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  308. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  309. /* Shadow data */
  310. opencl_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  311. /* And perform actual computation */
  312. #ifdef LIFE
  313. opencl_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  314. #else
  315. cl_event event;
  316. clEnqueueCopyBuffer(cq, old, newer, 0, 0, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), 0, NULL, &event);
  317. clWaitForEvents(1, &event);
  318. clReleaseEvent(event);
  319. #endif /* LIFE */
  320. }
  321. cl_int err;
  322. if ((err = clFinish(cq)))
  323. STARPU_OPENCL_REPORT_ERROR(err);
  324. if (block->bz == 0)
  325. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  326. }
  327. #endif /* STARPU_USE_OPENCL */
  328. /*
  329. * cl_update (CPU version)
  330. */
  331. void update_func_cpu(void *descr[], void *arg)
  332. {
  333. struct block_description *block = (struct block_description *) arg;
  334. int workerid = starpu_worker_get_id();
  335. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  336. if (block->bz == 0)
  337. FPRINTF(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  338. else
  339. DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  340. #ifdef STARPU_USE_MPI
  341. int rank = 0;
  342. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  343. DEBUG( "!!! RANK %d !!!\n", rank);
  344. #endif
  345. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  346. unsigned block_size_z = get_block_size(block->bz);
  347. unsigned i;
  348. update_per_worker[workerid]++;
  349. record_who_runs_what(block);
  350. /*
  351. * Load neighbours' boundaries : TOP
  352. */
  353. /* The offset along the z axis is (block_size_z + K) */
  354. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  355. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  356. /*
  357. * Load neighbours' boundaries : BOTTOM
  358. */
  359. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  360. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  361. /*
  362. * Stencils ... do the actual work here :) TODO
  363. */
  364. for (i=1; i<=K; i++)
  365. {
  366. struct starpu_block_interface *oldb = (struct starpu_block_interface *) descr[i%2], *newb = (struct starpu_block_interface *) descr[(i+1)%2];
  367. TYPE *old = (TYPE*) oldb->ptr, *newer = (TYPE*) newb->ptr;
  368. /* Shadow data */
  369. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  370. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  371. unsigned x, y, z;
  372. unsigned stepx = 1;
  373. unsigned stepy = 1;
  374. unsigned stepz = 1;
  375. unsigned idx = 0;
  376. unsigned idy = 0;
  377. unsigned idz = 0;
  378. TYPE *ptr = old;
  379. # include "shadow.h"
  380. /* And perform actual computation */
  381. #ifdef LIFE
  382. life_update(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  383. #else
  384. memcpy(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer));
  385. #endif /* LIFE */
  386. }
  387. if (block->bz == 0)
  388. starpu_top_update_data_integer(starpu_top_achieved_loop, ++achieved_iter);
  389. }
  390. /* Performance model and codelet structure */
  391. static struct starpu_perfmodel cl_update_model =
  392. {
  393. .type = STARPU_HISTORY_BASED,
  394. .symbol = "cl_update"
  395. };
  396. struct starpu_codelet cl_update =
  397. {
  398. .cpu_funcs = {update_func_cpu, NULL},
  399. .cpu_funcs_name = {"update_func_cpu", NULL},
  400. #ifdef STARPU_USE_CUDA
  401. .cuda_funcs = {update_func_cuda, NULL},
  402. #endif
  403. #ifdef STARPU_USE_OPENCL
  404. .opencl_funcs = {update_func_opencl, NULL},
  405. #endif
  406. .model = &cl_update_model,
  407. .nbuffers = 6,
  408. .modes = {STARPU_RW, STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  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. cl_event event;
  460. clEnqueueCopyBuffer(cq, block_data, boundary_data, offset, 0, boundary_size, 0, NULL, &event);
  461. clWaitForEvents(1, &event);
  462. clReleaseEvent(event);
  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. void dummy_func_top_cpu(void *descr[] STARPU_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. void dummy_func_bottom_cpu(void *descr[] STARPU_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[] STARPU_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[] STARPU_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[] STARPU_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[] STARPU_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. .cpu_funcs = {dummy_func_bottom_cpu, NULL},
  560. .cpu_funcs_name = {"dummy_func_bottom_cpu", NULL},
  561. #ifdef STARPU_USE_CUDA
  562. .cuda_funcs = {dummy_func_bottom_cuda, NULL},
  563. #endif
  564. #ifdef STARPU_USE_OPENCL
  565. .opencl_funcs = {dummy_func_bottom_opencl, NULL},
  566. #endif
  567. .model = &save_cl_bottom_model,
  568. .nbuffers = 4,
  569. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  570. };
  571. struct starpu_codelet save_cl_top =
  572. {
  573. .cpu_funcs = {dummy_func_top_cpu, NULL},
  574. .cpu_funcs_name = {"dummy_func_top_cpu", NULL},
  575. #ifdef STARPU_USE_CUDA
  576. .cuda_funcs = {dummy_func_top_cuda, NULL},
  577. #endif
  578. #ifdef STARPU_USE_OPENCL
  579. .opencl_funcs = {dummy_func_top_opencl, NULL},
  580. #endif
  581. .model = &save_cl_top_model,
  582. .nbuffers = 4,
  583. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  584. };