mpi_irecv_detached.c 2.0 KB

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