sync.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2017 CNRS
  4. * Copyright (C) 2015,2017,2018 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_mpi.h>
  18. #include "helper.h"
  19. int main(int argc, char **argv)
  20. {
  21. int size, x=789;
  22. int rank, other_rank;
  23. int ret;
  24. starpu_data_handle_t data[2];
  25. int mpi_init;
  26. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  27. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  28. MPI_Comm_size(MPI_COMM_WORLD, &size);
  29. if (size % 2)
  30. {
  31. FPRINTF(stderr, "We need a even number of processes.\n");
  32. if (!mpi_init)
  33. MPI_Finalize();
  34. return STARPU_TEST_SKIPPED;
  35. }
  36. other_rank = rank%2 == 0 ? rank+1 : rank-1;
  37. FPRINTF_MPI(stderr, "rank %d exchanging with rank %d\n", rank, other_rank);
  38. if (rank % 2)
  39. {
  40. MPI_Send(&rank, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD);
  41. FPRINTF(stderr, "[%d] sending %d\n", rank, rank);
  42. }
  43. else
  44. {
  45. MPI_Recv(&x, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  46. FPRINTF(stderr, "[%d] received %d\n", rank, x);
  47. }
  48. ret = starpu_mpi_init_conf(NULL, NULL, 0, MPI_COMM_WORLD, NULL);
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  50. if (rank % 2)
  51. {
  52. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  53. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  54. starpu_mpi_data_register(data[1], 22, 0);
  55. }
  56. else
  57. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(unsigned));
  58. starpu_mpi_data_register(data[0], 12, 0);
  59. if (rank % 2)
  60. {
  61. starpu_mpi_req req;
  62. starpu_mpi_issend(data[1], &req, other_rank, 22, MPI_COMM_WORLD);
  63. starpu_mpi_send(data[0], other_rank, 12, MPI_COMM_WORLD);
  64. starpu_mpi_wait(&req, MPI_STATUS_IGNORE);
  65. }
  66. else
  67. {
  68. int *xx;
  69. starpu_mpi_recv(data[0], other_rank, 12, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  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_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(unsigned));
  74. starpu_mpi_data_register(data[1], 22, 0);
  75. starpu_mpi_recv(data[0], other_rank, 22, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  76. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  77. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  78. }
  79. starpu_data_unregister(data[0]);
  80. starpu_data_unregister(data[1]);
  81. starpu_mpi_shutdown();
  82. if (!mpi_init)
  83. MPI_Finalize();
  84. return 0;
  85. }