sync.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. MPI_INIT_THREAD_real(&argc, &argv, MPI_THREAD_SERIALIZED);
  25. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  26. MPI_Comm_size(MPI_COMM_WORLD, &size);
  27. if (size % 2)
  28. {
  29. FPRINTF(stderr, "We need a even number of processes.\n");
  30. MPI_Finalize();
  31. return STARPU_TEST_SKIPPED;
  32. }
  33. other_rank = rank%2 == 0 ? rank+1 : rank-1;
  34. FPRINTF_MPI(stderr, "rank %d exchanging with rank %d\n", rank, other_rank);
  35. if (rank % 2)
  36. {
  37. MPI_Send(&rank, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD);
  38. FPRINTF(stderr, "[%d] sending %d\n", rank, rank);
  39. }
  40. else
  41. {
  42. MPI_Recv(&x, 1, MPI_INT, other_rank, 10, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  43. FPRINTF(stderr, "[%d] received %d\n", rank, x);
  44. }
  45. ret = starpu_init(NULL);
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  47. ret = starpu_mpi_init(NULL, NULL, 0);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  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. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  70. FPRINTF_MPI(stderr, "received %d\n", *xx);
  71. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  72. starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(unsigned));
  73. starpu_mpi_data_register(data[1], 22, 0);
  74. starpu_mpi_recv(data[0], other_rank, 22, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  75. xx = (int *)starpu_variable_get_local_ptr(data[0]);
  76. STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
  77. }
  78. starpu_data_unregister(data[0]);
  79. starpu_data_unregister(data[1]);
  80. starpu_mpi_shutdown();
  81. starpu_shutdown();
  82. MPI_Finalize();
  83. return 0;
  84. }