mix_comm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015,2017 CNRS
  4. * Copyright (C) 2015,2017 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This example splits the whole set of communicators in subgroups,
  19. * communications take place both within each subgroups and MPI_COMM_WORLD.
  20. */
  21. #include <starpu_mpi.h>
  22. #include "../helper.h"
  23. void func_cpu(void *descr[], void *_args)
  24. {
  25. int *value = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  26. int rank;
  27. starpu_codelet_unpack_args(_args, &rank);
  28. FPRINTF_MPI(stderr, "Executing codelet with value %d and rank %d\n", *value, rank);
  29. STARPU_ASSERT_MSG(*value == rank, "Received value %d is not the expected value %d\n", *value, rank);
  30. }
  31. struct starpu_codelet mycodelet =
  32. {
  33. .cpu_funcs = {func_cpu},
  34. .nbuffers = 1,
  35. .modes = {STARPU_RW},
  36. .model = &starpu_perfmodel_nop,
  37. };
  38. int main(int argc, char **argv)
  39. {
  40. int size, x;
  41. int color;
  42. MPI_Comm newcomm;
  43. int rank, newrank;
  44. int ret;
  45. starpu_data_handle_t data[3];
  46. int value = 90;
  47. int thread_support;
  48. if (MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  49. {
  50. fprintf(stderr,"MPI_Init_thread failed\n");
  51. exit(1);
  52. }
  53. if (thread_support == MPI_THREAD_FUNNELED)
  54. fprintf(stderr,"Warning: MPI only has funneled thread support, not serialized, hoping this will work\n");
  55. if (thread_support < MPI_THREAD_FUNNELED)
  56. fprintf(stderr,"Warning: MPI does not have thread support!\n");
  57. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  58. MPI_Comm_size(MPI_COMM_WORLD, &size);
  59. if (size < 4)
  60. {
  61. FPRINTF(stderr, "We need at least 4 processes.\n");
  62. MPI_Finalize();
  63. return STARPU_TEST_SKIPPED;
  64. }
  65. color = rank%2;
  66. MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
  67. MPI_Comm_rank(newcomm, &newrank);
  68. FPRINTF(stderr, "[%d][%d] color %d\n", rank, newrank, color);
  69. if (newrank == 0)
  70. {
  71. FPRINTF(stderr, "[%d][%d] sending %d\n", rank, newrank, rank);
  72. MPI_Send(&rank, 1, MPI_INT, 1, 10, newcomm);
  73. }
  74. else if (newrank == 1)
  75. {
  76. MPI_Recv(&x, 1, MPI_INT, 0, 10, newcomm, MPI_STATUS_IGNORE);
  77. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, x);
  78. }
  79. ret = starpu_init(NULL);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. ret = starpu_mpi_init(NULL, NULL, 0);
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  83. if (rank == 0)
  84. {
  85. starpu_variable_data_register(&data[2], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  86. }
  87. else
  88. starpu_variable_data_register(&data[2], -1, (uintptr_t)NULL, sizeof(int));
  89. starpu_mpi_data_register_comm(data[2], 44, 0, MPI_COMM_WORLD);
  90. if (newrank == 0)
  91. {
  92. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  93. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  94. starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
  95. }
  96. else
  97. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(int));
  98. starpu_mpi_data_register_comm(data[0], 12, 0, newcomm);
  99. if (newrank == 0)
  100. {
  101. starpu_mpi_req req[2];
  102. starpu_mpi_issend(data[1], &req[0], 1, 22, newcomm);
  103. starpu_mpi_isend(data[0], &req[1], 1, 12, newcomm);
  104. starpu_mpi_wait(&req[0], MPI_STATUS_IGNORE);
  105. starpu_mpi_wait(&req[1], MPI_STATUS_IGNORE);
  106. }
  107. else if (newrank == 1)
  108. {
  109. int *xx;
  110. starpu_mpi_recv(data[0], 0, 12, newcomm, MPI_STATUS_IGNORE);
  111. starpu_data_acquire(data[0], STARPU_RW);
  112. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  113. starpu_data_release(data[0]);
  114. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
  115. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  116. starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(int));
  117. starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
  118. starpu_mpi_recv(data[0], 0, 22, newcomm, MPI_STATUS_IGNORE);
  119. starpu_data_acquire(data[0], STARPU_RW);
  120. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  121. starpu_data_release(data[0]);
  122. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
  123. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  124. }
  125. if (rank == 0)
  126. {
  127. starpu_data_acquire(data[2], STARPU_RW);
  128. int rvalue = *((int *)starpu_variable_get_local_ptr(data[2]));
  129. starpu_data_release(data[2]);
  130. FPRINTF_MPI(stderr, "sending value %d to %d and receiving from %d\n", rvalue, 1, size-1);
  131. starpu_mpi_send(data[2], 1, 44, MPI_COMM_WORLD);
  132. starpu_mpi_recv(data[2], size-1, 44, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  133. starpu_data_acquire(data[2], STARPU_RW);
  134. int *xx = (int *)starpu_variable_get_local_ptr(data[2]);
  135. starpu_data_release(data[2]);
  136. FPRINTF_MPI(stderr, "Value back is %d\n", *xx);
  137. STARPU_ASSERT_MSG(*xx == rvalue + (2*(size-1)), "Received value %d is incorrect (should be %d)\n", *xx, rvalue + (2*(size-1)));
  138. }
  139. else
  140. {
  141. int next = (rank == size-1) ? 0 : rank+1;
  142. starpu_mpi_recv(data[2], rank-1, 44, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  143. starpu_data_acquire(data[2], STARPU_RW);
  144. int *xx = (int *)starpu_variable_get_local_ptr(data[2]);
  145. FPRINTF_MPI(stderr, "receiving %d from %d and sending %d to %d\n", *xx, rank-1, *xx+2, next);
  146. *xx = *xx + 2;
  147. starpu_data_release(data[2]);
  148. starpu_mpi_send(data[2], next, 44, MPI_COMM_WORLD);
  149. }
  150. if (newrank == 0 || newrank == 1)
  151. {
  152. starpu_mpi_task_insert(newcomm, &mycodelet,
  153. STARPU_RW, data[0],
  154. STARPU_VALUE, &x, sizeof(x),
  155. STARPU_EXECUTE_ON_NODE, 1,
  156. 0);
  157. starpu_task_wait_for_all();
  158. starpu_data_unregister(data[0]);
  159. starpu_data_unregister(data[1]);
  160. }
  161. starpu_data_unregister(data[2]);
  162. starpu_mpi_shutdown();
  163. starpu_shutdown();
  164. MPI_Comm_free(&newcomm);
  165. MPI_Finalize();
  166. return 0;
  167. }