mpi_reduction.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2013 Inria
  4. * Copyright (C) 2012-2017 CNRS
  5. * Copyright (C) 2013-2015,2017,2018 Université de Bordeaux
  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_mpi.h>
  19. #include <math.h>
  20. #include "helper.h"
  21. extern void init_cpu_func(void *descr[], void *cl_arg);
  22. extern void redux_cpu_func(void *descr[], void *cl_arg);
  23. extern void dot_cpu_func(void *descr[], void *cl_arg);
  24. extern void display_cpu_func(void *descr[], void *cl_arg);
  25. static struct starpu_codelet init_codelet =
  26. {
  27. .cpu_funcs = {init_cpu_func},
  28. .nbuffers = 1,
  29. .modes = {STARPU_W},
  30. #ifdef STARPU_SIMGRID
  31. .model = &starpu_perfmodel_nop,
  32. #endif
  33. .name = "init_codelet"
  34. };
  35. static struct starpu_codelet redux_codelet =
  36. {
  37. .cpu_funcs = {redux_cpu_func},
  38. .modes = {STARPU_RW, STARPU_R},
  39. .nbuffers = 2,
  40. #ifdef STARPU_SIMGRID
  41. .model = &starpu_perfmodel_nop,
  42. #endif
  43. .name = "redux_codelet"
  44. };
  45. static struct starpu_codelet dot_codelet =
  46. {
  47. .cpu_funcs = {dot_cpu_func},
  48. .nbuffers = 2,
  49. .modes = {STARPU_R, STARPU_REDUX},
  50. #ifdef STARPU_SIMGRID
  51. .model = &starpu_perfmodel_nop,
  52. #endif
  53. .name = "dot_codelet"
  54. };
  55. static struct starpu_codelet display_codelet =
  56. {
  57. .cpu_funcs = {display_cpu_func},
  58. .nbuffers = 1,
  59. .modes = {STARPU_R},
  60. #ifdef STARPU_SIMGRID
  61. .model = &starpu_perfmodel_nop,
  62. #endif
  63. .name = "display_codelet"
  64. };
  65. /* Returns the MPI node number where data indexes index is */
  66. int my_distrib(int x, int nb_nodes)
  67. {
  68. return x % nb_nodes;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. int my_rank, size, x, y, i;
  73. long int *vector;
  74. long int dot, sum=0;
  75. starpu_data_handle_t *handles;
  76. starpu_data_handle_t dot_handle;
  77. int ret;
  78. int nb_elements, step, loops;
  79. STARPU_SKIP_IF_VALGRIND_RETURN_SKIP;
  80. /* Not supported yet */
  81. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  82. return STARPU_TEST_SKIPPED;
  83. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  85. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  86. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  87. if (starpu_cpu_worker_get_count() == 0)
  88. {
  89. if (my_rank == 0)
  90. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  91. starpu_mpi_shutdown();
  92. return STARPU_TEST_SKIPPED;
  93. }
  94. nb_elements = size*8000;
  95. step = 4;
  96. loops = 5;
  97. vector = (long int *) malloc(nb_elements*sizeof(vector[0]));
  98. for(x = 0; x < nb_elements; x+=step)
  99. {
  100. int mpi_rank = my_distrib(x/step, size);
  101. if (mpi_rank == my_rank)
  102. {
  103. for(y=0 ; y<step ; y++)
  104. {
  105. vector[x+y] = x+y+1;
  106. }
  107. }
  108. }
  109. if (my_rank == 0)
  110. {
  111. dot = 14;
  112. sum = (nb_elements * (nb_elements + 1)) / 2;
  113. sum *= loops;
  114. sum += dot;
  115. starpu_variable_data_register(&dot_handle, STARPU_MAIN_RAM, (uintptr_t)&dot, sizeof(dot));
  116. }
  117. else
  118. {
  119. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(dot));
  120. }
  121. handles = (starpu_data_handle_t *) malloc(nb_elements*sizeof(handles[0]));
  122. for(x = 0; x < nb_elements; x+=step)
  123. {
  124. handles[x] = NULL;
  125. int mpi_rank = my_distrib(x/step, size);
  126. if (mpi_rank == my_rank)
  127. {
  128. /* Owning data */
  129. starpu_vector_data_register(&handles[x], STARPU_MAIN_RAM, (uintptr_t)&(vector[x]), step, sizeof(vector[0]));
  130. }
  131. else
  132. {
  133. starpu_vector_data_register(&handles[x], -1, (uintptr_t)NULL, step, sizeof(vector[0]));
  134. }
  135. if (handles[x])
  136. {
  137. starpu_mpi_data_register(handles[x], x, mpi_rank);
  138. }
  139. }
  140. starpu_mpi_data_register(dot_handle, nb_elements+1, 0);
  141. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  142. for (i = 0; i < loops; i++)
  143. {
  144. for (x = 0; x < nb_elements; x+=step)
  145. {
  146. starpu_mpi_task_insert(MPI_COMM_WORLD,
  147. &dot_codelet,
  148. STARPU_R, handles[x],
  149. STARPU_REDUX, dot_handle,
  150. 0);
  151. }
  152. starpu_mpi_redux_data(MPI_COMM_WORLD, dot_handle);
  153. starpu_mpi_task_insert(MPI_COMM_WORLD, &display_codelet, STARPU_R, dot_handle, 0);
  154. }
  155. FPRINTF_MPI(stderr, "Waiting ...\n");
  156. starpu_task_wait_for_all();
  157. for(x = 0; x < nb_elements; x+=step)
  158. {
  159. if (handles[x])
  160. starpu_data_unregister(handles[x]);
  161. }
  162. starpu_data_unregister(dot_handle);
  163. free(vector);
  164. free(handles);
  165. starpu_mpi_shutdown();
  166. if (my_rank == 0)
  167. {
  168. FPRINTF(stderr, "[%d] sum=%ld\n", my_rank, sum);
  169. }
  170. #ifndef STARPU_SIMGRID
  171. if (my_rank == 0)
  172. {
  173. FPRINTF(stderr, "[%d] dot=%ld\n", my_rank, dot);
  174. FPRINTF(stderr, "%s when computing reduction\n", (sum == dot) ? "Success" : "Error");
  175. if (sum != dot)
  176. return 1;
  177. }
  178. #endif
  179. return 0;
  180. }