mpi_isend_detached.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 <common/utils.h>
  19. #include <pthread.h>
  20. #include "helper.h"
  21. #define NITER 2048
  22. #define SIZE 16
  23. static _starpu_pthread_mutex_t mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  24. static _starpu_pthread_cond_t cond = _STARPU_PTHREAD_COND_INITIALIZER;
  25. void callback(void *arg)
  26. {
  27. unsigned *completed = arg;
  28. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  29. *completed = 1;
  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;
  36. float *tab;
  37. starpu_data_handle_t tab_handle;
  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. tab = malloc(SIZE*sizeof(float));
  53. starpu_vector_data_register(&tab_handle, 0, (uintptr_t)tab, SIZE, sizeof(float));
  54. unsigned nloops = NITER;
  55. unsigned loop;
  56. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  57. for (loop = 0; loop < nloops; loop++)
  58. {
  59. if ((loop % 2) == (rank%2))
  60. {
  61. int sent = 0;
  62. starpu_mpi_isend_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &sent);
  63. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  64. while (!sent)
  65. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  66. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  67. }
  68. else
  69. {
  70. int received = 0;
  71. starpu_mpi_irecv_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &received);
  72. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  73. while (!received)
  74. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  75. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  76. }
  77. }
  78. starpu_mpi_shutdown();
  79. starpu_shutdown();
  80. MPI_Finalize();
  81. return 0;
  82. }