early_request.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2017 CNRS
  4. * Copyright (C) 2015-2018 Université de Bordeaux
  5. * Copyright (C) 2015 Inria
  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 <starpu.h>
  19. #include <starpu_mpi.h>
  20. #include "helper.h"
  21. #define NUM_EL 5
  22. #define NUM_LOOPS 10
  23. /*
  24. * This testcase written by J-M Couteyen allows to test that several
  25. * early requests for a given source and tag can be posted to StarPU
  26. * by the application before data arrive.
  27. *
  28. * In this test case, multiples processes (called "domains") exchanges
  29. * informations between multiple "elements" multiple times, with
  30. * different sizes (in order to catch error more easily).
  31. * The communications are independent between the elements (each one
  32. * as its proper tag), but must occur in the submitted order for an
  33. * element taken independtly.
  34. */
  35. struct element
  36. {
  37. int tag;
  38. int foreign_domain;
  39. int array_send[100];
  40. int array_recv[100];
  41. starpu_data_handle_t ensure_submitted_order_send;
  42. starpu_data_handle_t ensure_submitted_order_recv;
  43. starpu_data_handle_t send;
  44. starpu_data_handle_t recv;
  45. };
  46. /* functions/codelet to fill the bufferss*/
  47. void fill_tmp_buffer(void *buffers[], void *cl_arg)
  48. {
  49. (void)cl_arg;
  50. int *tmp = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  51. int nx = STARPU_VECTOR_GET_NX(buffers[0]);
  52. int i;
  53. for (i=0; i<nx; i++)
  54. tmp[i]=nx+i;
  55. }
  56. static struct starpu_codelet fill_tmp_buffer_cl =
  57. {
  58. .where = STARPU_CPU,
  59. .cpu_funcs = {fill_tmp_buffer, NULL},
  60. .nbuffers = 1,
  61. .modes = {STARPU_W},
  62. #ifdef STARPU_SIMGRID
  63. .model = &starpu_perfmodel_nop,
  64. #endif
  65. .name = "fill_tmp_buffer"
  66. };
  67. void read_ghost(void *buffers[], void *cl_arg)
  68. {
  69. (void)cl_arg;
  70. int *tmp = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  71. int nx=STARPU_VECTOR_GET_NX(buffers[0]);
  72. int i;
  73. for(i=0; i<nx;i++)
  74. {
  75. assert(tmp[i]==nx+i);
  76. }
  77. }
  78. static struct starpu_codelet read_ghost_value_cl =
  79. {
  80. .where = STARPU_CPU,
  81. .cpu_funcs = {read_ghost, NULL},
  82. .nbuffers = 1,
  83. .modes = {STARPU_R},
  84. #ifdef STARPU_SIMGRID
  85. .model = &starpu_perfmodel_nop,
  86. #endif
  87. .name = "read_ghost_value"
  88. };
  89. /*codelet to ensure submitted order for a given element*/
  90. void noop(void *buffers[], void *cl_arg)
  91. {
  92. (void)buffers;
  93. (void)cl_arg;
  94. }
  95. void submitted_order_fun(void *buffers[], void *cl_arg)
  96. {
  97. (void)buffers;
  98. (void)cl_arg;
  99. }
  100. static struct starpu_codelet submitted_order =
  101. {
  102. .where = STARPU_CPU,
  103. .cpu_funcs = {submitted_order_fun, NULL},
  104. .nbuffers = 2,
  105. .modes = {STARPU_RW, STARPU_W},
  106. #ifdef STARPU_SIMGRID
  107. .model = &starpu_perfmodel_nop,
  108. #endif
  109. .name = "submitted_order_enforcer"
  110. };
  111. void init_element(struct element *el, int size, int foreign_domain)
  112. {
  113. el->tag=size;
  114. el->foreign_domain=foreign_domain;
  115. int mpi_rank;
  116. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  117. starpu_vector_data_register(&el->recv, 0, (uintptr_t)el->array_recv, size, sizeof(int));
  118. starpu_vector_data_register(&el->send, 0, (uintptr_t)el->array_send, size, sizeof(int));
  119. starpu_void_data_register(&el->ensure_submitted_order_send);
  120. starpu_void_data_register(&el->ensure_submitted_order_recv);
  121. }
  122. void free_element(struct element *el)
  123. {
  124. starpu_data_unregister(el->recv);
  125. starpu_data_unregister(el->send);
  126. starpu_data_unregister(el->ensure_submitted_order_send);
  127. starpu_data_unregister(el->ensure_submitted_order_recv);
  128. }
  129. void insert_work_for_one_element(struct element *el)
  130. {
  131. starpu_data_handle_t tmp_recv;
  132. starpu_data_handle_t tmp_send;
  133. starpu_vector_data_register(&tmp_recv, -1, 0, el->tag, sizeof(int));
  134. starpu_vector_data_register(&tmp_send, -1, 0, el->tag, sizeof(int));
  135. //Emulate the work to fill the send buffer
  136. starpu_insert_task(&fill_tmp_buffer_cl,
  137. STARPU_W,tmp_send,
  138. 0);
  139. //Send operation
  140. starpu_insert_task(&submitted_order,
  141. STARPU_RW,el->ensure_submitted_order_send,
  142. STARPU_W,tmp_send,
  143. 0);
  144. starpu_mpi_isend_detached(tmp_send,el->foreign_domain,el->tag, MPI_COMM_WORLD, NULL, NULL);
  145. //Recv operation for current element
  146. starpu_insert_task(&submitted_order,
  147. STARPU_RW,el->ensure_submitted_order_recv,
  148. STARPU_W,tmp_recv,
  149. 0);
  150. starpu_mpi_irecv_detached(tmp_recv,el->foreign_domain,el->tag, MPI_COMM_WORLD, NULL, NULL);
  151. //Emulate the "reading" of the recv value.
  152. starpu_insert_task(&read_ghost_value_cl,
  153. STARPU_R,tmp_recv,
  154. 0);
  155. starpu_data_unregister_submit(tmp_send);
  156. starpu_data_unregister_submit(tmp_recv);
  157. }
  158. /*main program*/
  159. int main(int argc, char * argv[])
  160. {
  161. /* Init */
  162. int ret;
  163. int mpi_rank, mpi_size;
  164. int mpi_init;
  165. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  166. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  168. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  169. starpu_mpi_comm_size(MPI_COMM_WORLD, &mpi_size);
  170. if (starpu_cpu_worker_get_count() == 0)
  171. {
  172. if (mpi_rank == 0)
  173. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  174. starpu_mpi_shutdown();
  175. if (!mpi_init)
  176. MPI_Finalize();
  177. return STARPU_TEST_SKIPPED;
  178. }
  179. /*element initialization : domains are connected as a ring for this test*/
  180. int num_elements=NUM_EL;
  181. struct element * el_left=malloc(num_elements*sizeof(el_left[0]));
  182. struct element * el_right=malloc(num_elements*sizeof(el_right[0]));
  183. int i;
  184. for(i=0;i<num_elements;i++)
  185. {
  186. init_element(el_left+i,i+1,((mpi_rank-1)+mpi_size)%mpi_size);
  187. init_element(el_right+i,i+1,(mpi_rank+1)%mpi_size);
  188. }
  189. /* Communication loop */
  190. for (i=0; i<NUM_LOOPS; i++) //number of "computations loops"
  191. {
  192. int e;
  193. for (e=0;e<num_elements;e++) //Do something for each elements
  194. {
  195. insert_work_for_one_element(el_right+e);
  196. insert_work_for_one_element(el_left+e);
  197. }
  198. }
  199. /* End */
  200. starpu_task_wait_for_all();
  201. for(i=0;i<num_elements;i++)
  202. {
  203. free_element(el_left+i);
  204. free_element(el_right+i);
  205. }
  206. free(el_left);
  207. free(el_right);
  208. starpu_mpi_shutdown();
  209. if (!mpi_init)
  210. MPI_Finalize();
  211. FPRINTF(stderr, "No assert until end\n");
  212. return 0;
  213. }