mpi_redux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2015 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 = %d\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. MPI_Init(&argc, &argv);
  35. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  36. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  37. sum = ((size-1) * (size) / 2);
  38. ret = starpu_init(NULL);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  40. ret = starpu_mpi_init(NULL, NULL, 0);
  41. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  42. if (rank == 0)
  43. {
  44. int src;
  45. int received = 1;
  46. handles = malloc(size * sizeof(starpu_data_handle_t));
  47. for(src=1 ; src<size ; src++)
  48. {
  49. starpu_variable_data_register(&handles[src], -1, (uintptr_t)NULL, sizeof(int));
  50. starpu_mpi_irecv_detached(handles[src], src, 12+src, MPI_COMM_WORLD, callback, &received);
  51. }
  52. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  53. while (received != size)
  54. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  55. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  56. for(src=1 ; src<size ; src++)
  57. {
  58. void *ptr = starpu_data_get_local_ptr(handles[src]);
  59. value += *((int *)ptr);
  60. starpu_data_unregister(handles[src]);
  61. }
  62. for(src=1 ; src<size ; src++)
  63. {
  64. starpu_variable_data_register(&handles[src], STARPU_MAIN_RAM, (uintptr_t)&sum, sizeof(int));
  65. starpu_mpi_send(handles[src], src, 12+src, MPI_COMM_WORLD);
  66. starpu_data_unregister(handles[src]);
  67. }
  68. }
  69. else
  70. {
  71. value = rank;
  72. handles = malloc(sizeof(starpu_data_handle_t));
  73. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  74. starpu_mpi_send(handles[0], 0, 12+rank, MPI_COMM_WORLD);
  75. starpu_data_unregister_submit(handles[0]);
  76. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(int));
  77. starpu_mpi_recv(handles[0], 0, 12+rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  78. starpu_data_unregister(handles[0]);
  79. }
  80. starpu_task_wait_for_all();
  81. free(handles);
  82. starpu_mpi_shutdown();
  83. starpu_shutdown();
  84. MPI_Finalize();
  85. STARPU_ASSERT_MSG(sum == value, "Sum of first %d integers is %d, not %d\n", size-1, sum, value);
  86. return 0;
  87. }