mpi_isend_detached.c 2.2 KB

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