mpi_reduction.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #define X 5
  19. #define Y 3
  20. int display = 0;
  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. static struct starpu_codelet init_codelet =
  25. {
  26. .where = STARPU_CPU,
  27. .cpu_funcs = {init_cpu_func, NULL},
  28. .nbuffers = 1
  29. };
  30. static struct starpu_codelet redux_codelet =
  31. {
  32. .where = STARPU_CPU,
  33. .cpu_funcs = {redux_cpu_func, NULL},
  34. .nbuffers = 2
  35. };
  36. static struct starpu_codelet dot_codelet =
  37. {
  38. .where = STARPU_CPU,
  39. .cpu_funcs = {dot_cpu_func, NULL},
  40. .nbuffers = 3,
  41. .modes = {STARPU_R, STARPU_R, STARPU_REDUX}
  42. };
  43. static void parse_args(int argc, char **argv)
  44. {
  45. int i;
  46. for (i = 1; i < argc; i++)
  47. {
  48. if (strcmp(argv[i], "-display") == 0)
  49. {
  50. display = 1;
  51. }
  52. }
  53. }
  54. /* Returns the MPI node number where data indexes index is */
  55. int my_distrib(int x, int y, int nb_nodes)
  56. {
  57. /* Block distrib */
  58. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. int my_rank, size, x, y;
  63. int value=0;
  64. unsigned matrixA[X][Y];
  65. unsigned matrixB[X][Y];
  66. unsigned dot, sum=0;
  67. starpu_data_handle_t handlesA[X][Y];
  68. starpu_data_handle_t handlesB[X][Y];
  69. starpu_data_handle_t dot_handle;
  70. starpu_init(NULL);
  71. starpu_mpi_initialize_extended(&my_rank, &size);
  72. parse_args(argc, argv);
  73. for(x = 0; x < X; x++)
  74. {
  75. for (y = 0; y < Y; y++)
  76. {
  77. matrixA[x][y] = value;
  78. matrixB[x][y] = 10+value;
  79. value++;
  80. sum += matrixA[x][y] + matrixB[x][y];
  81. }
  82. }
  83. for(x = 0; x < X; x++)
  84. {
  85. for (y = 0; y < Y; y++)
  86. {
  87. int mpi_rank = my_distrib(x, y, size);
  88. if (mpi_rank == my_rank)
  89. {
  90. /* Owning data */
  91. starpu_variable_data_register(&handlesA[x][y], 0, (uintptr_t)&(matrixA[x][y]), sizeof(unsigned));
  92. starpu_variable_data_register(&handlesB[x][y], 0, (uintptr_t)&(matrixB[x][y]), sizeof(unsigned));
  93. }
  94. else
  95. {
  96. starpu_variable_data_register(&handlesA[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  97. starpu_variable_data_register(&handlesB[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  98. }
  99. if (handlesA[x][y])
  100. {
  101. starpu_data_set_rank(handlesA[x][y], mpi_rank);
  102. starpu_data_set_tag(handlesA[x][y], (y*X)+x);
  103. }
  104. if (handlesB[x][y])
  105. {
  106. starpu_data_set_rank(handlesB[x][y], mpi_rank);
  107. starpu_data_set_tag(handlesB[x][y], (y*X)+x);
  108. }
  109. }
  110. }
  111. starpu_variable_data_register(&dot_handle, 0, (uintptr_t)&dot, sizeof(unsigned));
  112. starpu_data_set_rank(dot_handle, 0);
  113. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  114. for (x = 0; x < X; x++)
  115. {
  116. for (y = 0; y < Y ; y++)
  117. {
  118. starpu_mpi_insert_task(MPI_COMM_WORLD,
  119. &dot_codelet,
  120. STARPU_R, handlesA[x][y],
  121. STARPU_R, handlesB[x][y],
  122. STARPU_REDUX, dot_handle,
  123. 0);
  124. }
  125. }
  126. fprintf(stderr, "Waiting ...\n");
  127. starpu_task_wait_for_all();
  128. for(x = 0; x < X; x++)
  129. {
  130. for (y = 0; y < Y; y++)
  131. {
  132. if (handlesA[x][y]) starpu_data_unregister(handlesA[x][y]);
  133. if (handlesB[x][y]) starpu_data_unregister(handlesB[x][y]);
  134. }
  135. }
  136. if (dot_handle)
  137. {
  138. starpu_data_unregister(dot_handle);
  139. }
  140. starpu_mpi_shutdown();
  141. starpu_shutdown();
  142. if (display)
  143. {
  144. fprintf(stdout, "[%d] sum=%d\n", my_rank, sum);
  145. fprintf(stdout, "[%d] dot=%d\n", my_rank, dot);
  146. }
  147. return 0;
  148. }