mpi_redux.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2015, 2016, 2017 CNRS
  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_init(NULL);
  37. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  38. ret = starpu_mpi_init(NULL, NULL, mpi_init);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  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. void *ptr = starpu_data_get_local_ptr(handles[src]);
  60. value += *((int *)ptr);
  61. starpu_data_unregister(handles[src]);
  62. }
  63. for(src=1 ; src<size ; src++)
  64. {
  65. starpu_variable_data_register(&handles[src], STARPU_MAIN_RAM, (uintptr_t)&sum, sizeof(int));
  66. starpu_mpi_send(handles[src], src, 12+src, MPI_COMM_WORLD);
  67. starpu_data_unregister(handles[src]);
  68. }
  69. }
  70. else
  71. {
  72. value = rank;
  73. handles = malloc(sizeof(starpu_data_handle_t));
  74. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  75. starpu_mpi_send(handles[0], 0, 12+rank, MPI_COMM_WORLD);
  76. starpu_data_unregister_submit(handles[0]);
  77. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  78. starpu_mpi_recv(handles[0], 0, 12+rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  79. starpu_data_unregister(handles[0]);
  80. }
  81. starpu_task_wait_for_all();
  82. free(handles);
  83. starpu_mpi_shutdown();
  84. starpu_shutdown();
  85. if (!mpi_init)
  86. MPI_Finalize();
  87. STARPU_ASSERT_MSG(sum == value, "Sum of first %d integers is %d, not %d\n", size-1, sum, value);
  88. return 0;
  89. }