mpi_reduction.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu_mpi.h>
  17. #include <math.h>
  18. #include "helper.h"
  19. extern void init_cpu_func(void *descr[], void *cl_arg);
  20. extern void redux_cpu_func(void *descr[], void *cl_arg);
  21. extern void dot_cpu_func(void *descr[], void *cl_arg);
  22. extern void display_cpu_func(void *descr[], void *cl_arg);
  23. static struct starpu_codelet init_codelet =
  24. {
  25. .cpu_funcs = {init_cpu_func},
  26. .nbuffers = 1,
  27. .modes = {STARPU_W},
  28. #ifdef STARPU_SIMGRID
  29. .model = &starpu_perfmodel_nop,
  30. #endif
  31. .name = "init_codelet"
  32. };
  33. static struct starpu_codelet redux_codelet =
  34. {
  35. .cpu_funcs = {redux_cpu_func},
  36. .modes = {STARPU_RW, STARPU_R},
  37. .nbuffers = 2,
  38. #ifdef STARPU_SIMGRID
  39. .model = &starpu_perfmodel_nop,
  40. #endif
  41. .name = "redux_codelet"
  42. };
  43. static struct starpu_codelet dot_codelet =
  44. {
  45. .cpu_funcs = {dot_cpu_func},
  46. .nbuffers = 2,
  47. .modes = {STARPU_R, STARPU_REDUX},
  48. #ifdef STARPU_SIMGRID
  49. .model = &starpu_perfmodel_nop,
  50. #endif
  51. .name = "dot_codelet"
  52. };
  53. static struct starpu_codelet display_codelet =
  54. {
  55. .cpu_funcs = {display_cpu_func},
  56. .nbuffers = 1,
  57. .modes = {STARPU_R},
  58. #ifdef STARPU_SIMGRID
  59. .model = &starpu_perfmodel_nop,
  60. #endif
  61. .name = "display_codelet"
  62. };
  63. /* Returns the MPI node number where data indexes index is */
  64. int my_distrib(int x, int nb_nodes)
  65. {
  66. return x % nb_nodes;
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. int my_rank, size, x, y, i;
  71. long int *vector;
  72. long int dot, sum=0;
  73. starpu_data_handle_t *handles;
  74. starpu_data_handle_t dot_handle;
  75. int ret;
  76. int nb_elements, step, loops;
  77. STARPU_SKIP_IF_VALGRIND_RETURN_SKIP;
  78. /* Not supported yet */
  79. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  80. return STARPU_TEST_SKIPPED;
  81. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  83. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  84. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  85. if (starpu_cpu_worker_get_count() == 0)
  86. {
  87. if (my_rank == 0)
  88. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  89. starpu_mpi_shutdown();
  90. return STARPU_TEST_SKIPPED;
  91. }
  92. nb_elements = size*8000;
  93. step = 4;
  94. loops = 5;
  95. vector = (long int *) malloc(nb_elements*sizeof(vector[0]));
  96. for(x = 0; x < nb_elements; x+=step)
  97. {
  98. int mpi_rank = my_distrib(x/step, size);
  99. if (mpi_rank == my_rank)
  100. {
  101. for(y=0 ; y<step ; y++)
  102. {
  103. vector[x+y] = x+y+1;
  104. }
  105. }
  106. }
  107. if (my_rank == 0)
  108. {
  109. dot = 14;
  110. sum = (nb_elements * (nb_elements + 1)) / 2;
  111. sum *= loops;
  112. sum += dot;
  113. starpu_variable_data_register(&dot_handle, STARPU_MAIN_RAM, (uintptr_t)&dot, sizeof(dot));
  114. }
  115. else
  116. {
  117. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(dot));
  118. }
  119. handles = (starpu_data_handle_t *) malloc(nb_elements*sizeof(handles[0]));
  120. for(x = 0; x < nb_elements; x+=step)
  121. {
  122. handles[x] = NULL;
  123. int mpi_rank = my_distrib(x/step, size);
  124. if (mpi_rank == my_rank)
  125. {
  126. /* Owning data */
  127. starpu_vector_data_register(&handles[x], STARPU_MAIN_RAM, (uintptr_t)&(vector[x]), step, sizeof(vector[0]));
  128. }
  129. else
  130. {
  131. starpu_vector_data_register(&handles[x], -1, (uintptr_t)NULL, step, sizeof(vector[0]));
  132. }
  133. if (handles[x])
  134. {
  135. starpu_mpi_data_register(handles[x], x, mpi_rank);
  136. }
  137. }
  138. starpu_mpi_data_register(dot_handle, nb_elements+1, 0);
  139. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  140. for (i = 0; i < loops; i++)
  141. {
  142. for (x = 0; x < nb_elements; x+=step)
  143. {
  144. starpu_mpi_task_insert(MPI_COMM_WORLD,
  145. &dot_codelet,
  146. STARPU_R, handles[x],
  147. STARPU_REDUX, dot_handle,
  148. 0);
  149. }
  150. starpu_mpi_redux_data(MPI_COMM_WORLD, dot_handle);
  151. starpu_mpi_task_insert(MPI_COMM_WORLD, &display_codelet, STARPU_R, dot_handle, 0);
  152. }
  153. FPRINTF_MPI(stderr, "Waiting ...\n");
  154. starpu_task_wait_for_all();
  155. for(x = 0; x < nb_elements; x+=step)
  156. {
  157. if (handles[x])
  158. starpu_data_unregister(handles[x]);
  159. }
  160. starpu_data_unregister(dot_handle);
  161. free(vector);
  162. free(handles);
  163. starpu_mpi_shutdown();
  164. if (my_rank == 0)
  165. {
  166. FPRINTF(stderr, "[%d] sum=%ld\n", my_rank, sum);
  167. }
  168. #ifndef STARPU_SIMGRID
  169. if (my_rank == 0)
  170. {
  171. FPRINTF(stderr, "[%d] dot=%ld\n", my_rank, dot);
  172. FPRINTF(stderr, "%s when computing reduction\n", (sum == dot) ? "Success" : "Error");
  173. if (sum != dot)
  174. return 1;
  175. }
  176. #endif
  177. return 0;
  178. }