early_request.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. static struct starpu_codelet fill_tmp_buffer_cl =
  55. {
  56. .where = STARPU_CPU,
  57. .cpu_funcs = {fill_tmp_buffer, NULL},
  58. .nbuffers = 1,
  59. .modes = {STARPU_W},
  60. #ifdef STARPU_SIMGRID
  61. .model = &starpu_nop_perf_model,
  62. #endif
  63. .name = "fill_tmp_buffer"
  64. };
  65. void read_ghost(void *buffers[], void *cl_arg)
  66. {
  67. int *tmp = (int *) STARPU_VECTOR_GET_PTR(buffers[0]);
  68. int nx=STARPU_VECTOR_GET_NX(buffers[0]);
  69. int i;
  70. for(i=0; i<nx;i++)
  71. {
  72. assert(tmp[i]==nx+i);
  73. }
  74. }
  75. static struct starpu_codelet read_ghost_value_cl =
  76. {
  77. .where = STARPU_CPU,
  78. .cpu_funcs = {read_ghost, NULL},
  79. .nbuffers = 1,
  80. .modes = {STARPU_R},
  81. #ifdef STARPU_SIMGRID
  82. .model = &starpu_nop_perf_model,
  83. #endif
  84. .name = "read_ghost_value"
  85. };
  86. /*codelet to ensure submitted order for a given element*/
  87. void noop(void *buffers[], void *cl_arg)
  88. {
  89. }
  90. void submitted_order_fun(void *buffers[], void *cl_arg)
  91. {
  92. }
  93. static struct starpu_codelet submitted_order =
  94. {
  95. .where = STARPU_CPU,
  96. .cpu_funcs = {submitted_order_fun, NULL},
  97. .nbuffers = 2,
  98. .modes = {STARPU_RW, STARPU_W},
  99. #ifdef STARPU_SIMGRID
  100. .model = &starpu_nop_perf_model,
  101. #endif
  102. .name = "submitted_order_enforcer"
  103. };
  104. void init_element(struct element *el, int size, int foreign_domain)
  105. {
  106. el->tag=size;
  107. el->foreign_domain=foreign_domain;
  108. int mpi_rank;
  109. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  110. starpu_vector_data_register(&el->recv, 0, (uintptr_t)el->array_recv, size, sizeof(int));
  111. starpu_vector_data_register(&el->send, 0, (uintptr_t)el->array_send, size, sizeof(int));
  112. starpu_void_data_register(&el->ensure_submitted_order_send);
  113. starpu_void_data_register(&el->ensure_submitted_order_recv);
  114. }
  115. void free_element(struct element *el)
  116. {
  117. starpu_data_unregister(el->recv);
  118. starpu_data_unregister(el->send);
  119. starpu_data_unregister(el->ensure_submitted_order_send);
  120. starpu_data_unregister(el->ensure_submitted_order_recv);
  121. }
  122. void insert_work_for_one_element(struct element *el)
  123. {
  124. starpu_data_handle_t tmp_recv;
  125. starpu_data_handle_t tmp_send;
  126. starpu_vector_data_register(&tmp_recv, -1, 0, el->tag, sizeof(int));
  127. starpu_vector_data_register(&tmp_send, -1, 0, el->tag, sizeof(int));
  128. //Emulate the work to fill the send buffer
  129. starpu_insert_task(&fill_tmp_buffer_cl,
  130. STARPU_W,tmp_send,
  131. 0);
  132. //Send operation
  133. starpu_insert_task(&submitted_order,
  134. STARPU_RW,el->ensure_submitted_order_send,
  135. STARPU_W,tmp_send,
  136. 0);
  137. starpu_mpi_isend_detached(tmp_send,el->foreign_domain,el->tag, MPI_COMM_WORLD, NULL, NULL);
  138. //Recv operation for current element
  139. starpu_insert_task(&submitted_order,
  140. STARPU_RW,el->ensure_submitted_order_recv,
  141. STARPU_W,tmp_recv,
  142. 0);
  143. starpu_mpi_irecv_detached(tmp_recv,el->foreign_domain,el->tag, MPI_COMM_WORLD, NULL, NULL);
  144. //Emulate the "reading" of the recv value.
  145. starpu_insert_task(&read_ghost_value_cl,
  146. STARPU_R,tmp_recv,
  147. 0);
  148. starpu_data_unregister_submit(tmp_send);
  149. starpu_data_unregister_submit(tmp_recv);
  150. }
  151. /*main program*/
  152. int main(int argc, char * argv[])
  153. {
  154. /* Init */
  155. int ret;
  156. int mpi_rank, mpi_size;
  157. int mpi_init;
  158. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  159. ret = starpu_init(NULL);
  160. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  161. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  163. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  164. starpu_mpi_comm_size(MPI_COMM_WORLD, &mpi_size);
  165. if (starpu_cpu_worker_get_count() == 0)
  166. {
  167. if (mpi_rank == 0)
  168. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  169. starpu_mpi_shutdown();
  170. starpu_shutdown();
  171. if (!mpi_init)
  172. MPI_Finalize();
  173. return STARPU_TEST_SKIPPED;
  174. }
  175. /*element initialization : domains are connected as a ring for this test*/
  176. int num_elements=NUM_EL;
  177. struct element * el_left=malloc(num_elements*sizeof(el_left[0]));
  178. struct element * el_right=malloc(num_elements*sizeof(el_right[0]));
  179. int i;
  180. for(i=0;i<num_elements;i++)
  181. {
  182. init_element(el_left+i,i+1,((mpi_rank-1)+mpi_size)%mpi_size);
  183. init_element(el_right+i,i+1,(mpi_rank+1)%mpi_size);
  184. }
  185. /* Communication loop */
  186. for (i=0; i<NUM_LOOPS; i++) //number of "computations loops"
  187. {
  188. int e;
  189. for (e=0;e<num_elements;e++) //Do something for each elements
  190. {
  191. insert_work_for_one_element(el_right+e);
  192. insert_work_for_one_element(el_left+e);
  193. }
  194. }
  195. /* End */
  196. starpu_task_wait_for_all();
  197. for(i=0;i<num_elements;i++)
  198. {
  199. free_element(el_left+i);
  200. free_element(el_right+i);
  201. }
  202. free(el_left);
  203. free(el_right);
  204. starpu_mpi_shutdown();
  205. starpu_shutdown();
  206. if (!mpi_init)
  207. MPI_Finalize();
  208. FPRINTF(stderr, "No assert until end\n");
  209. return 0;
  210. }