early_request.c 6.5 KB

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