mpi_earlyrecv2_detached.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  23. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  24. void callback(void *arg STARPU_ATTRIBUTE_UNUSED)
  25. {
  26. unsigned *received = arg;
  27. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  28. *received = *received + 1;
  29. FPRINTF_MPI("Requests %d received\n", *received);
  30. STARPU_PTHREAD_COND_SIGNAL(&cond);
  31. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  32. }
  33. int main(int argc, char **argv)
  34. {
  35. int ret, rank, size, i;
  36. starpu_data_handle_t tab_handle[NB];
  37. int value[NB];
  38. MPI_Init(NULL, NULL);
  39. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  40. MPI_Comm_size(MPI_COMM_WORLD, &size);
  41. if (size%2 != 0)
  42. {
  43. if (rank == 0)
  44. FPRINTF(stderr, "We need a even number of processes.\n");
  45. MPI_Finalize();
  46. return STARPU_TEST_SKIPPED;
  47. }
  48. ret = starpu_init(NULL);
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  50. ret = starpu_mpi_init(NULL, NULL, 0);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  52. for(i=0 ; i<NB ; i++)
  53. {
  54. value[i]=i*rank;
  55. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  56. starpu_data_set_tag(tab_handle[i], i);
  57. }
  58. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  59. FPRINTF_MPI("Exchanging with rank %d\n", other_rank);
  60. if (rank%2)
  61. {
  62. starpu_mpi_send(tab_handle[0], other_rank, 0, MPI_COMM_WORLD);
  63. starpu_mpi_send(tab_handle[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  64. for(i=1 ; i<NB-1 ; i++)
  65. {
  66. starpu_mpi_send(tab_handle[i], other_rank, i, MPI_COMM_WORLD);
  67. }
  68. }
  69. else
  70. {
  71. int received = 0;
  72. starpu_mpi_irecv_detached(tab_handle[0], other_rank, 0, MPI_COMM_WORLD, callback, &received);
  73. usleep(2000000);
  74. // We sleep to make sure that the data for the tag 9 will be received before the recv is posted
  75. for(i=1 ; i<NB ; i++)
  76. {
  77. starpu_mpi_irecv_detached(tab_handle[i], other_rank, i, MPI_COMM_WORLD, callback, &received);
  78. }
  79. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  80. while (received != NB)
  81. {
  82. FPRINTF_MPI("Received %d messages\n", received);
  83. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  84. }
  85. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  86. for(i=0 ; i<NB ; i++)
  87. {
  88. int *rvalue = (int *)starpu_data_get_local_ptr(tab_handle[i]);
  89. STARPU_ASSERT_MSG(*rvalue==i*other_rank, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  90. }
  91. }
  92. for(i=0 ; i<NB ; i++)
  93. starpu_data_unregister(tab_handle[i]);
  94. starpu_mpi_shutdown();
  95. starpu_shutdown();
  96. MPI_Finalize();
  97. return 0;
  98. }