mpi_irecv_detached.c 2.0 KB

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