sync.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-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. #include <starpu_mpi.h>
  17. #include "helper.h"
  18. int main(int argc, char **argv)
  19. {
  20. int size, x=789;
  21. int rank, other_rank;
  22. int ret;
  23. starpu_data_handle_t data[2];
  24. int mpi_init;
  25. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  26. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27. MPI_Comm_size(MPI_COMM_WORLD, &size);
  28. if (size % 2)
  29. {
  30. FPRINTF(stderr, "We need a even number of processes.\n");
  31. if (!mpi_init)
  32. MPI_Finalize();
  33. return STARPU_TEST_SKIPPED;
  34. }
  35. other_rank = rank%2 == 0 ? rank+1 : rank-1;
  36. FPRINTF_MPI(stderr, "rank %d exchanging with rank %d\n", rank, other_rank);
  37. if (rank % 2)
  38. {
  39. MPI_Send(&rank, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD);
  40. FPRINTF(stderr, "[%d] sending %d\n", rank, rank);
  41. }
  42. else
  43. {
  44. MPI_Recv(&x, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  45. FPRINTF(stderr, "[%d] received %d\n", rank, x);
  46. }
  47. ret = starpu_mpi_init_conf(NULL, NULL, 0, MPI_COMM_WORLD, NULL);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  49. if (rank % 2)
  50. {
  51. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  52. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  53. starpu_mpi_data_register(data[1], 22, 0);
  54. }
  55. else
  56. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(unsigned));
  57. starpu_mpi_data_register(data[0], 12, 0);
  58. if (rank % 2)
  59. {
  60. starpu_mpi_req req;
  61. starpu_mpi_issend(data[1], &req, other_rank, 22, MPI_COMM_WORLD);
  62. starpu_mpi_send(data[0], other_rank, 12, MPI_COMM_WORLD);
  63. starpu_mpi_wait(&req, MPI_STATUS_IGNORE);
  64. }
  65. else
  66. {
  67. int *xx;
  68. starpu_mpi_recv(data[0], other_rank, 12, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  69. starpu_data_acquire(data[0], STARPU_R);
  70. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  71. FPRINTF_MPI(stderr, "received %d\n", *xx);
  72. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  73. starpu_data_release(data[0]);
  74. starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(unsigned));
  75. starpu_mpi_data_register(data[1], 22, 0);
  76. starpu_mpi_recv(data[0], other_rank, 22, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  77. starpu_data_acquire(data[0], STARPU_R);
  78. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  79. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  80. starpu_data_release(data[0]);
  81. }
  82. starpu_data_unregister(data[0]);
  83. starpu_data_unregister(data[1]);
  84. starpu_mpi_shutdown();
  85. if (!mpi_init)
  86. MPI_Finalize();
  87. return 0;
  88. }