mpi_isend_detached.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012, 2014-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2016, 2017 CNRS
  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 <common/thread.h>
  19. #include "helper.h"
  20. #ifdef STARPU_QUICK_CHECK
  21. # define NITER 16
  22. #elif !defined(STARPU_LONG_CHECK)
  23. # define NITER 256
  24. #else
  25. # define NITER 2048
  26. #endif
  27. #define SIZE 16
  28. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  29. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  30. void callback(void *arg)
  31. {
  32. unsigned *completed = arg;
  33. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  34. *completed = 1;
  35. STARPU_PTHREAD_COND_SIGNAL(&cond);
  36. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. int ret, rank, size;
  41. float *tab;
  42. starpu_data_handle_t tab_handle;
  43. int mpi_init;
  44. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  45. ret = starpu_init(NULL);
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  47. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  49. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  50. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  51. if (size%2 != 0)
  52. {
  53. if (rank == 0)
  54. FPRINTF(stderr, "We need a even number of processes.\n");
  55. starpu_mpi_shutdown();
  56. starpu_shutdown();
  57. if (!mpi_init)
  58. MPI_Finalize();
  59. return STARPU_TEST_SKIPPED;
  60. }
  61. tab = calloc(SIZE, sizeof(float));
  62. starpu_vector_data_register(&tab_handle, STARPU_MAIN_RAM, (uintptr_t)tab, SIZE, sizeof(float));
  63. int nloops = NITER;
  64. int loop;
  65. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  66. for (loop = 0; loop < nloops; loop++)
  67. {
  68. if ((loop % 2) == (rank%2))
  69. {
  70. int sent = 0;
  71. starpu_mpi_isend_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &sent);
  72. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  73. while (!sent)
  74. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  75. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  76. }
  77. else
  78. {
  79. int received = 0;
  80. starpu_mpi_irecv_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &received);
  81. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  82. while (!received)
  83. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  84. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  85. }
  86. }
  87. starpu_data_unregister(tab_handle);
  88. free(tab);
  89. starpu_mpi_shutdown();
  90. starpu_shutdown();
  91. if (!mpi_init)
  92. MPI_Finalize();
  93. return 0;
  94. }