mpi_reduction.c 3.4 KB

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