mpi_reduction.c 3.8 KB

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