mpi_earlyrecv.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  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. #include <starpu_mpi.h>
  18. #include "helper.h"
  19. #include <unistd.h>
  20. int main(int argc, char **argv)
  21. {
  22. int ret, rank, size, i;
  23. starpu_data_handle_t tab_handle[4];
  24. starpu_mpi_req request[2] = {NULL, NULL};
  25. MPI_Init(NULL, NULL);
  26. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27. MPI_Comm_size(MPI_COMM_WORLD, &size);
  28. if (size%2 != 0)
  29. {
  30. if (rank == 0)
  31. FPRINTF(stderr, "We need a even number of processes.\n");
  32. MPI_Finalize();
  33. return STARPU_TEST_SKIPPED;
  34. }
  35. ret = starpu_init(NULL);
  36. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  37. ret = starpu_mpi_init(NULL, NULL, 0);
  38. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  39. for(i=0 ; i<4 ; i++)
  40. {
  41. if (i<3 || rank%2)
  42. {
  43. // all data are registered on all nodes, bu the 4th data which is not registered on the receiving node
  44. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  45. starpu_mpi_data_register(tab_handle[i], i, rank);
  46. }
  47. }
  48. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  49. FPRINTF_MPI("rank %d exchanging with rank %d\n", rank, other_rank);
  50. if (rank%2)
  51. {
  52. // this data will be received as an early registered data
  53. starpu_mpi_isend(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  54. // this data will be received as an early UNregistered data
  55. starpu_mpi_isend(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  56. starpu_mpi_send(tab_handle[1], other_rank, 1, MPI_COMM_WORLD);
  57. starpu_mpi_recv(tab_handle[2], other_rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  58. }
  59. else
  60. {
  61. starpu_mpi_recv(tab_handle[1], other_rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  62. starpu_mpi_send(tab_handle[2], other_rank, 2, MPI_COMM_WORLD);
  63. // we register the data
  64. starpu_variable_data_register(&tab_handle[3], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(int));
  65. starpu_mpi_data_register(tab_handle[3], 3, rank);
  66. starpu_mpi_irecv(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  67. starpu_mpi_irecv(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  68. }
  69. int finished=0;
  70. while (!finished)
  71. {
  72. for(i=0 ; i<2 ; i++)
  73. {
  74. if (request[i])
  75. {
  76. int flag;
  77. MPI_Status status;
  78. starpu_mpi_test(&request[i], &flag, &status);
  79. if (flag)
  80. FPRINTF_MPI("request[%d] = %d %p\n", i, flag, request[i]);
  81. }
  82. }
  83. finished = request[0] == NULL && request[1] == NULL;
  84. }
  85. for(i=0 ; i<4 ; i++)
  86. starpu_data_unregister(tab_handle[i]);
  87. starpu_mpi_shutdown();
  88. starpu_shutdown();
  89. MPI_Finalize();
  90. return 0;
  91. }