mpi_reduction.c 4.3 KB

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