stencil-kernels.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2009-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 "stencil.h"
  17. #include <sys/time.h>
  18. /* Computation Kernels */
  19. /*
  20. * There are three codeletets:
  21. *
  22. * - cl_update, which takes a block and the boundaries of its neighbours, loads
  23. * the boundaries into the block and perform some update loops:
  24. *
  25. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  26. * | ... |
  27. * | | +------------------+ +------------------+
  28. * | #N+1 | | #N+1 bottom copy====>#N+1 bottom copy |
  29. * +-------------+ +------------------+ +------------------+
  30. * | #N top copy | | #N top copy | | |
  31. * +-------------+ +------------------+ | |
  32. * | #N |
  33. * ...
  34. * | | +----------------+ +----------------------+
  35. * | | | #N bottom copy | | block #N bottom copy |
  36. * ^ +------------------+ +----------------+ +----------------------+
  37. * | | #N-1 top copy <====#N-1 top copy | | block #N-1 |
  38. * | +------------------+ +----------------+ | |
  39. * Z ...
  40. *
  41. * - save_cl_top, which take a block and its top boundary, and saves the top of
  42. * the block into the boundary (to be given as bottom of the neighbour above
  43. * this block).
  44. *
  45. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  46. * | ... |
  47. * | | +------------------+ +------------------+
  48. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  49. * +-------------+ +------------------+ +------------------+
  50. * | #N top copy | | #N top copy <==== |
  51. * +-------------+ +------------------+ |..................|
  52. * | #N |
  53. * ...
  54. * | | +----------------+ +----------------------+
  55. * | | | #N bottom copy | | block #N bottom copy |
  56. * ^ +------------------+ +----------------+ +----------------------+
  57. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  58. * | +------------------+ +----------------+ | |
  59. * Z ...
  60. *
  61. * - save_cl_bottom, same for the bottom
  62. * comp. buffer save. buffers comp. buffer save. buffers comp. buffer
  63. * | ... |
  64. * | | +------------------+ +------------------+
  65. * | #N+1 | | #N+1 bottom copy | | #N+1 bottom copy |
  66. * +-------------+ +------------------+ +------------------+
  67. * | #N top copy | | #N top copy | | |
  68. * +-------------+ +------------------+ | |
  69. * | #N |
  70. * ...
  71. * |..................| +----------------+ +----------------------+
  72. * | ====>#N bottom copy | | block #N bottom copy |
  73. * ^ +------------------+ +----------------+ +----------------------+
  74. * | | #N-1 top copy | | #N-1 top copy | | block #N-1 |
  75. * | +------------------+ +----------------+ | |
  76. * Z ...
  77. *
  78. * The idea is that the computation buffers thus don't have to move, only their
  79. * boundaries are copied to buffers that do move (be it CPU/GPU, GPU/GPU or via
  80. * MPI)
  81. *
  82. * For each of the buffers above, there are two (0/1) buffers to make new/old switch costless.
  83. */
  84. #if 0
  85. # define DEBUG(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
  86. #else
  87. # define DEBUG(fmt, ...) (void) 0
  88. #endif
  89. /* Record which GPU ran which block, for nice pictures */
  90. int who_runs_what_len;
  91. int *who_runs_what;
  92. int *who_runs_what_index;
  93. struct timeval *last_tick;
  94. /* Record how many updates each worker performed */
  95. unsigned update_per_worker[STARPU_NMAXWORKERS];
  96. /*
  97. * Load a neighbour's boundary into block, CPU version
  98. */
  99. static void load_subblock_from_buffer_cpu(starpu_block_interface_t *block,
  100. starpu_block_interface_t *boundary,
  101. unsigned firstz)
  102. {
  103. /* Sanity checks */
  104. STARPU_ASSERT(block->nx == boundary->nx);
  105. STARPU_ASSERT(block->ny == boundary->ny);
  106. STARPU_ASSERT(boundary->nz == K);
  107. /* NB: this is not fully garanteed ... but it's *very* likely and that
  108. * makes our life much simpler */
  109. STARPU_ASSERT(block->ldy == boundary->ldy);
  110. STARPU_ASSERT(block->ldz == boundary->ldz);
  111. /* We do a contiguous memory transfer */
  112. size_t boundary_size = K*block->ldz*block->elemsize;
  113. unsigned offset = firstz*block->ldz;
  114. TYPE *block_data = (TYPE *)block->ptr;
  115. TYPE *boundary_data = (TYPE *)boundary->ptr;
  116. memcpy(&block_data[offset], boundary_data, boundary_size);
  117. }
  118. /*
  119. * Load a neighbour's boundary into block, CUDA version
  120. */
  121. #ifdef STARPU_USE_CUDA
  122. static void load_subblock_from_buffer_cuda(starpu_block_interface_t *block,
  123. starpu_block_interface_t *boundary,
  124. unsigned firstz)
  125. {
  126. /* Sanity checks */
  127. STARPU_ASSERT(block->nx == boundary->nx);
  128. STARPU_ASSERT(block->ny == boundary->ny);
  129. STARPU_ASSERT(boundary->nz == K);
  130. /* NB: this is not fully garanteed ... but it's *very* likely and that
  131. * makes our life much simpler */
  132. STARPU_ASSERT(block->ldy == boundary->ldy);
  133. STARPU_ASSERT(block->ldz == boundary->ldz);
  134. /* We do a contiguous memory transfer */
  135. size_t boundary_size = K*block->ldz*block->elemsize;
  136. unsigned offset = firstz*block->ldz;
  137. TYPE *block_data = (TYPE *)block->ptr;
  138. TYPE *boundary_data = (TYPE *)boundary->ptr;
  139. cudaMemcpy(&block_data[offset], boundary_data, boundary_size, cudaMemcpyDeviceToDevice);
  140. }
  141. /*
  142. * cl_update (CUDA version)
  143. */
  144. static void update_func_cuda(void *descr[], void *arg)
  145. {
  146. struct block_description *block = arg;
  147. int workerid = starpu_worker_get_id();
  148. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  149. if (block->bz == 0)
  150. fprintf(stderr,"!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  151. else
  152. DEBUG( "!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
  153. #ifdef STARPU_USE_MPI
  154. int rank = 0;
  155. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  156. DEBUG( "!!! RANK %d !!!\n", rank);
  157. #endif
  158. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  159. unsigned block_size_z = get_block_size(block->bz);
  160. unsigned i;
  161. update_per_worker[workerid]++;
  162. struct timeval tv, tv2, diff, delta = {.tv_sec = 0, .tv_usec = get_ticks()*1000};
  163. gettimeofday(&tv, NULL);
  164. timersub(&tv, &start, &tv2);
  165. timersub(&tv2, &last_tick[block->bz], &diff);
  166. while (timercmp(&diff, &delta, >=)) {
  167. timeradd(&last_tick[block->bz], &delta, &last_tick[block->bz]);
  168. timersub(&tv2, &last_tick[block->bz], &diff);
  169. if (who_runs_what_index[block->bz] < who_runs_what_len)
  170. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
  171. }
  172. if (who_runs_what_index[block->bz] < who_runs_what_len)
  173. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);
  174. /*
  175. * Load neighbours' boundaries : TOP
  176. */
  177. /* The offset along the z axis is (block_size_z + K) */
  178. load_subblock_from_buffer_cuda(descr[0], descr[2], block_size_z+K);
  179. load_subblock_from_buffer_cuda(descr[1], descr[3], block_size_z+K);
  180. /*
  181. * Load neighbours' boundaries : BOTTOM
  182. */
  183. load_subblock_from_buffer_cuda(descr[0], descr[4], 0);
  184. load_subblock_from_buffer_cuda(descr[1], descr[5], 0);
  185. /*
  186. * Stencils ... do the actual work here :) TODO
  187. */
  188. for (i=1; i<=K; i++)
  189. {
  190. starpu_block_interface_t *oldb = descr[i%2], *newb = descr[(i+1)%2];
  191. TYPE *old = (void*) oldb->ptr, *new = (void*) newb->ptr;
  192. /* Shadow data */
  193. cuda_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  194. /* And perform actual computation */
  195. #ifdef LIFE
  196. cuda_life_update_host(block->bz, old, new, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  197. #else
  198. cudaMemcpy(new, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*new), cudaMemcpyDeviceToDevice);
  199. #endif /* LIFE */
  200. }
  201. cudaError_t cures;
  202. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  203. STARPU_CUDA_REPORT_ERROR(cures);
  204. }
  205. #endif /* STARPU_USE_CUDA */
  206. /*
  207. * cl_update (CPU version)
  208. */
  209. static void update_func_cpu(void *descr[], void *arg)
  210. {
  211. struct block_description *block = arg;
  212. int workerid = starpu_worker_get_id();
  213. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  214. if (block->bz == 0)
  215. fprintf(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  216. else
  217. DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
  218. #ifdef STARPU_USE_MPI
  219. int rank = 0;
  220. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  221. DEBUG( "!!! RANK %d !!!\n", rank);
  222. #endif
  223. DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  224. unsigned block_size_z = get_block_size(block->bz);
  225. unsigned i;
  226. update_per_worker[workerid]++;
  227. struct timeval tv, tv2, diff, delta = {.tv_sec = 0, .tv_usec = get_ticks() * 1000};
  228. gettimeofday(&tv, NULL);
  229. timersub(&tv, &start, &tv2);
  230. timersub(&tv2, &last_tick[block->bz], &diff);
  231. while (timercmp(&diff, &delta, >=)) {
  232. timeradd(&last_tick[block->bz], &delta, &last_tick[block->bz]);
  233. timersub(&tv2, &last_tick[block->bz], &diff);
  234. if (who_runs_what_index[block->bz] < who_runs_what_len)
  235. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
  236. }
  237. if (who_runs_what_index[block->bz] < who_runs_what_len)
  238. who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);
  239. /*
  240. * Load neighbours' boundaries : TOP
  241. */
  242. /* The offset along the z axis is (block_size_z + K) */
  243. load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
  244. load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);
  245. /*
  246. * Load neighbours' boundaries : BOTTOM
  247. */
  248. load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
  249. load_subblock_from_buffer_cpu(descr[1], descr[5], 0);
  250. /*
  251. * Stencils ... do the actual work here :) TODO
  252. */
  253. for (i=1; i<=K; i++)
  254. {
  255. starpu_block_interface_t *oldb = descr[i%2], *newb = descr[(i+1)%2];
  256. TYPE *old = (void*) oldb->ptr, *new = (void*) newb->ptr;
  257. /* Shadow data */
  258. unsigned ldy = oldb->ldy, ldz = oldb->ldz;
  259. unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
  260. unsigned x, y, z;
  261. unsigned stepx = 1;
  262. unsigned stepy = 1;
  263. unsigned stepz = 1;
  264. unsigned idx = 0;
  265. unsigned idy = 0;
  266. unsigned idz = 0;
  267. TYPE *ptr = old;
  268. # include "shadow.h"
  269. /* And perform actual computation */
  270. #ifdef LIFE
  271. life_update(block->bz, old, new, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
  272. #else
  273. memcpy(new, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*new));
  274. #endif /* LIFE */
  275. }
  276. }
  277. /* Performance model and codelet structure */
  278. static struct starpu_perfmodel_t cl_update_model = {
  279. .type = STARPU_HISTORY_BASED,
  280. .symbol = "cl_update"
  281. };
  282. starpu_codelet cl_update = {
  283. .where =
  284. #ifdef STARPU_USE_CUDA
  285. STARPU_CUDA|
  286. #endif
  287. STARPU_CPU,
  288. .cpu_func = update_func_cpu,
  289. #ifdef STARPU_USE_CUDA
  290. .cuda_func = update_func_cuda,
  291. #endif
  292. .model = &cl_update_model,
  293. .nbuffers = 6
  294. };
  295. /*
  296. * Save the block internal boundaries to give them to our neighbours.
  297. */
  298. /* CPU version */
  299. static void load_subblock_into_buffer_cpu(starpu_block_interface_t *block,
  300. starpu_block_interface_t *boundary,
  301. unsigned firstz)
  302. {
  303. /* Sanity checks */
  304. STARPU_ASSERT(block->nx == boundary->nx);
  305. STARPU_ASSERT(block->ny == boundary->ny);
  306. STARPU_ASSERT(boundary->nz == K);
  307. /* NB: this is not fully garanteed ... but it's *very* likely and that
  308. * makes our life much simpler */
  309. STARPU_ASSERT(block->ldy == boundary->ldy);
  310. STARPU_ASSERT(block->ldz == boundary->ldz);
  311. /* We do a contiguous memory transfer */
  312. size_t boundary_size = K*block->ldz*block->elemsize;
  313. unsigned offset = firstz*block->ldz;
  314. TYPE *block_data = (TYPE *)block->ptr;
  315. TYPE *boundary_data = (TYPE *)boundary->ptr;
  316. memcpy(boundary_data, &block_data[offset], boundary_size);
  317. }
  318. /* CUDA version */
  319. #ifdef STARPU_USE_CUDA
  320. static void load_subblock_into_buffer_cuda(starpu_block_interface_t *block,
  321. starpu_block_interface_t *boundary,
  322. unsigned firstz)
  323. {
  324. /* Sanity checks */
  325. STARPU_ASSERT(block->nx == boundary->nx);
  326. STARPU_ASSERT(block->ny == boundary->ny);
  327. STARPU_ASSERT(boundary->nz == K);
  328. /* NB: this is not fully garanteed ... but it's *very* likely and that
  329. * makes our life much simpler */
  330. STARPU_ASSERT(block->ldy == boundary->ldy);
  331. STARPU_ASSERT(block->ldz == boundary->ldz);
  332. /* We do a contiguous memory transfer */
  333. size_t boundary_size = K*block->ldz*block->elemsize;
  334. unsigned offset = firstz*block->ldz;
  335. TYPE *block_data = (TYPE *)block->ptr;
  336. TYPE *boundary_data = (TYPE *)boundary->ptr;
  337. cudaMemcpy(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice);
  338. }
  339. #endif /* STARPU_USE_CUDA */
  340. /* Record how many top/bottom saves each worker performed */
  341. unsigned top_per_worker[STARPU_NMAXWORKERS];
  342. unsigned bottom_per_worker[STARPU_NMAXWORKERS];
  343. /* top save, CPU version */
  344. static void dummy_func_top_cpu(void *descr[] __attribute__((unused)), void *arg)
  345. {
  346. struct block_description *block = arg;
  347. int workerid = starpu_worker_get_id();
  348. top_per_worker[workerid]++;
  349. DEBUG( "DO SAVE Bottom block %d\n", block->bz);
  350. /* The offset along the z axis is (block_size_z + K)- K */
  351. unsigned block_size_z = get_block_size(block->bz);
  352. load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
  353. load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
  354. }
  355. /* bottom save, CPU version */
  356. static void dummy_func_bottom_cpu(void *descr[] __attribute__((unused)), void *arg)
  357. {
  358. struct block_description *block = arg;
  359. int workerid = starpu_worker_get_id();
  360. bottom_per_worker[workerid]++;
  361. DEBUG( "DO SAVE Top block %d\n", block->bz);
  362. load_subblock_into_buffer_cpu(descr[0], descr[2], K);
  363. load_subblock_into_buffer_cpu(descr[1], descr[3], K);
  364. }
  365. /* top save, CUDA version */
  366. #ifdef STARPU_USE_CUDA
  367. static void dummy_func_top_cuda(void *descr[] __attribute__((unused)), void *arg)
  368. {
  369. struct block_description *block = arg;
  370. int workerid = starpu_worker_get_id();
  371. top_per_worker[workerid]++;
  372. DEBUG( "DO SAVE Top block %d\n", block->bz);
  373. /* The offset along the z axis is (block_size_z + K)- K */
  374. unsigned block_size_z = get_block_size(block->bz);
  375. load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
  376. load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
  377. cudaThreadSynchronize();
  378. }
  379. /* bottom save, CUDA version */
  380. static void dummy_func_bottom_cuda(void *descr[] __attribute__((unused)), void *arg)
  381. {
  382. struct block_description *block = arg;
  383. int workerid = starpu_worker_get_id();
  384. bottom_per_worker[workerid]++;
  385. DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);
  386. load_subblock_into_buffer_cuda(descr[0], descr[2], K);
  387. load_subblock_into_buffer_cuda(descr[1], descr[3], K);
  388. cudaThreadSynchronize();
  389. }
  390. #endif /* STARPU_USE_CUDA */
  391. /* Performance models and codelet for save */
  392. static struct starpu_perfmodel_t save_cl_bottom_model = {
  393. .type = STARPU_HISTORY_BASED,
  394. .symbol = "save_cl_bottom"
  395. };
  396. static struct starpu_perfmodel_t save_cl_top_model = {
  397. .type = STARPU_HISTORY_BASED,
  398. .symbol = "save_cl_top"
  399. };
  400. starpu_codelet save_cl_bottom = {
  401. .where =
  402. #ifdef STARPU_USE_CUDA
  403. STARPU_CUDA|
  404. #endif
  405. STARPU_CPU,
  406. .cpu_func = dummy_func_bottom_cpu,
  407. #ifdef STARPU_USE_CUDA
  408. .cuda_func = dummy_func_bottom_cuda,
  409. #endif
  410. .model = &save_cl_bottom_model,
  411. .nbuffers = 4
  412. };
  413. starpu_codelet save_cl_top = {
  414. .where =
  415. #ifdef STARPU_USE_CUDA
  416. STARPU_CUDA|
  417. #endif
  418. STARPU_CPU,
  419. .cpu_func = dummy_func_top_cpu,
  420. #ifdef STARPU_USE_CUDA
  421. .cuda_func = dummy_func_top_cuda,
  422. #endif
  423. .model = &save_cl_top_model,
  424. .nbuffers = 4
  425. };