mpi_isend_detached.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012,2014-2017 Université de Bordeaux
  4. * Copyright (C) 2010-2013,2015-2017 CNRS
  5. * Copyright (C) 2013 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include <common/thread.h>
  20. #include "helper.h"
  21. #ifdef STARPU_QUICK_CHECK
  22. # define NITER 16
  23. #elif !defined(STARPU_LONG_CHECK)
  24. # define NITER 256
  25. #else
  26. # define NITER 2048
  27. #endif
  28. #define SIZE 16
  29. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  30. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  31. void callback(void *arg)
  32. {
  33. unsigned *completed = arg;
  34. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  35. *completed = 1;
  36. STARPU_PTHREAD_COND_SIGNAL(&cond);
  37. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. int ret, rank, size;
  42. float *tab;
  43. starpu_data_handle_t tab_handle;
  44. int mpi_init;
  45. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  46. ret = starpu_init(NULL);
  47. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  48. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  50. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  51. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  52. if (size%2 != 0)
  53. {
  54. if (rank == 0)
  55. FPRINTF(stderr, "We need a even number of processes.\n");
  56. starpu_mpi_shutdown();
  57. starpu_shutdown();
  58. if (!mpi_init)
  59. MPI_Finalize();
  60. return STARPU_TEST_SKIPPED;
  61. }
  62. tab = calloc(SIZE, sizeof(float));
  63. starpu_vector_data_register(&tab_handle, STARPU_MAIN_RAM, (uintptr_t)tab, SIZE, sizeof(float));
  64. int nloops = NITER;
  65. int loop;
  66. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  67. for (loop = 0; loop < nloops; loop++)
  68. {
  69. if ((loop % 2) == (rank%2))
  70. {
  71. int sent = 0;
  72. starpu_mpi_isend_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &sent);
  73. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  74. while (!sent)
  75. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  76. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  77. }
  78. else
  79. {
  80. int received = 0;
  81. starpu_mpi_irecv_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &received);
  82. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  83. while (!received)
  84. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  85. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  86. }
  87. }
  88. starpu_data_unregister(tab_handle);
  89. free(tab);
  90. starpu_mpi_shutdown();
  91. starpu_shutdown();
  92. if (!mpi_init)
  93. MPI_Finalize();
  94. return 0;
  95. }