mix_comm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 CNRS
  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. /*
  17. * This example splits the whole set of communicators in subgroups,
  18. * communications take place both within each subgroups and MPI_COMM_WORLD.
  19. */
  20. #include <starpu_mpi.h>
  21. #include "../helper.h"
  22. void func_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  23. {
  24. int *value = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  25. int rank;
  26. starpu_codelet_unpack_args(_args, &rank);
  27. FPRINTF_MPI(stderr, "Executing codelet with value %d and rank %d\n", *value, rank);
  28. STARPU_ASSERT_MSG(*value == rank, "Received value %d is not the expected value %d\n", *value, rank);
  29. }
  30. struct starpu_codelet mycodelet =
  31. {
  32. .cpu_funcs = {func_cpu},
  33. .nbuffers = 1,
  34. .modes = {STARPU_RW}
  35. };
  36. int main(int argc, char **argv)
  37. {
  38. int size, x;
  39. int color;
  40. MPI_Comm newcomm;
  41. int rank, newrank;
  42. int ret;
  43. starpu_data_handle_t data[3];
  44. int value = 90;
  45. MPI_Init(&argc, &argv);
  46. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  47. MPI_Comm_size(MPI_COMM_WORLD, &size);
  48. if (size < 4)
  49. {
  50. FPRINTF(stderr, "We need at least 4 processes.\n");
  51. MPI_Finalize();
  52. return STARPU_TEST_SKIPPED;
  53. }
  54. color = rank%2;
  55. MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
  56. MPI_Comm_rank(newcomm, &newrank);
  57. FPRINTF(stderr, "[%d][%d] color %d\n", rank, newrank, color);
  58. if (newrank == 0)
  59. {
  60. FPRINTF(stderr, "[%d][%d] sending %d\n", rank, newrank, rank);
  61. MPI_Send(&rank, 1, MPI_INT, 1, 10, newcomm);
  62. }
  63. else if (newrank == 1)
  64. {
  65. MPI_Recv(&x, 1, MPI_INT, 0, 10, newcomm, MPI_STATUS_IGNORE);
  66. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, x);
  67. }
  68. ret = starpu_init(NULL);
  69. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  70. ret = starpu_mpi_init(NULL, NULL, 0);
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  72. if (rank == 0)
  73. {
  74. starpu_variable_data_register(&data[2], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  75. }
  76. else
  77. starpu_variable_data_register(&data[2], -1, (uintptr_t)NULL, sizeof(int));
  78. starpu_mpi_data_register_comm(data[2], 44, 0, MPI_COMM_WORLD);
  79. if (newrank == 0)
  80. {
  81. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  82. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  83. starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
  84. }
  85. else
  86. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(int));
  87. starpu_mpi_data_register_comm(data[0], 12, 0, newcomm);
  88. if (newrank == 0)
  89. {
  90. starpu_mpi_req req[2];
  91. starpu_mpi_issend(data[1], &req[0], 1, 22, newcomm);
  92. starpu_mpi_isend(data[0], &req[1], 1, 12, newcomm);
  93. starpu_mpi_wait(&req[0], MPI_STATUS_IGNORE);
  94. starpu_mpi_wait(&req[1], MPI_STATUS_IGNORE);
  95. }
  96. else if (newrank == 1)
  97. {
  98. int *xx;
  99. starpu_mpi_recv(data[0], 0, 12, newcomm, MPI_STATUS_IGNORE);
  100. starpu_data_acquire(data[0], STARPU_RW);
  101. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  102. starpu_data_release(data[0]);
  103. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
  104. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  105. starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(int));
  106. starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
  107. starpu_mpi_recv(data[0], 0, 22, newcomm, MPI_STATUS_IGNORE);
  108. starpu_data_acquire(data[0], STARPU_RW);
  109. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  110. starpu_data_release(data[0]);
  111. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
  112. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  113. }
  114. if (rank == 0)
  115. {
  116. starpu_data_acquire(data[2], STARPU_RW);
  117. int rvalue = *((int *)starpu_variable_get_local_ptr(data[2]));
  118. starpu_data_release(data[2]);
  119. FPRINTF_MPI(stderr, "sending value %d to %d and receiving from %d\n", rvalue, 1, size-1);
  120. starpu_mpi_send(data[2], 1, 44, MPI_COMM_WORLD);
  121. starpu_mpi_recv(data[2], size-1, 44, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  122. starpu_data_acquire(data[2], STARPU_RW);
  123. int *xx = (int *)starpu_variable_get_local_ptr(data[2]);
  124. starpu_data_release(data[2]);
  125. FPRINTF_MPI(stderr, "Value back is %d\n", *xx);
  126. STARPU_ASSERT_MSG(*xx == rvalue + (2*(size-1)), "Received value %d is incorrect (should be %d)\n", *xx, rvalue + (2*(size-1)));
  127. }
  128. else
  129. {
  130. int next = (rank == size-1) ? 0 : rank+1;
  131. starpu_mpi_recv(data[2], rank-1, 44, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  132. starpu_data_acquire(data[2], STARPU_RW);
  133. int *xx = (int *)starpu_variable_get_local_ptr(data[2]);
  134. FPRINTF_MPI(stderr, "receiving %d from %d and sending %d to %d\n", *xx, rank-1, *xx+2, next);
  135. *xx = *xx + 2;
  136. starpu_data_release(data[2]);
  137. starpu_mpi_send(data[2], next, 44, MPI_COMM_WORLD);
  138. }
  139. if (newrank == 0 || newrank == 1)
  140. {
  141. starpu_mpi_task_insert(newcomm, &mycodelet,
  142. STARPU_RW, data[0],
  143. STARPU_VALUE, &x, sizeof(x),
  144. STARPU_EXECUTE_ON_NODE, 1,
  145. 0);
  146. starpu_task_wait_for_all();
  147. starpu_data_unregister(data[0]);
  148. starpu_data_unregister(data[1]);
  149. }
  150. starpu_data_unregister(data[2]);
  151. starpu_mpi_shutdown();
  152. starpu_shutdown();
  153. MPI_Comm_free(&newcomm);
  154. MPI_Finalize();
  155. return 0;
  156. }