mpi_reduction.c 4.2 KB

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