mpi_earlyrecv.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "helper.h"
  18. #include <unistd.h>
  19. int main(int argc, char **argv)
  20. {
  21. int ret, rank, size, i;
  22. starpu_data_handle_t tab_handle[4];
  23. int values[4];
  24. starpu_mpi_req request[2] = {NULL, NULL};
  25. int mpi_init;
  26. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  27. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  28. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  29. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  30. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  31. if (size%2 != 0)
  32. {
  33. FPRINTF_MPI(stderr, "We need a even number of processes.\n");
  34. starpu_mpi_shutdown();
  35. if (!mpi_init)
  36. MPI_Finalize();
  37. return STARPU_TEST_SKIPPED;
  38. }
  39. for(i=0 ; i<4 ; i++)
  40. {
  41. if (i<3 || rank%2)
  42. {
  43. // all data are registered on all nodes, but the 4th data which is not registered on the receiving node
  44. values[i] = (rank+1) * (i+1);
  45. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&values[i], sizeof(values[i]));
  46. starpu_mpi_data_register(tab_handle[i], i, rank);
  47. }
  48. }
  49. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  50. FPRINTF_MPI(stderr, "rank %d exchanging with rank %d\n", rank, other_rank);
  51. if (rank%2)
  52. {
  53. FPRINTF_MPI(stderr, "Sending values %d and %d to node %d\n", values[0], values[3], other_rank);
  54. // this data will be received as an early registered data
  55. starpu_mpi_isend(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  56. // this data will be received as an early UNregistered data
  57. starpu_mpi_isend(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  58. starpu_mpi_send(tab_handle[1], other_rank, 1, MPI_COMM_WORLD);
  59. starpu_mpi_recv(tab_handle[2], other_rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  60. }
  61. else
  62. {
  63. starpu_mpi_recv(tab_handle[1], other_rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  64. starpu_mpi_send(tab_handle[2], other_rank, 2, MPI_COMM_WORLD);
  65. // we register the data
  66. starpu_variable_data_register(&tab_handle[3], -1, (uintptr_t)NULL, sizeof(int));
  67. starpu_mpi_data_register(tab_handle[3], 3, rank);
  68. starpu_mpi_irecv(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  69. starpu_mpi_irecv(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  70. }
  71. int finished=0;
  72. while (!finished)
  73. {
  74. for(i=0 ; i<2 ; i++)
  75. {
  76. if (request[i])
  77. {
  78. int flag;
  79. MPI_Status status;
  80. starpu_mpi_test(&request[i], &flag, &status);
  81. if (flag)
  82. FPRINTF_MPI(stderr, "request[%d] = %d %p\n", i, flag, request[i]);
  83. }
  84. }
  85. finished = request[0] == NULL && request[1] == NULL;
  86. #ifdef STARPU_SIMGRID
  87. starpu_sleep(0.001);
  88. #endif
  89. }
  90. if (rank%2 == 0)
  91. {
  92. void *ptr0;
  93. void *ptr3;
  94. starpu_data_acquire(tab_handle[0], STARPU_RW);
  95. ptr0 = starpu_data_get_local_ptr(tab_handle[0]);
  96. starpu_data_release(tab_handle[0]);
  97. starpu_data_acquire(tab_handle[3], STARPU_RW);
  98. ptr3 = starpu_data_get_local_ptr(tab_handle[3]);
  99. starpu_data_release(tab_handle[3]);
  100. ret = (*((int *)ptr0) == (other_rank+1)*1) && (*((int *)ptr3) == (other_rank+1)*4);
  101. ret = !ret;
  102. FPRINTF_MPI(stderr, "[%s] Received values %d and %d from node %d\n", ret?"FAILURE":"SUCCESS", *((int *)ptr0), *((int *)ptr3), other_rank);
  103. }
  104. for(i=0 ; i<4 ; i++)
  105. starpu_data_unregister(tab_handle[i]);
  106. starpu_mpi_shutdown();
  107. if (!mpi_init)
  108. MPI_Finalize();
  109. return ret;
  110. }