implicit-stencil-kernels.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "implicit-stencil.h"
  17. /* Computation Kernels */
  18. /*
  19. * There are three codeletets:
  20. *
  21. * - cl_update, which takes a block and the boundaries of its neighbours, loads
  22. * the boundaries into the block and perform some update loops:
  23. *
  24. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  25. * | ... |
  26. * | | +------------------+ +------------------+
  27. * | #N+1 | | #N+1 bottom copy====>#N+1 bottom copy |
  28. * +-------------+ +------------------+ +------------------+
  29. * | #N top copy | | #N top copy | | |
  30. * +-------------+ +------------------+ | |
  31. * | #N |
  32. * ...
  33. * | | +----------------+ +----------------------+
  34. * | | | #N bottom copy | | block #N bottom copy |
  35. * ^ +------------------+ +----------------+ +----------------------+
  36. * | | #N-1 top copy <====#N-1 top copy | | block #N-1 |
  37. * | +------------------+ +----------------+ | |
  38. * Z ...
  39. *
  40. * - save_cl_top, which take a block and its top boundary, and saves the top of
  41. * the block into the boundary (to be given as bottom of the neighbour above
  42. * this block).
  43. *
  44. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  45. * | ... |
  46. * | | +------------------+ +------------------+
  47. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  48. * +-------------+ +------------------+ +------------------+
  49. * | #N top copy | | #N top copy <==== |
  50. * +-------------+ +------------------+ |..................|
  51. * | #N |
  52. * ...
  53. * | | +----------------+ +----------------------+
  54. * | | | #N bottom copy | | block #N bottom copy |
  55. * ^ +------------------+ +----------------+ +----------------------+
  56. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  57. * | +------------------+ +----------------+ | |
  58. * Z ...
  59. *
  60. * - save_cl_bottom, same for the bottom
  61. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  62. * | ... |
  63. * | | +------------------+ +------------------+
  64. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  65. * +-------------+ +------------------+ +------------------+
  66. * | #N top copy | | #N top copy | | |
  67. * +-------------+ +------------------+ | |
  68. * | #N |
  69. * ...
  70. * |..................| +----------------+ +----------------------+
  71. * | ====>#N bottom copy | | block #N bottom copy |
  72. * ^ +------------------+ +----------------+ +----------------------+
  73. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  74. * | +------------------+ +----------------+ | |
  75. * Z ...
  76. *
  77. * The idea is that the computation buffers thus don't have to move, only their
  78. * boundaries are copied to buffers that do move (be it CPU/GPU, GPU/GPU or via
  79. * MPI)
  80. *
  81. * For each of the buffers above, there are two (0/1) buffers to make new/old switch costless.
  82. */
  83. #if 0
  84. # define DEBUG(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
  85. #else
  86. # define DEBUG(fmt, ...) (void) 0
  87. #endif
  88. /* Record which GPU ran which block, for nice pictures */
  89. int who_runs_what_len;
  90. int *who_runs_what;
  91. int *who_runs_what_index;
  92. double *last_tick;
  93. /* Achieved iterations */
  94. static int achieved_iter;
  95. /* Record how many updates each worker performed */
  96. unsigned update_per_worker[STARPU_NMAXWORKERS];
  97. static void record_who_runs_what(struct block_description *block)
  98. {
  99. double now, now2, diff, delta = get_ticks() * 1000;
  100. int workerid = starpu_worker_get_id_check();
  101. now = starpu_timing_now();
  102. now2 = now - start;
  103. diff = now2 - last_tick[block->bz];
  104. while (diff >= delta)
  105. {
  106. last_tick[block->bz] += delta;
  107. diff = now2 - last_tick[block->bz];
  108. if (who_runs_what_index[block->bz] < who_runs_what_len)
  109. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
  110. }
  111. if (who_runs_what_index[block->bz] < who_runs_what_len)
  112. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);
  113. }
  114. static void check_load(struct starpu_block_interface *block, struct starpu_block_interface *boundary)
  115. {
  116. /* Sanity checks */
  117. STARPU_ASSERT(block->nx == boundary->nx);
  118. STARPU_ASSERT(block->ny == boundary->ny);
  119. STARPU_ASSERT(boundary->nz == K);
  120. /* NB: this is not fully garanteed ... but it's *very* likely and that
  121. * makes our life much simpler */
  122. STARPU_ASSERT(block->ldy == boundary->ldy);
  123. STARPU_ASSERT(block->ldz == boundary->ldz);
  124. }
  125. /*
  126. * Load a neighbour's boundary into block, CPU version
  127. */
  128. static void load_subblock_from_buffer_cpu(void *_block,
  129. void *_boundary,
  130. unsigned firstz)
  131. {
  132. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  133. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  134. check_load(block, boundary);
  135. /* We do a contiguous memory transfer */
  136. size_t boundary_size = K*block->ldz*block->elemsize;
  137. unsigned offset = firstz*block->ldz;
  138. TYPE *block_data = (TYPE *)block->ptr;
  139. TYPE *boundary_data = (TYPE *)boundary->ptr;
  140. memcpy(&block_data[offset], boundary_data, boundary_size);
  141. }
  142. /*
  143. * Load a neighbour's boundary into block, CUDA version
  144. */
  145. #ifdef STARPU_USE_CUDA
  146. static void load_subblock_from_buffer_cuda(void *_block,
  147. void *_boundary,
  148. unsigned firstz)
  149. {
  150. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  151. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  152. check_load(block, boundary);
  153. /* We do a contiguous memory transfer */
  154. size_t boundary_size = K*block->ldz*block->elemsize;
  155. unsigned offset = firstz*block->ldz;
  156. TYPE *block_data = (TYPE *)block->ptr;
  157. TYPE *boundary_data = (TYPE *)boundary->ptr;
  158. cudaMemcpyAsync(&block_data[offset], boundary_data, boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  159. }
  160. /*
  161. * cl_update (CUDA version)
  162. */
  163. static void update_func_cuda(void *descr[], void *arg)
  164. {
  165. unsigned z;
  166. starpu_codelet_unpack_args(arg, &z);
  167. struct block_description *block = get_block_description(z);
  168. int workerid = starpu_worker_get_id_check();
  169. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  170. if (block->bz == 0)
  171. FPRINTF(stderr,"!!! DO update_func_cuda z %u CUDA%d !!!\n", block->bz, workerid);
  172. else
  173. DEBUG( "!!! DO update_func_cuda z %u CUDA%d !!!\n", block->bz, workerid);
  174. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  175. int rank = 0;
  176. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  177. DEBUG( "!!! RANK %d !!!\n", rank);
  178. #endif
  179. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  180. unsigned block_size_z = get_block_size(block->bz);
  181. unsigned i;
  182. update_per_worker[workerid]++;
  183. record_who_runs_what(block);
  184. /*
  185. * Load neighbours' boundaries : TOP
  186. */
  187. /* The offset along the z axis is (block_size_z + K) */
  188. load_subblock_from_buffer_cuda(descr[0], descr[2], block_size_z+K);
  189. load_subblock_from_buffer_cuda(descr[1], descr[3], block_size_z+K);
  190. /*
  191. * Load neighbours' boundaries : BOTTOM
  192. */
  193. load_subblock_from_buffer_cuda(descr[0], descr[4], 0);
  194. load_subblock_from_buffer_cuda(descr[1], descr[5], 0);
  195. /*
  196. * Stencils ... do the actual work here :) TODO
  197. */
  198. for (i=1; i<=K; i++)
  199. {
  200. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  201. TYPE *old = (void*) oldb->ptr, *newer = (void*) newb->ptr;
  202. /* Shadow data */
  203. cuda_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  204. /* And perform actual computation */
  205. #ifdef LIFE
  206. cuda_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  207. #else
  208. cudaMemcpyAsync(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  209. #endif /* LIFE */
  210. }
  211. }
  212. #endif /* STARPU_USE_CUDA */
  213. /*
  214. * Load a neighbour's boundary into block, OpenCL version
  215. */
  216. #ifdef STARPU_USE_OPENCL
  217. static void load_subblock_from_buffer_opencl(struct starpu_block_interface *block,
  218. struct starpu_block_interface *boundary,
  219. unsigned firstz)
  220. {
  221. check_load(block, boundary);
  222. /* We do a contiguous memory transfer */
  223. size_t boundary_size = K*block->ldz*block->elemsize;
  224. unsigned offset = firstz*block->ldz;
  225. cl_mem block_data = (cl_mem)block->dev_handle;
  226. cl_mem boundary_data = (cl_mem)boundary->dev_handle;
  227. cl_command_queue cq;
  228. starpu_opencl_get_current_queue(&cq);
  229. cl_int ret = clEnqueueCopyBuffer(cq, boundary_data, block_data, 0, offset, boundary_size, 0, NULL, NULL);
  230. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  231. }
  232. /*
  233. * cl_update (OpenCL version)
  234. */
  235. static void update_func_opencl(void *descr[], void *arg)
  236. {
  237. unsigned z;
  238. starpu_codelet_unpack_args(arg, &z);
  239. struct block_description *block = get_block_description(z);
  240. int workerid = starpu_worker_get_id_check();
  241. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  242. if (block->bz == 0)
  243. FPRINTF(stderr,"!!! DO update_func_opencl z %u OPENCL%d !!!\n", block->bz, workerid);
  244. else
  245. DEBUG( "!!! DO update_func_opencl z %u OPENCL%d !!!\n", block->bz, workerid);
  246. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  247. int rank = 0;
  248. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  249. DEBUG( "!!! RANK %d !!!\n", rank);
  250. #endif
  251. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  252. unsigned block_size_z = get_block_size(block->bz);
  253. unsigned i;
  254. update_per_worker[workerid]++;
  255. record_who_runs_what(block);
  256. cl_command_queue cq;
  257. starpu_opencl_get_current_queue(&cq);
  258. /*
  259. * Load neighbours' boundaries : TOP
  260. */
  261. /* The offset along the z axis is (block_size_z + K) */
  262. load_subblock_from_buffer_opencl(descr[0], descr[2], block_size_z+K);
  263. load_subblock_from_buffer_opencl(descr[1], descr[3], block_size_z+K);
  264. /*
  265. * Load neighbours' boundaries : BOTTOM
  266. */
  267. load_subblock_from_buffer_opencl(descr[0], descr[4], 0);
  268. load_subblock_from_buffer_opencl(descr[1], descr[5], 0);
  269. /*
  270. * Stencils ... do the actual work here :) TODO
  271. */
  272. for (i=1; i<=K; i++)
  273. {
  274. struct starpu_block_interface *oldb = descr[i%2], *newb = descr[(i+1)%2];
  275. TYPE *old = (void*) oldb->dev_handle, *newer = (void*) newb->dev_handle;
  276. /* Shadow data */
  277. opencl_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  278. /* And perform actual computation */
  279. #ifdef LIFE
  280. opencl_life_update_host(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  281. #else
  282. cl_event event;
  283. cl_int ret = clEnqueueCopyBuffer(cq, old, newer, 0, 0, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer), 0, NULL, &event);
  284. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  285. #endif /* LIFE */
  286. }
  287. }
  288. #endif /* STARPU_USE_OPENCL */
  289. /*
  290. * cl_update (CPU version)
  291. */
  292. void update_func_cpu(void *descr[], void *arg)
  293. {
  294. unsigned zz;
  295. starpu_codelet_unpack_args(arg, &zz);
  296. struct block_description *block = get_block_description(zz);
  297. int workerid = starpu_worker_get_id_check();
  298. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  299. if (block->bz == 0)
  300. DEBUG("!!! DO update_func_cpu z %u CPU%d !!!\n", block->bz, workerid);
  301. else
  302. DEBUG("!!! DO update_func_cpu z %u CPU%d !!!\n", block->bz, workerid);
  303. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  304. int rank = 0;
  305. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  306. DEBUG( "!!! RANK %d !!!\n", rank);
  307. #endif
  308. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  309. unsigned block_size_z = get_block_size(block->bz);
  310. unsigned i;
  311. update_per_worker[workerid]++;
  312. record_who_runs_what(block);
  313. /*
  314. * Load neighbours' boundaries : TOP
  315. */
  316. /* The offset along the z axis is (block_size_z + K) */
  317. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  318. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  319. /*
  320. * Load neighbours' boundaries : BOTTOM
  321. */
  322. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  323. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  324. /*
  325. * Stencils ... do the actual work here :) TODO
  326. */
  327. for (i=1; i<=K; i++)
  328. {
  329. struct starpu_block_interface *oldb = (struct starpu_block_interface *) descr[i%2], *newb = (struct starpu_block_interface *) descr[(i+1)%2];
  330. TYPE *old = (TYPE*) oldb->ptr, *newer = (TYPE*) newb->ptr;
  331. /* Shadow data */
  332. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  333. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  334. unsigned x, y, z;
  335. unsigned stepx = 1;
  336. unsigned stepy = 1;
  337. unsigned stepz = 1;
  338. unsigned idx = 0;
  339. unsigned idy = 0;
  340. unsigned idz = 0;
  341. TYPE *ptr = old;
  342. # include "shadow.h"
  343. /* And perform actual computation */
  344. #ifdef LIFE
  345. life_update(block->bz, old, newer, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  346. #else
  347. memcpy(newer, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*newer));
  348. #endif /* LIFE */
  349. }
  350. }
  351. /* Performance model and codelet structure */
  352. static struct starpu_perfmodel cl_update_model =
  353. {
  354. .type = STARPU_HISTORY_BASED,
  355. .symbol = "cl_update"
  356. };
  357. struct starpu_codelet cl_update =
  358. {
  359. .cpu_funcs = {update_func_cpu},
  360. #ifdef STARPU_USE_CUDA
  361. .cuda_funcs = {update_func_cuda},
  362. .cuda_flags = {STARPU_CUDA_ASYNC},
  363. #endif
  364. #ifdef STARPU_USE_OPENCL
  365. .opencl_funcs = {update_func_opencl},
  366. .opencl_flags = {STARPU_OPENCL_ASYNC},
  367. #endif
  368. .model = &cl_update_model,
  369. .nbuffers = 6,
  370. .modes = {STARPU_RW, STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  371. };
  372. /*
  373. * Save the block internal boundaries to give them to our neighbours.
  374. */
  375. /* CPU version */
  376. static void load_subblock_into_buffer_cpu(void *_block,
  377. void *_boundary,
  378. unsigned firstz)
  379. {
  380. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  381. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  382. check_load(block, boundary);
  383. /* We do a contiguous memory transfer */
  384. size_t boundary_size = K*block->ldz*block->elemsize;
  385. unsigned offset = firstz*block->ldz;
  386. TYPE *block_data = (TYPE *)block->ptr;
  387. TYPE *boundary_data = (TYPE *)boundary->ptr;
  388. memcpy(boundary_data, &block_data[offset], boundary_size);
  389. }
  390. /* CUDA version */
  391. #ifdef STARPU_USE_CUDA
  392. static void load_subblock_into_buffer_cuda(void *_block,
  393. void *_boundary,
  394. unsigned firstz)
  395. {
  396. struct starpu_block_interface *block = (struct starpu_block_interface *)_block;
  397. struct starpu_block_interface *boundary = (struct starpu_block_interface *)_boundary;
  398. check_load(block, boundary);
  399. /* We do a contiguous memory transfer */
  400. size_t boundary_size = K*block->ldz*block->elemsize;
  401. unsigned offset = firstz*block->ldz;
  402. TYPE *block_data = (TYPE *)block->ptr;
  403. TYPE *boundary_data = (TYPE *)boundary->ptr;
  404. cudaMemcpyAsync(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  405. }
  406. #endif /* STARPU_USE_CUDA */
  407. /* OPENCL version */
  408. #ifdef STARPU_USE_OPENCL
  409. static void load_subblock_into_buffer_opencl(struct starpu_block_interface *block,
  410. struct starpu_block_interface *boundary,
  411. unsigned firstz)
  412. {
  413. check_load(block, boundary);
  414. /* We do a contiguous memory transfer */
  415. size_t boundary_size = K*block->ldz*block->elemsize;
  416. unsigned offset = firstz*block->ldz;
  417. cl_mem block_data = (cl_mem)block->dev_handle;
  418. cl_mem boundary_data = (cl_mem)boundary->dev_handle;
  419. cl_command_queue cq;
  420. starpu_opencl_get_current_queue(&cq);
  421. cl_int ret = clEnqueueCopyBuffer(cq, block_data, boundary_data, offset, 0, boundary_size, 0, NULL, NULL);
  422. if (ret != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(ret);
  423. }
  424. #endif /* STARPU_USE_OPENCL */
  425. /* Record how many top/bottom saves each worker performed */
  426. unsigned top_per_worker[STARPU_NMAXWORKERS];
  427. unsigned bottom_per_worker[STARPU_NMAXWORKERS];
  428. /* top save, CPU version */
  429. void dummy_func_top_cpu(void *descr[], void *arg)
  430. {
  431. unsigned z;
  432. starpu_codelet_unpack_args(arg, &z);
  433. struct block_description *block = get_block_description(z);
  434. int workerid = starpu_worker_get_id_check();
  435. top_per_worker[workerid]++;
  436. DEBUG( "DO SAVE Bottom block %d\n", block->bz);
  437. /* The offset along the z axis is (block_size_z + K)- K */
  438. unsigned block_size_z = get_block_size(block->bz);
  439. load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
  440. load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
  441. }
  442. /* bottom save, CPU version */
  443. void dummy_func_bottom_cpu(void *descr[], void *arg)
  444. {
  445. unsigned z;
  446. starpu_codelet_unpack_args(arg, &z);
  447. struct block_description *block = get_block_description(z);
  448. STARPU_ASSERT(block);
  449. int workerid = starpu_worker_get_id_check();
  450. bottom_per_worker[workerid]++;
  451. DEBUG( "DO SAVE Top block %d\n", block->bz);
  452. load_subblock_into_buffer_cpu(descr[0], descr[2], K);
  453. load_subblock_into_buffer_cpu(descr[1], descr[3], K);
  454. }
  455. /* top save, CUDA version */
  456. #ifdef STARPU_USE_CUDA
  457. static void dummy_func_top_cuda(void *descr[], void *arg)
  458. {
  459. unsigned z;
  460. starpu_codelet_unpack_args(arg, &z);
  461. struct block_description *block = get_block_description(z);
  462. int workerid = starpu_worker_get_id_check();
  463. top_per_worker[workerid]++;
  464. DEBUG( "DO SAVE Top block %d\n", block->bz);
  465. /* The offset along the z axis is (block_size_z + K)- K */
  466. unsigned block_size_z = get_block_size(block->bz);
  467. load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
  468. load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
  469. }
  470. /* bottom save, CUDA version */
  471. static void dummy_func_bottom_cuda(void *descr[], void *arg)
  472. {
  473. unsigned z;
  474. starpu_codelet_unpack_args(arg, &z);
  475. struct block_description *block = get_block_description(z);
  476. (void) block;
  477. int workerid = starpu_worker_get_id_check();
  478. bottom_per_worker[workerid]++;
  479. DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);
  480. load_subblock_into_buffer_cuda(descr[0], descr[2], K);
  481. load_subblock_into_buffer_cuda(descr[1], descr[3], K);
  482. }
  483. #endif /* STARPU_USE_CUDA */
  484. /* top save, OpenCL version */
  485. #ifdef STARPU_USE_OPENCL
  486. static void dummy_func_top_opencl(void *descr[], void *arg)
  487. {
  488. unsigned z;
  489. starpu_codelet_unpack_args(arg, &z);
  490. struct block_description *block = get_block_description(z);
  491. int workerid = starpu_worker_get_id_check();
  492. top_per_worker[workerid]++;
  493. DEBUG( "DO SAVE Top block %d\n", block->bz);
  494. /* The offset along the z axis is (block_size_z + K)- K */
  495. unsigned block_size_z = get_block_size(block->bz);
  496. load_subblock_into_buffer_opencl(descr[0], descr[2], block_size_z);
  497. load_subblock_into_buffer_opencl(descr[1], descr[3], block_size_z);
  498. }
  499. /* bottom save, OPENCL version */
  500. static void dummy_func_bottom_opencl(void *descr[], void *arg)
  501. {
  502. unsigned z;
  503. starpu_codelet_unpack_args(arg, &z);
  504. struct block_description *block = get_block_description(z);
  505. (void) block;
  506. int workerid = starpu_worker_get_id_check();
  507. bottom_per_worker[workerid]++;
  508. DEBUG( "DO SAVE Bottom block %d on OPENCL\n", block->bz);
  509. load_subblock_into_buffer_opencl(descr[0], descr[2], K);
  510. load_subblock_into_buffer_opencl(descr[1], descr[3], K);
  511. }
  512. #endif /* STARPU_USE_OPENCL */
  513. /* Performance models and codelet for save */
  514. static struct starpu_perfmodel save_cl_bottom_model =
  515. {
  516. .type = STARPU_HISTORY_BASED,
  517. .symbol = "save_cl_bottom"
  518. };
  519. static struct starpu_perfmodel save_cl_top_model =
  520. {
  521. .type = STARPU_HISTORY_BASED,
  522. .symbol = "save_cl_top"
  523. };
  524. struct starpu_codelet save_cl_bottom =
  525. {
  526. .cpu_funcs = {dummy_func_bottom_cpu},
  527. #ifdef STARPU_USE_CUDA
  528. .cuda_funcs = {dummy_func_bottom_cuda},
  529. .cuda_flags = {STARPU_CUDA_ASYNC},
  530. #endif
  531. #ifdef STARPU_USE_OPENCL
  532. .opencl_funcs = {dummy_func_bottom_opencl},
  533. .opencl_flags = {STARPU_OPENCL_ASYNC},
  534. #endif
  535. .model = &save_cl_bottom_model,
  536. .nbuffers = 4,
  537. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  538. };
  539. struct starpu_codelet save_cl_top =
  540. {
  541. .cpu_funcs = {dummy_func_top_cpu},
  542. #ifdef STARPU_USE_CUDA
  543. .cuda_funcs = {dummy_func_top_cuda},
  544. .cuda_flags = {STARPU_CUDA_ASYNC},
  545. #endif
  546. #ifdef STARPU_USE_OPENCL
  547. .opencl_funcs = {dummy_func_top_opencl},
  548. .opencl_flags = {STARPU_OPENCL_ASYNC},
  549. #endif
  550. .model = &save_cl_top_model,
  551. .nbuffers = 4,
  552. .modes = {STARPU_R, STARPU_R, STARPU_W, STARPU_W}
  553. };
  554. /* Memset a block's buffers */
  555. static void memset_func(void *descr[], void *arg)
  556. {
  557. (void)descr;
  558. unsigned sizex, sizey, bz;
  559. starpu_codelet_unpack_args(arg, &sizex, &sizey, &bz);
  560. struct block_description *block = get_block_description(bz);
  561. unsigned size_bz = get_block_size(bz);
  562. unsigned x,y,z;
  563. for (x = 0; x < sizex + 2*K; x++)
  564. {
  565. for (y = 0; y < sizey + 2*K; y++)
  566. {
  567. /* Main blocks */
  568. for (z = 0; z < size_bz + 2*K; z++)
  569. {
  570. block->layers[0][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  571. block->layers[1][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  572. }
  573. for (z = 0; z < K; z++)
  574. {
  575. /* Boundary blocks : Top */
  576. block->boundaries[T][0][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  577. block->boundaries[T][1][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  578. /* Boundary blocks : Bottom */
  579. block->boundaries[B][0][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  580. block->boundaries[B][1][(x)+(y)*(sizex + 2*K)+(z)*(sizex+2*K)*(sizey+2*K)] = 0;
  581. }
  582. }
  583. }
  584. //memset(block->layers[0], 0, (sizex + 2*K)*(sizey + 2*K)*(size_bz + 2*K)*sizeof(block->layers[0]));
  585. //memset(block->layers[1], 0, (sizex + 2*K)*(sizey + 2*K)*(size_bz + 2*K)*sizeof(block->layers[1]));
  586. //memset(block->boundaries[T][0], 0, (sizex + 2*K)*(sizey + 2*K)*K*sizeof(block->boundaries[T][0]));
  587. //memset(block->boundaries[T][1], 0, (sizex + 2*K)*(sizey + 2*K)*K*sizeof(block->boundaries[T][1]));
  588. //memset(block->boundaries[B][0], 0, (sizex + 2*K)*(sizey + 2*K)*K*sizeof(block->boundaries[B][0]));
  589. //memset(block->boundaries[B][1], 0, (sizex + 2*K)*(sizey + 2*K)*K*sizeof(block->boundaries[B][1]));
  590. }
  591. static double memset_cost_function(struct starpu_task *task, unsigned nimpl)
  592. {
  593. (void) task;
  594. (void) nimpl;
  595. return 0.000001;
  596. }
  597. static struct starpu_perfmodel memset_model =
  598. {
  599. .type = STARPU_COMMON,
  600. .cost_function = memset_cost_function,
  601. .symbol = "memset"
  602. };
  603. struct starpu_codelet cl_memset =
  604. {
  605. .cpu_funcs = {memset_func},
  606. .model = &memset_model,
  607. .nbuffers = 6,
  608. .modes = {STARPU_W, STARPU_W, STARPU_W, STARPU_W, STARPU_W, STARPU_W}
  609. };
  610. /* Initialize a block's layer */
  611. static void initlayer_func(void *descr[], void *arg)
  612. {
  613. (void)descr;
  614. unsigned sizex, sizey, bz;
  615. starpu_codelet_unpack_args(arg, &sizex, &sizey, &bz);
  616. struct block_description *block = get_block_description(bz);
  617. unsigned size_bz = get_block_size(bz);
  618. /* Initialize layer with some random data */
  619. unsigned x, y, z;
  620. unsigned sum = 0;
  621. for (x = 0; x < sizex; x++)
  622. for (y = 0; y < sizey; y++)
  623. for (z = 0; z < size_bz; z++)
  624. sum += block->layers[0][(K+x)+(K+y)*(sizex + 2*K)+(K+z)*(sizex+2*K)*(sizey+2*K)] = (int)((x/7.+y/13.+(bz*size_bz + z)/17.) * 10.) % 2;
  625. }
  626. static double initlayer_cost_function(struct starpu_task *task, unsigned nimpl)
  627. {
  628. (void) task;
  629. (void) nimpl;
  630. return 0.000001;
  631. }
  632. static struct starpu_perfmodel initlayer_model =
  633. {
  634. .type = STARPU_COMMON,
  635. .cost_function = initlayer_cost_function,
  636. .symbol = "initlayer"
  637. };
  638. struct starpu_codelet cl_initlayer =
  639. {
  640. .cpu_funcs = {initlayer_func},
  641. .model = &initlayer_model,
  642. .nbuffers = 1,
  643. .modes = {STARPU_W}
  644. };