mpi_reduction.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 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 nb_elements, step, loops;
  78. STARPU_SKIP_IF_VALGRIND_RETURN_SKIP;
  79. /* Not supported yet */
  80. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  81. return STARPU_TEST_SKIPPED;
  82. int ret = starpu_init(NULL);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  84. ret = starpu_mpi_init(&argc, &argv, 1);
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  86. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  87. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  88. if (starpu_cpu_worker_get_count() == 0)
  89. {
  90. if (my_rank == 0)
  91. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  92. starpu_mpi_shutdown();
  93. starpu_shutdown();
  94. return STARPU_TEST_SKIPPED;
  95. }
  96. nb_elements = size*8000;
  97. step = 4;
  98. loops = 5;
  99. vector = (long int *) malloc(nb_elements*sizeof(vector[0]));
  100. for(x = 0; x < nb_elements; x+=step)
  101. {
  102. int mpi_rank = my_distrib(x/step, size);
  103. if (mpi_rank == my_rank)
  104. {
  105. for(y=0 ; y<step ; y++)
  106. {
  107. vector[x+y] = x+y+1;
  108. }
  109. }
  110. }
  111. if (my_rank == 0)
  112. {
  113. dot = 14;
  114. sum = (nb_elements * (nb_elements + 1)) / 2;
  115. sum *= loops;
  116. sum += dot;
  117. starpu_variable_data_register(&dot_handle, STARPU_MAIN_RAM, (uintptr_t)&dot, sizeof(dot));
  118. }
  119. else
  120. {
  121. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(dot));
  122. }
  123. handles = (starpu_data_handle_t *) malloc(nb_elements*sizeof(handles[0]));
  124. for(x = 0; x < nb_elements; x+=step)
  125. {
  126. handles[x] = NULL;
  127. int mpi_rank = my_distrib(x/step, size);
  128. if (mpi_rank == my_rank)
  129. {
  130. /* Owning data */
  131. starpu_vector_data_register(&handles[x], STARPU_MAIN_RAM, (uintptr_t)&(vector[x]), step, sizeof(vector[0]));
  132. }
  133. else
  134. {
  135. starpu_vector_data_register(&handles[x], -1, (uintptr_t)NULL, step, sizeof(vector[0]));
  136. }
  137. if (handles[x])
  138. {
  139. starpu_mpi_data_register(handles[x], x, mpi_rank);
  140. }
  141. }
  142. starpu_mpi_data_register(dot_handle, nb_elements+1, 0);
  143. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  144. for (i = 0; i < loops; i++)
  145. {
  146. for (x = 0; x < nb_elements; x+=step)
  147. {
  148. starpu_mpi_task_insert(MPI_COMM_WORLD,
  149. &dot_codelet,
  150. STARPU_R, handles[x],
  151. STARPU_REDUX, dot_handle,
  152. 0);
  153. }
  154. starpu_mpi_redux_data(MPI_COMM_WORLD, dot_handle);
  155. starpu_mpi_task_insert(MPI_COMM_WORLD, &display_codelet, STARPU_R, dot_handle, 0);
  156. }
  157. FPRINTF_MPI(stderr, "Waiting ...\n");
  158. starpu_task_wait_for_all();
  159. for(x = 0; x < nb_elements; x+=step)
  160. {
  161. if (handles[x])
  162. starpu_data_unregister(handles[x]);
  163. }
  164. starpu_data_unregister(dot_handle);
  165. free(vector);
  166. free(handles);
  167. starpu_mpi_shutdown();
  168. starpu_shutdown();
  169. if (my_rank == 0)
  170. {
  171. FPRINTF(stderr, "[%d] sum=%ld\n", my_rank, sum);
  172. }
  173. #ifndef STARPU_SIMGRID
  174. if (my_rank == 0)
  175. {
  176. FPRINTF(stderr, "[%d] dot=%ld\n", my_rank, dot);
  177. FPRINTF(stderr, "%s when computing reduction\n", (sum == dot) ? "Success" : "Error");
  178. if (sum != dot)
  179. return 1;
  180. }
  181. #endif
  182. return 0;
  183. }