stencil-tasks.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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 "stencil.h"
  17. #define BIND_LAST 1
  18. /*
  19. * Schedule tasks for updates and saves
  20. */
  21. /*
  22. * NB: iter = 0: initialization phase, TAG_U(z, 0) = TAG_INIT
  23. *
  24. * dir is -1 or +1.
  25. */
  26. #if 0
  27. # define DEBUG(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
  28. #else
  29. # define DEBUG(fmt, ...)
  30. #endif
  31. /*
  32. * SAVE
  33. */
  34. /* R(z) = R(z+d) = local, just call the save kernel */
  35. static void create_task_save_local(unsigned iter, unsigned z, int dir)
  36. {
  37. struct starpu_task *save_task = starpu_task_create();
  38. struct block_description *descr = get_block_description(z);
  39. save_task->cl = (dir == -1)?&save_cl_bottom:&save_cl_top;
  40. save_task->cl_arg = descr;
  41. /* Saving our border... */
  42. save_task->handles[0] = descr->layers_handle[0];
  43. save_task->handles[1] = descr->layers_handle[1];
  44. /* ... to the neighbour's copy */
  45. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  46. save_task->handles[2] = neighbour->boundaries_handle[(1-dir)/2][0];
  47. save_task->handles[3] = neighbour->boundaries_handle[(1-dir)/2][1];
  48. /* Bind */
  49. if (iter <= BIND_LAST)
  50. save_task->execute_on_a_specific_worker = get_bind_tasks();
  51. save_task->workerid = descr->preferred_worker;
  52. int ret = starpu_task_submit(save_task);
  53. if (ret)
  54. {
  55. FPRINTF(stderr, "Could not submit task save: %d\n", ret);
  56. if (ret == -ENODEV)
  57. exit(77);
  58. STARPU_ABORT();
  59. }
  60. }
  61. /* R(z) = local & R(z+d) != local */
  62. /* We need to send our save over MPI */
  63. static void send_done(void *arg)
  64. {
  65. uintptr_t z = (uintptr_t) arg;
  66. (void) z;
  67. DEBUG("DO SEND %d\n", (int)z);
  68. }
  69. #if defined(STARPU_USE_MPI) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  70. /* Post MPI send */
  71. static void create_task_save_mpi_send(unsigned iter, unsigned z, int dir, int local_rank)
  72. {
  73. struct block_description *descr = get_block_description(z);
  74. STARPU_ASSERT(descr->mpi_node == local_rank);
  75. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  76. int dest = neighbour->mpi_node;
  77. STARPU_ASSERT(neighbour->mpi_node != local_rank);
  78. /* Send neighbour's border copy to the neighbour */
  79. starpu_data_handle_t handle0 = neighbour->boundaries_handle[(1-dir)/2][0];
  80. starpu_data_handle_t handle1 = neighbour->boundaries_handle[(1-dir)/2][1];
  81. starpu_mpi_isend_detached(handle0, dest, MPI_TAG0(z, iter, dir), MPI_COMM_WORLD, send_done, (void*)(uintptr_t)z);
  82. starpu_mpi_isend_detached(handle1, dest, MPI_TAG1(z, iter, dir), MPI_COMM_WORLD, send_done, (void*)(uintptr_t)z);
  83. }
  84. /* R(z) != local & R(z+d) = local */
  85. /* We need to receive over MPI */
  86. static void recv_done(void *arg)
  87. {
  88. uintptr_t z = (uintptr_t) arg;
  89. (void) z;
  90. DEBUG("DO RECV %d\n", (int)z);
  91. }
  92. /* Post MPI recv */
  93. static void create_task_save_mpi_recv(unsigned iter, unsigned z, int dir, int local_rank)
  94. {
  95. struct block_description *descr = get_block_description(z);
  96. STARPU_ASSERT(descr->mpi_node != local_rank);
  97. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  98. int source = descr->mpi_node;
  99. STARPU_ASSERT(neighbour->mpi_node == local_rank);
  100. /* Receive our neighbour's border in our neighbour copy */
  101. starpu_data_handle_t handle0 = neighbour->boundaries_handle[(1-dir)/2][0];
  102. starpu_data_handle_t handle1 = neighbour->boundaries_handle[(1-dir)/2][1];
  103. starpu_mpi_irecv_detached(handle0, source, MPI_TAG0(z, iter, dir), MPI_COMM_WORLD, recv_done, (void*)(uintptr_t)z);
  104. starpu_mpi_irecv_detached(handle1, source, MPI_TAG1(z, iter, dir), MPI_COMM_WORLD, recv_done, (void*)(uintptr_t)z);
  105. }
  106. #endif /* STARPU_USE_MPI */
  107. /*
  108. * Schedule saving boundaries of blocks to communication buffers
  109. */
  110. void create_task_save(unsigned iter, unsigned z, int dir, int local_rank)
  111. {
  112. int node_z = get_block_mpi_node(z);
  113. int node_z_and_d = get_block_mpi_node(z+dir);
  114. #if defined(STARPU_USE_MPI) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  115. if (node_z == local_rank)
  116. {
  117. /* Save data from update */
  118. create_task_save_local(iter, z, dir);
  119. if (node_z_and_d != local_rank)
  120. {
  121. /* R(z) = local & R(z+d) != local, We have to send the data */
  122. create_task_save_mpi_send(iter, z, dir, local_rank);
  123. }
  124. }
  125. else
  126. {
  127. /* node_z != local_rank, this MPI node doesn't have the saved data */
  128. if (node_z_and_d == local_rank)
  129. {
  130. create_task_save_mpi_recv(iter, z, dir, local_rank);
  131. }
  132. else
  133. {
  134. /* R(z) != local & R(z+d) != local We don't have
  135. the saved data and don't need it, we shouldn't
  136. even have been called! */
  137. STARPU_ABORT();
  138. }
  139. }
  140. #else /* !STARPU_USE_MPI */
  141. STARPU_ASSERT((node_z == local_rank) && (node_z_and_d == local_rank));
  142. create_task_save_local(iter, z, dir);
  143. #endif /* STARPU_USE_MPI */
  144. }
  145. /*
  146. * Schedule update computation in computation buffer
  147. */
  148. void create_task_update(unsigned iter, unsigned z, int local_rank)
  149. {
  150. (void)local_rank; // unneeded parameter, we keep it to have a similar function prototype to the implicit case
  151. STARPU_ASSERT(iter != 0);
  152. struct starpu_task *task = starpu_task_create();
  153. unsigned niter = get_niter();
  154. /* We are going to synchronize with the last tasks */
  155. if (iter == niter)
  156. {
  157. task->use_tag = 1;
  158. task->tag_id = TAG_FINISH(z);
  159. }
  160. unsigned old_layer = (K*(iter-1)) % 2;
  161. unsigned new_layer = (old_layer + 1) % 2;
  162. struct block_description *descr = get_block_description(z);
  163. task->handles[0] = descr->layers_handle[new_layer];
  164. task->handles[1] = descr->layers_handle[old_layer];
  165. task->handles[2] = descr->boundaries_handle[T][new_layer];
  166. task->handles[3] = descr->boundaries_handle[T][old_layer];
  167. task->handles[4] = descr->boundaries_handle[B][new_layer];
  168. task->handles[5] = descr->boundaries_handle[B][old_layer];
  169. task->cl = &cl_update;
  170. task->cl_arg = descr;
  171. if (iter <= BIND_LAST)
  172. task->execute_on_a_specific_worker = get_bind_tasks();
  173. task->workerid = descr->preferred_worker;
  174. int ret = starpu_task_submit(task);
  175. if (ret)
  176. {
  177. FPRINTF(stderr, "Could not submit task update block: %d\n", ret);
  178. if (ret == -ENODEV)
  179. exit(77);
  180. STARPU_ABORT();
  181. }
  182. }
  183. /* Dummy empty codelet taking one buffer */
  184. void null_func(void *descr[], void *arg)
  185. {
  186. (void)descr;
  187. (void)arg;
  188. }
  189. static double null_cost_function(struct starpu_task *task, unsigned nimpl)
  190. {
  191. (void) task;
  192. (void) nimpl;
  193. return 0.000001;
  194. }
  195. static struct starpu_perfmodel null_model =
  196. {
  197. .type = STARPU_COMMON,
  198. .cost_function = null_cost_function,
  199. .symbol = "null"
  200. };
  201. static struct starpu_codelet null =
  202. {
  203. .modes = { STARPU_W, STARPU_W },
  204. .cpu_funcs = {null_func},
  205. .cpu_funcs_name = {"null_func"},
  206. .cuda_funcs = {null_func},
  207. .opencl_funcs = {null_func},
  208. .nbuffers = 2,
  209. .model = &null_model,
  210. .name = "start"
  211. };
  212. void create_start_task(int z, int dir)
  213. {
  214. /* Dumb task depending on the init task and simulating writing the
  215. neighbour buffers, to avoid communications and computation running
  216. before we start measuring time */
  217. struct starpu_task *wait_init = starpu_task_create();
  218. struct block_description *descr = get_block_description(z);
  219. starpu_tag_t tag_init = TAG_INIT_TASK;
  220. wait_init->cl = &null;
  221. wait_init->use_tag = 1;
  222. wait_init->tag_id = TAG_START(z, dir);
  223. wait_init->handles[0] = descr->boundaries_handle[(1 + dir) / 2][0];
  224. wait_init->handles[1] = descr->boundaries_handle[(1 + dir) / 2][1];
  225. starpu_tag_declare_deps_array(wait_init->tag_id, 1, &tag_init);
  226. int ret = starpu_task_submit(wait_init);
  227. if (ret)
  228. {
  229. FPRINTF(stderr, "Could not submit task initial wait: %d\n", ret);
  230. if (ret == -ENODEV)
  231. exit(77);
  232. STARPU_ABORT();
  233. }
  234. }
  235. /*
  236. * Create all the tasks
  237. */
  238. void create_tasks(int rank)
  239. {
  240. int iter;
  241. int bz;
  242. int niter = get_niter();
  243. int nbz = get_nbz();
  244. for (bz = 0; bz < nbz; bz++)
  245. {
  246. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz+1) == rank))
  247. create_start_task(bz, +1);
  248. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz-1) == rank))
  249. create_start_task(bz, -1);
  250. }
  251. for (iter = 0; iter <= niter; iter++)
  252. {
  253. starpu_iteration_push(iter);
  254. for (bz = 0; bz < nbz; bz++)
  255. {
  256. if ((iter > 0) && (get_block_mpi_node(bz) == rank))
  257. create_task_update(iter, bz, rank);
  258. }
  259. for (bz = 0; bz < nbz; bz++)
  260. {
  261. if (iter != niter)
  262. {
  263. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz+1) == rank))
  264. create_task_save(iter, bz, +1, rank);
  265. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz-1) == rank))
  266. create_task_save(iter, bz, -1, rank);
  267. }
  268. }
  269. starpu_iteration_pop();
  270. }
  271. }
  272. /*
  273. * Wait for termination
  274. */
  275. void wait_end_tasks(int rank)
  276. {
  277. int bz;
  278. int nbz = get_nbz();
  279. for (bz = 0; bz < nbz; bz++)
  280. {
  281. if (get_block_mpi_node(bz) == rank)
  282. {
  283. /* Wait for the task producing block "bz" */
  284. starpu_tag_wait(TAG_FINISH(bz));
  285. /* Get the result back to memory */
  286. struct block_description *block = get_block_description(bz);
  287. starpu_data_acquire(block->layers_handle[0], STARPU_R);
  288. starpu_data_acquire(block->layers_handle[1], STARPU_R);
  289. /* the data_acquire here is done to make sure
  290. * the data is sent back to the ram memory, we
  291. * can safely do a data_release, to avoid the
  292. * data_unregister to block later on
  293. */
  294. starpu_data_release(block->layers_handle[0]);
  295. starpu_data_release(block->layers_handle[1]);
  296. }
  297. }
  298. }