comm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2017 CNRS
  4. * Copyright (C) 2015,2017,2018 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. * all communications take place within each subgroups
  20. */
  21. #include <starpu_mpi.h>
  22. #include "../helper.h"
  23. #define DATA0_TAG 12
  24. #define DATA1_TAG 22
  25. void func_cpu(void *descr[], void *_args)
  26. {
  27. int *value = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  28. int rank;
  29. starpu_codelet_unpack_args(_args, &rank);
  30. FPRINTF_MPI(stderr, "Executing codelet with value %d and rank %d\n", *value, rank);
  31. STARPU_ASSERT_MSG(*value == rank, "Received value %d is not the expected value %d\n", *value, rank);
  32. }
  33. struct starpu_codelet mycodelet =
  34. {
  35. .cpu_funcs = {func_cpu},
  36. .nbuffers = 1,
  37. .modes = {STARPU_RW},
  38. .model = &starpu_perfmodel_nop,
  39. };
  40. int main(int argc, char **argv)
  41. {
  42. int size, x=789;
  43. int color;
  44. MPI_Comm newcomm;
  45. int rank, newrank;
  46. int ret;
  47. starpu_data_handle_t data[2];
  48. int thread_support;
  49. if (MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  50. {
  51. fprintf(stderr,"MPI_Init_thread failed\n");
  52. exit(1);
  53. }
  54. if (thread_support == MPI_THREAD_FUNNELED)
  55. fprintf(stderr,"Warning: MPI only has funneled thread support, not serialized, hoping this will work\n");
  56. if (thread_support < MPI_THREAD_FUNNELED)
  57. fprintf(stderr,"Warning: MPI does not have thread support!\n");
  58. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  59. MPI_Comm_size(MPI_COMM_WORLD, &size);
  60. if (size < 4)
  61. {
  62. FPRINTF(stderr, "We need at least 4 processes.\n");
  63. MPI_Finalize();
  64. return STARPU_TEST_SKIPPED;
  65. }
  66. color = rank%2;
  67. MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
  68. MPI_Comm_rank(newcomm, &newrank);
  69. FPRINTF(stderr, "[%d][%d] color %d\n", rank, newrank, color);
  70. if (newrank == 0)
  71. {
  72. FPRINTF(stderr, "[%d][%d] sending %d\n", rank, newrank, rank);
  73. MPI_Send(&rank, 1, MPI_INT, 1, 10, newcomm);
  74. }
  75. else if (newrank == 1)
  76. {
  77. MPI_Recv(&x, 1, MPI_INT, 0, 10, newcomm, MPI_STATUS_IGNORE);
  78. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, x);
  79. }
  80. ret = starpu_mpi_init_conf(NULL, NULL, 0, newcomm, NULL);
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  82. if (newrank == 0)
  83. {
  84. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  85. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  86. starpu_mpi_data_register_comm(data[1], DATA1_TAG, 0, newcomm);
  87. }
  88. else
  89. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(int));
  90. starpu_mpi_data_register_comm(data[0], DATA0_TAG, 0, newcomm);
  91. if (newrank == 0)
  92. {
  93. starpu_mpi_req req[2];
  94. starpu_mpi_issend(data[1], &req[0], 1, DATA1_TAG, newcomm);
  95. starpu_mpi_isend(data[0], &req[1], 1, DATA0_TAG, newcomm);
  96. starpu_mpi_wait(&req[0], MPI_STATUS_IGNORE);
  97. starpu_mpi_wait(&req[1], MPI_STATUS_IGNORE);
  98. }
  99. else if (newrank == 1)
  100. {
  101. int *xx;
  102. starpu_mpi_recv(data[0], 0, DATA0_TAG, newcomm, MPI_STATUS_IGNORE);
  103. starpu_data_acquire(data[0], STARPU_RW);
  104. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  105. starpu_data_release(data[0]);
  106. FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
  107. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  108. starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(int));
  109. starpu_mpi_data_register_comm(data[1], DATA1_TAG, 0, newcomm);
  110. starpu_mpi_recv(data[0], 0, DATA1_TAG, 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. }
  117. if (newrank == 0 || newrank == 1)
  118. {
  119. starpu_mpi_task_insert(newcomm, &mycodelet,
  120. STARPU_RW, data[0],
  121. STARPU_VALUE, &x, sizeof(x),
  122. STARPU_EXECUTE_ON_NODE, 1,
  123. 0);
  124. starpu_task_wait_for_all();
  125. starpu_data_unregister(data[0]);
  126. starpu_data_unregister(data[1]);
  127. }
  128. starpu_mpi_shutdown();
  129. MPI_Comm_free(&newcomm);
  130. MPI_Finalize();
  131. return 0;
  132. }