mpi_redux.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2021 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. /* This test does a manual reduction: all ranks send a number to the rank 0,
  17. * the rank 0 sums these numbers and sends back the result to all ranks. */
  18. #include <starpu_mpi.h>
  19. #include "helper.h"
  20. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  21. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  22. void callback(void *arg)
  23. {
  24. unsigned *received = arg;
  25. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  26. *received = *received + 1;
  27. FPRINTF_MPI(stderr, "received = %u\n", *received);
  28. STARPU_PTHREAD_COND_SIGNAL(&cond);
  29. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  30. }
  31. int main(int argc, char **argv)
  32. {
  33. int ret, rank, size, sum;
  34. int value=0;
  35. starpu_data_handle_t *handles;
  36. int mpi_init;
  37. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  38. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  40. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  41. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  42. sum = ((size-1) * (size) / 2);
  43. if (rank == 0)
  44. {
  45. int src;
  46. int received = 1;
  47. handles = malloc(size * sizeof(starpu_data_handle_t));
  48. for(src=1 ; src<size ; src++)
  49. {
  50. starpu_variable_data_register(&handles[src], -1, (uintptr_t)NULL, sizeof(int));
  51. starpu_mpi_irecv_detached(handles[src], src, 12+src, MPI_COMM_WORLD, callback, &received);
  52. }
  53. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  54. while (received != size)
  55. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  56. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  57. for(src=1 ; src<size ; src++)
  58. {
  59. starpu_data_acquire(handles[src], STARPU_R);
  60. void *ptr = starpu_data_get_local_ptr(handles[src]);
  61. value += *((int *)ptr);
  62. starpu_data_release(handles[src]);
  63. starpu_data_unregister(handles[src]);
  64. }
  65. for(src=1 ; src<size ; src++)
  66. {
  67. starpu_variable_data_register(&handles[src], STARPU_MAIN_RAM, (uintptr_t)&sum, sizeof(int));
  68. starpu_mpi_send(handles[src], src, 12+src, MPI_COMM_WORLD);
  69. starpu_data_unregister(handles[src]);
  70. }
  71. }
  72. else
  73. {
  74. value = rank;
  75. handles = malloc(sizeof(starpu_data_handle_t));
  76. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  77. starpu_mpi_send(handles[0], 0, 12+rank, MPI_COMM_WORLD);
  78. starpu_data_unregister_submit(handles[0]);
  79. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  80. starpu_mpi_recv(handles[0], 0, 12+rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  81. starpu_data_unregister(handles[0]);
  82. }
  83. starpu_task_wait_for_all();
  84. free(handles);
  85. starpu_mpi_shutdown();
  86. if (!mpi_init)
  87. MPI_Finalize();
  88. STARPU_ASSERT_MSG(sum == value, "Sum of first %d integers is %d, not %d\n", size-1, sum, value);
  89. return 0;
  90. }