mpi_irecv_detached.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. #define NITER 2048
  20. #define SIZE 16
  21. float *tab;
  22. starpu_data_handle tab_handle;
  23. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  24. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  25. void callback(void *arg __attribute__((unused)))
  26. {
  27. unsigned *received = arg;
  28. PTHREAD_MUTEX_LOCK(&mutex);
  29. *received = 1;
  30. PTHREAD_COND_SIGNAL(&cond);
  31. PTHREAD_MUTEX_UNLOCK(&mutex);
  32. }
  33. int main(int argc, char **argv)
  34. {
  35. MPI_Init(NULL, NULL);
  36. int rank, size;
  37. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  38. MPI_Comm_size(MPI_COMM_WORLD, &size);
  39. if (size != 2)
  40. {
  41. if (rank == 0)
  42. fprintf(stderr, "We need exactly 2 processes.\n");
  43. MPI_Finalize();
  44. return 0;
  45. }
  46. starpu_init(NULL);
  47. starpu_mpi_initialize();
  48. tab = malloc(SIZE*sizeof(float));
  49. starpu_vector_data_register(&tab_handle, 0, (uintptr_t)tab, SIZE, sizeof(float));
  50. unsigned nloops = NITER;
  51. unsigned loop;
  52. int other_rank = (rank + 1)%2;
  53. for (loop = 0; loop < nloops; loop++)
  54. {
  55. if (rank == 0)
  56. {
  57. starpu_mpi_send(tab_handle, other_rank, loop, MPI_COMM_WORLD);
  58. }
  59. else {
  60. MPI_Status status;
  61. int received = 0;
  62. starpu_mpi_irecv_detached(tab_handle, other_rank, loop, MPI_COMM_WORLD, callback, &received);
  63. PTHREAD_MUTEX_LOCK(&mutex);
  64. while (!received)
  65. PTHREAD_COND_WAIT(&cond, &mutex);
  66. PTHREAD_MUTEX_UNLOCK(&mutex);
  67. }
  68. }
  69. starpu_mpi_shutdown();
  70. starpu_shutdown();
  71. MPI_Finalize();
  72. return 0;
  73. }