mpi_isend_detached.c 2.7 KB

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