early_request.c 5.7 KB

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