stencil-tasks.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2013-2015, 2017 Université de Bordeaux
  4. * Copyright (C) 2012, 2013, 2015, 2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "stencil.h"
  18. #define BIND_LAST 1
  19. /*
  20. * Schedule tasks for updates and saves
  21. */
  22. /*
  23. * NB: iter = 0: initialization phase, TAG_U(z, 0) = TAG_INIT
  24. *
  25. * dir is -1 or +1.
  26. */
  27. #if 0
  28. # define DEBUG(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
  29. #else
  30. # define DEBUG(fmt, ...)
  31. #endif
  32. /*
  33. * SAVE
  34. */
  35. /* R(z) = R(z+d) = local, just call the save kernel */
  36. static void create_task_save_local(unsigned iter, unsigned z, int dir, int local_rank)
  37. {
  38. struct starpu_task *save_task = starpu_task_create();
  39. struct block_description *descr = get_block_description(z);
  40. save_task->cl = (dir == -1)?&save_cl_bottom:&save_cl_top;
  41. save_task->cl_arg = descr;
  42. /* Saving our border... */
  43. save_task->handles[0] = descr->layers_handle[0];
  44. save_task->handles[1] = descr->layers_handle[1];
  45. /* ... to the neighbour's copy */
  46. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  47. save_task->handles[2] = neighbour->boundaries_handle[(1-dir)/2][0];
  48. save_task->handles[3] = neighbour->boundaries_handle[(1-dir)/2][1];
  49. /* Bind */
  50. if (iter <= BIND_LAST)
  51. save_task->execute_on_a_specific_worker = get_bind_tasks();
  52. save_task->workerid = descr->preferred_worker;
  53. int ret = starpu_task_submit(save_task);
  54. if (ret)
  55. {
  56. FPRINTF(stderr, "Could not submit task save: %d\n", ret);
  57. if (ret == -ENODEV)
  58. exit(77);
  59. STARPU_ABORT();
  60. }
  61. }
  62. /* R(z) = local & R(z+d) != local */
  63. /* We need to send our save over MPI */
  64. static void send_done(void *arg)
  65. {
  66. uintptr_t z = (uintptr_t) arg;
  67. (void) z;
  68. DEBUG("DO SEND %d\n", (int)z);
  69. }
  70. #if defined(STARPU_USE_MPI) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  71. /* Post MPI send */
  72. static void create_task_save_mpi_send(unsigned iter, unsigned z, int dir, int local_rank)
  73. {
  74. struct block_description *descr = get_block_description(z);
  75. STARPU_ASSERT(descr->mpi_node == local_rank);
  76. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  77. int dest = neighbour->mpi_node;
  78. STARPU_ASSERT(neighbour->mpi_node != local_rank);
  79. /* Send neighbour's border copy to the neighbour */
  80. starpu_data_handle_t handle0 = neighbour->boundaries_handle[(1-dir)/2][0];
  81. starpu_data_handle_t handle1 = neighbour->boundaries_handle[(1-dir)/2][1];
  82. starpu_mpi_isend_detached(handle0, dest, MPI_TAG0(z, iter, dir), MPI_COMM_WORLD, send_done, (void*)(uintptr_t)z);
  83. starpu_mpi_isend_detached(handle1, dest, MPI_TAG1(z, iter, dir), MPI_COMM_WORLD, send_done, (void*)(uintptr_t)z);
  84. }
  85. /* R(z) != local & R(z+d) = local */
  86. /* We need to receive over MPI */
  87. static void recv_done(void *arg)
  88. {
  89. uintptr_t z = (uintptr_t) arg;
  90. (void) z;
  91. DEBUG("DO RECV %d\n", (int)z);
  92. }
  93. /* Post MPI recv */
  94. static void create_task_save_mpi_recv(unsigned iter, unsigned z, int dir, int local_rank)
  95. {
  96. struct block_description *descr = get_block_description(z);
  97. STARPU_ASSERT(descr->mpi_node != local_rank);
  98. struct block_description *neighbour = descr->boundary_blocks[(1+dir)/2];
  99. int source = descr->mpi_node;
  100. STARPU_ASSERT(neighbour->mpi_node == local_rank);
  101. /* Receive our neighbour's border in our neighbour copy */
  102. starpu_data_handle_t handle0 = neighbour->boundaries_handle[(1-dir)/2][0];
  103. starpu_data_handle_t handle1 = neighbour->boundaries_handle[(1-dir)/2][1];
  104. starpu_mpi_irecv_detached(handle0, source, MPI_TAG0(z, iter, dir), MPI_COMM_WORLD, recv_done, (void*)(uintptr_t)z);
  105. starpu_mpi_irecv_detached(handle1, source, MPI_TAG1(z, iter, dir), MPI_COMM_WORLD, recv_done, (void*)(uintptr_t)z);
  106. }
  107. #endif /* STARPU_USE_MPI */
  108. /*
  109. * Schedule saving boundaries of blocks to communication buffers
  110. */
  111. void create_task_save(unsigned iter, unsigned z, int dir, int local_rank)
  112. {
  113. int node_z = get_block_mpi_node(z);
  114. int node_z_and_d = get_block_mpi_node(z+dir);
  115. #if defined(STARPU_USE_MPI) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  116. if (node_z == local_rank)
  117. {
  118. /* Save data from update */
  119. create_task_save_local(iter, z, dir, local_rank);
  120. if (node_z_and_d != local_rank)
  121. {
  122. /* R(z) = local & R(z+d) != local, We have to send the data */
  123. create_task_save_mpi_send(iter, z, dir, local_rank);
  124. }
  125. }
  126. else
  127. {
  128. /* node_z != local_rank, this MPI node doesn't have the saved data */
  129. if (node_z_and_d == local_rank)
  130. {
  131. create_task_save_mpi_recv(iter, z, dir, local_rank);
  132. }
  133. else
  134. {
  135. /* R(z) != local & R(z+d) != local We don't have
  136. the saved data and don't need it, we shouldn't
  137. even have been called! */
  138. STARPU_ABORT();
  139. }
  140. }
  141. #else /* !STARPU_USE_MPI */
  142. STARPU_ASSERT((node_z == local_rank) && (node_z_and_d == local_rank));
  143. create_task_save_local(iter, z, dir, local_rank);
  144. #endif /* STARPU_USE_MPI */
  145. }
  146. /*
  147. * Schedule update computation in computation buffer
  148. */
  149. void create_task_update(unsigned iter, unsigned z, int local_rank)
  150. {
  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[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  185. {
  186. }
  187. static double null_cost_function(struct starpu_task *task, unsigned nimpl)
  188. {
  189. (void) task;
  190. (void) nimpl;
  191. return 0.000001;
  192. }
  193. static struct starpu_perfmodel null_model =
  194. {
  195. .type = STARPU_COMMON,
  196. .cost_function = null_cost_function,
  197. .symbol = "null"
  198. };
  199. static struct starpu_codelet null =
  200. {
  201. .modes = { STARPU_W, STARPU_W },
  202. .cpu_funcs = {null_func},
  203. .cpu_funcs_name = {"null_func"},
  204. .cuda_funcs = {null_func},
  205. .opencl_funcs = {null_func},
  206. .nbuffers = 2,
  207. .model = &null_model,
  208. .name = "start"
  209. };
  210. void create_start_task(int z, int dir)
  211. {
  212. /* Dumb task depending on the init task and simulating writing the
  213. neighbour buffers, to avoid communications and computation running
  214. before we start measuring time */
  215. struct starpu_task *wait_init = starpu_task_create();
  216. struct block_description *descr = get_block_description(z);
  217. starpu_tag_t tag_init = TAG_INIT_TASK;
  218. wait_init->cl = &null;
  219. wait_init->use_tag = 1;
  220. wait_init->tag_id = TAG_START(z, dir);
  221. wait_init->handles[0] = descr->boundaries_handle[(1 + dir) / 2][0];
  222. wait_init->handles[1] = descr->boundaries_handle[(1 + dir) / 2][1];
  223. starpu_tag_declare_deps_array(wait_init->tag_id, 1, &tag_init);
  224. int ret = starpu_task_submit(wait_init);
  225. if (ret)
  226. {
  227. FPRINTF(stderr, "Could not submit task initial wait: %d\n", ret);
  228. if (ret == -ENODEV)
  229. exit(77);
  230. STARPU_ABORT();
  231. }
  232. }
  233. /*
  234. * Create all the tasks
  235. */
  236. void create_tasks(int rank)
  237. {
  238. int iter;
  239. int bz;
  240. int niter = get_niter();
  241. int nbz = get_nbz();
  242. for (bz = 0; bz < nbz; bz++)
  243. {
  244. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz+1) == rank))
  245. create_start_task(bz, +1);
  246. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz-1) == rank))
  247. create_start_task(bz, -1);
  248. }
  249. for (iter = 0; iter <= niter; iter++)
  250. {
  251. starpu_iteration_push(iter);
  252. for (bz = 0; bz < nbz; bz++)
  253. {
  254. if ((iter > 0) && (get_block_mpi_node(bz) == rank))
  255. create_task_update(iter, bz, rank);
  256. }
  257. for (bz = 0; bz < nbz; bz++)
  258. {
  259. if (iter != niter)
  260. {
  261. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz+1) == rank))
  262. create_task_save(iter, bz, +1, rank);
  263. if ((get_block_mpi_node(bz) == rank) || (get_block_mpi_node(bz-1) == rank))
  264. create_task_save(iter, bz, -1, rank);
  265. }
  266. }
  267. starpu_iteration_pop();
  268. }
  269. }
  270. /*
  271. * Wait for termination
  272. */
  273. void wait_end_tasks(int rank)
  274. {
  275. int bz;
  276. int nbz = get_nbz();
  277. for (bz = 0; bz < nbz; bz++)
  278. {
  279. if (get_block_mpi_node(bz) == rank)
  280. {
  281. /* Wait for the task producing block "bz" */
  282. starpu_tag_wait(TAG_FINISH(bz));
  283. /* Get the result back to memory */
  284. struct block_description *block = get_block_description(bz);
  285. starpu_data_acquire(block->layers_handle[0], STARPU_R);
  286. starpu_data_acquire(block->layers_handle[1], STARPU_R);
  287. /* the data_acquire here is done to make sure
  288. * the data is sent back to the ram memory, we
  289. * can safely do a data_release, to avoid the
  290. * data_unregister to block later on
  291. */
  292. starpu_data_release(block->layers_handle[0]);
  293. starpu_data_release(block->layers_handle[1]);
  294. }
  295. }
  296. }