mpi_reduction.c 3.3 KB

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