stencil-kernels.c 17 KB

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