mpi_redux.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 "helper.h"
  18. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  19. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  20. void callback(void *arg)
  21. {
  22. unsigned *received = arg;
  23. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  24. *received = *received + 1;
  25. FPRINTF_MPI(stderr, "received = %u\n", *received);
  26. STARPU_PTHREAD_COND_SIGNAL(&cond);
  27. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. int ret, rank, size, sum;
  32. int value=0;
  33. starpu_data_handle_t *handles;
  34. int mpi_init;
  35. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  36. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  37. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  38. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  39. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  40. sum = ((size-1) * (size) / 2);
  41. if (rank == 0)
  42. {
  43. int src;
  44. int received = 1;
  45. handles = malloc(size * sizeof(starpu_data_handle_t));
  46. for(src=1 ; src<size ; src++)
  47. {
  48. starpu_variable_data_register(&handles[src], -1, (uintptr_t)NULL, sizeof(int));
  49. starpu_mpi_irecv_detached(handles[src], src, 12+src, MPI_COMM_WORLD, callback, &received);
  50. }
  51. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  52. while (received != size)
  53. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  54. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  55. for(src=1 ; src<size ; src++)
  56. {
  57. void *ptr = starpu_data_get_local_ptr(handles[src]);
  58. value += *((int *)ptr);
  59. starpu_data_unregister(handles[src]);
  60. }
  61. for(src=1 ; src<size ; src++)
  62. {
  63. starpu_variable_data_register(&handles[src], STARPU_MAIN_RAM, (uintptr_t)&sum, sizeof(int));
  64. starpu_mpi_send(handles[src], src, 12+src, MPI_COMM_WORLD);
  65. starpu_data_unregister(handles[src]);
  66. }
  67. }
  68. else
  69. {
  70. value = rank;
  71. handles = malloc(sizeof(starpu_data_handle_t));
  72. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  73. starpu_mpi_send(handles[0], 0, 12+rank, MPI_COMM_WORLD);
  74. starpu_data_unregister_submit(handles[0]);
  75. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  76. starpu_mpi_recv(handles[0], 0, 12+rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  77. starpu_data_unregister(handles[0]);
  78. }
  79. starpu_task_wait_for_all();
  80. free(handles);
  81. starpu_mpi_shutdown();
  82. if (!mpi_init)
  83. MPI_Finalize();
  84. STARPU_ASSERT_MSG(sum == value, "Sum of first %d integers is %d, not %d\n", size-1, sum, value);
  85. return 0;
  86. }