implicit-stencil-kernels.c 25 KB

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