sync.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 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. 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_init(NULL);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  49. ret = starpu_mpi_init(NULL, NULL, 0);
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  51. if (rank % 2)
  52. {
  53. starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  54. starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
  55. starpu_mpi_data_register(data[1], 22, 0);
  56. }
  57. else
  58. starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(unsigned));
  59. starpu_mpi_data_register(data[0], 12, 0);
  60. if (rank % 2)
  61. {
  62. starpu_mpi_req req;
  63. starpu_mpi_issend(data[1], &req, other_rank, 22, MPI_COMM_WORLD);
  64. starpu_mpi_send(data[0], other_rank, 12, MPI_COMM_WORLD);
  65. starpu_mpi_wait(&req, MPI_STATUS_IGNORE);
  66. }
  67. else
  68. {
  69. int *xx;
  70. starpu_mpi_recv(data[0], other_rank, 12, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  71. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  72. FPRINTF_MPI(stderr, "received %d\n", *xx);
  73. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  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. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  78. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  79. }
  80. starpu_data_unregister(data[0]);
  81. starpu_data_unregister(data[1]);
  82. starpu_mpi_shutdown();
  83. starpu_shutdown();
  84. if (!mpi_init)
  85. MPI_Finalize();
  86. return 0;
  87. }