driver.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017,2018 CNRS
  4. * Copyright (C) 2018,2020 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 <math.h>
  19. #include "helper.h"
  20. int main(int argc, char **argv)
  21. {
  22. int ret, rank, size, i;
  23. starpu_data_handle_t tab_handle[4];
  24. int values[4];
  25. starpu_mpi_req request[2] = {NULL, NULL};
  26. int mpi_init;
  27. setenv("STARPU_MPI_DRIVER_CALL_FREQUENCY", "1", 1);
  28. setenv("STARPU_MPI_DRIVER_TASK_FREQUENCY", "10", 1);
  29. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  30. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  31. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  32. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  33. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  34. if (size%2 != 0)
  35. {
  36. FPRINTF_MPI(stderr, "We need a even number of processes.\n");
  37. starpu_mpi_shutdown();
  38. if (!mpi_init)
  39. MPI_Finalize();
  40. return STARPU_TEST_SKIPPED;
  41. }
  42. for(i=0 ; i<4 ; i++)
  43. {
  44. if (i<3 || rank%2)
  45. {
  46. // all data are registered on all nodes, but the 4th data which is not registered on the receiving node
  47. values[i] = (rank+1) * (i+1);
  48. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&values[i], sizeof(values[i]));
  49. starpu_mpi_data_register(tab_handle[i], i, rank);
  50. }
  51. }
  52. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  53. FPRINTF_MPI(stderr, "rank %d exchanging with rank %d\n", rank, other_rank);
  54. if (rank%2)
  55. {
  56. FPRINTF_MPI(stderr, "Sending values %d and %d to node %d\n", values[0], values[3], other_rank);
  57. // this data will be received as an early registered data
  58. starpu_mpi_isend(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  59. // this data will be received as an early UNregistered data
  60. starpu_mpi_isend(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  61. starpu_mpi_send(tab_handle[1], other_rank, 1, MPI_COMM_WORLD);
  62. starpu_mpi_recv(tab_handle[2], other_rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  63. }
  64. else
  65. {
  66. starpu_mpi_recv(tab_handle[1], other_rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  67. starpu_mpi_send(tab_handle[2], other_rank, 2, MPI_COMM_WORLD);
  68. // we register the data
  69. starpu_variable_data_register(&tab_handle[3], -1, (uintptr_t)NULL, sizeof(int));
  70. starpu_mpi_data_register(tab_handle[3], 3, rank);
  71. starpu_mpi_irecv(tab_handle[3], &request[1], other_rank, 3, MPI_COMM_WORLD);
  72. starpu_mpi_irecv(tab_handle[0], &request[0], other_rank, 0, MPI_COMM_WORLD);
  73. }
  74. int finished=0;
  75. while (!finished)
  76. {
  77. for(i=0 ; i<2 ; i++)
  78. {
  79. if (request[i])
  80. {
  81. int flag;
  82. MPI_Status status;
  83. starpu_mpi_test(&request[i], &flag, &status);
  84. if (flag)
  85. FPRINTF_MPI(stderr, "request[%d] = %d %p\n", i, flag, request[i]);
  86. }
  87. }
  88. finished = request[0] == NULL && request[1] == NULL;
  89. #ifdef STARPU_SIMGRID
  90. starpu_sleep(0.001);
  91. #endif
  92. }
  93. if (rank%2 == 0)
  94. {
  95. void *ptr0;
  96. void *ptr3;
  97. starpu_data_acquire(tab_handle[0], STARPU_RW);
  98. ptr0 = starpu_data_get_local_ptr(tab_handle[0]);
  99. starpu_data_release(tab_handle[0]);
  100. starpu_data_acquire(tab_handle[3], STARPU_RW);
  101. ptr3 = starpu_data_get_local_ptr(tab_handle[3]);
  102. starpu_data_release(tab_handle[3]);
  103. ret = (*((int *)ptr0) == (other_rank+1)*1) && (*((int *)ptr3) == (other_rank+1)*4);
  104. ret = !ret;
  105. FPRINTF_MPI(stderr, "[%s] Received values %d and %d from node %d\n", ret?"FAILURE":"SUCCESS", *((int *)ptr0), *((int *)ptr3), other_rank);
  106. }
  107. for(i=0 ; i<4 ; i++)
  108. starpu_data_unregister(tab_handle[i]);
  109. starpu_mpi_shutdown();
  110. if (!mpi_init)
  111. MPI_Finalize();
  112. return 0;
  113. }