mpi_earlyrecv2.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 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. //#define NB 1000
  21. #define NB 10
  22. int main(int argc, char **argv)
  23. {
  24. int ret, rank, size, i;
  25. starpu_data_handle_t tab_handle[NB];
  26. int value[NB];
  27. MPI_Init(NULL, NULL);
  28. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  29. MPI_Comm_size(MPI_COMM_WORLD, &size);
  30. if (size%2 != 0)
  31. {
  32. if (rank == 0)
  33. FPRINTF(stderr, "We need a even number of processes.\n");
  34. MPI_Finalize();
  35. return STARPU_TEST_SKIPPED;
  36. }
  37. ret = starpu_init(NULL);
  38. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  39. ret = starpu_mpi_init(NULL, NULL, 0);
  40. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  41. for(i=0 ; i<NB ; i++)
  42. {
  43. value[i]=i*rank;
  44. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  45. starpu_data_set_tag(tab_handle[i], i);
  46. }
  47. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  48. if (rank%2)
  49. {
  50. starpu_mpi_send(tab_handle[0], other_rank, 0, MPI_COMM_WORLD);
  51. starpu_mpi_send(tab_handle[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  52. for(i=1 ; i<NB-1 ; i++)
  53. {
  54. starpu_mpi_send(tab_handle[i], other_rank, i, MPI_COMM_WORLD);
  55. }
  56. }
  57. else
  58. {
  59. starpu_mpi_req req[NB];
  60. memset(req, 0, NB*sizeof(starpu_mpi_req));
  61. starpu_mpi_irecv(tab_handle[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  62. STARPU_ASSERT(req[0] != NULL);
  63. // We sleep to make sure that the data for the tag 9 will be received before the recv is posted
  64. usleep(2000000);
  65. for(i=1 ; i<NB ; i++)
  66. {
  67. starpu_mpi_irecv(tab_handle[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  68. STARPU_ASSERT(req[i] != NULL);
  69. }
  70. for(i=0 ; i<NB ; i++)
  71. {
  72. starpu_mpi_wait(&req[i], NULL);
  73. int *rvalue = (int *)starpu_data_get_local_ptr(tab_handle[i]);
  74. STARPU_ASSERT_MSG(*rvalue==i*other_rank, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  75. }
  76. }
  77. for(i=0 ; i<NB ; i++)
  78. starpu_data_unregister(tab_handle[i]);
  79. starpu_mpi_shutdown();
  80. starpu_shutdown();
  81. MPI_Finalize();
  82. return 0;
  83. }