mpi_reduction.c 4.4 KB

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