mpi_irecv_detached.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "helper.h"
  20. #define NITER 2048
  21. #define SIZE 16
  22. float *tab;
  23. starpu_data_handle_t tab_handle;
  24. static _starpu_pthread_mutex_t mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  25. static _starpu_pthread_cond_t cond = _STARPU_PTHREAD_COND_INITIALIZER;
  26. void callback(void *arg __attribute__((unused)))
  27. {
  28. unsigned *received = arg;
  29. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  30. *received = 1;
  31. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  32. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  33. }
  34. int main(int argc, char **argv)
  35. {
  36. int ret, rank, size;
  37. MPI_Init(NULL, NULL);
  38. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  39. MPI_Comm_size(MPI_COMM_WORLD, &size);
  40. if (size%2 != 0)
  41. {
  42. if (rank == 0)
  43. FPRINTF(stderr, "We need a even number of processes.\n");
  44. MPI_Finalize();
  45. return STARPU_TEST_SKIPPED;
  46. }
  47. ret = starpu_init(NULL);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  49. ret = starpu_mpi_init(NULL, NULL, 0);
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  51. tab = malloc(SIZE*sizeof(float));
  52. starpu_vector_data_register(&tab_handle, 0, (uintptr_t)tab, SIZE, sizeof(float));
  53. unsigned nloops = NITER;
  54. unsigned loop;
  55. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  56. for (loop = 0; loop < nloops; loop++)
  57. {
  58. if ((loop % 2) == (rank%2))
  59. {
  60. starpu_mpi_send(tab_handle, other_rank, loop, MPI_COMM_WORLD);
  61. }
  62. else
  63. {
  64. int received = 0;
  65. starpu_mpi_irecv_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &received);
  66. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  67. while (!received)
  68. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  69. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  70. }
  71. }
  72. starpu_mpi_shutdown();
  73. starpu_shutdown();
  74. MPI_Finalize();
  75. return 0;
  76. }