ring_sync.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 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. #ifdef STARPU_QUICK_CHECK
  19. # define NITER 32
  20. #elif !defined(STARPU_LONG_CHECK)
  21. # define NITER 256
  22. #else
  23. # define NITER 2048
  24. #endif
  25. #ifdef STARPU_USE_CUDA
  26. extern void increment_cuda(void *descr[], void *_args);
  27. #endif
  28. void increment_cpu(void *descr[], void *_args)
  29. {
  30. (void)_args;
  31. int *tokenptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  32. (*tokenptr)++;
  33. }
  34. static struct starpu_codelet increment_cl =
  35. {
  36. #ifdef STARPU_USE_CUDA
  37. .cuda_funcs = {increment_cuda},
  38. #endif
  39. .cpu_funcs = {increment_cpu},
  40. .nbuffers = 1,
  41. .modes = {STARPU_RW},
  42. .model = &starpu_perfmodel_nop,
  43. };
  44. void increment_token(starpu_data_handle_t token_handle)
  45. {
  46. struct starpu_task *task = starpu_task_create();
  47. task->cl = &increment_cl;
  48. task->handles[0] = token_handle;
  49. task->synchronous = 1;
  50. int ret = starpu_task_submit(task);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. int ret, rank, size;
  56. int mpi_init;
  57. int token = 42;
  58. starpu_data_handle_t token_handle;
  59. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  60. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  61. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  62. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  63. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  64. if (size < 2 || (starpu_cpu_worker_get_count() + starpu_cuda_worker_get_count() == 0))
  65. {
  66. if (rank == 0)
  67. {
  68. if (size < 2)
  69. FPRINTF(stderr, "We need at least 2 processes.\n");
  70. else
  71. FPRINTF(stderr, "We need at least 1 CPU or CUDA worker.\n");
  72. }
  73. starpu_mpi_shutdown();
  74. if (!mpi_init)
  75. MPI_Finalize();
  76. return STARPU_TEST_SKIPPED;
  77. }
  78. starpu_vector_data_register(&token_handle, 0, (uintptr_t)&token, 1, sizeof(token));
  79. int nloops = NITER;
  80. int loop;
  81. int last_loop = nloops - 1;
  82. int last_rank = size - 1;
  83. for (loop = 0; loop < nloops; loop++)
  84. {
  85. int tag = loop*size + rank;
  86. if (loop == 0 && rank == 0)
  87. {
  88. starpu_data_acquire(token_handle, STARPU_W);
  89. token = 0;
  90. FPRINTF(stdout, "Start with token value %d\n", token);
  91. starpu_data_release(token_handle);
  92. }
  93. else
  94. {
  95. MPI_Status status;
  96. starpu_mpi_recv(token_handle, (rank+size-1)%size, tag, MPI_COMM_WORLD, &status);
  97. }
  98. increment_token(token_handle);
  99. if (loop == last_loop && rank == last_rank)
  100. {
  101. starpu_data_acquire(token_handle, STARPU_R);
  102. FPRINTF(stdout, "Finished : token value %d\n", token);
  103. starpu_data_release(token_handle);
  104. }
  105. else
  106. {
  107. starpu_mpi_req req;
  108. MPI_Status status;
  109. starpu_mpi_issend(token_handle, &req, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  110. starpu_mpi_wait(&req, &status);
  111. }
  112. }
  113. starpu_data_unregister(token_handle);
  114. starpu_mpi_shutdown();
  115. if (!mpi_init)
  116. MPI_Finalize();
  117. #ifndef STARPU_SIMGRID
  118. if (rank == last_rank)
  119. {
  120. FPRINTF(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  121. STARPU_ASSERT(token == nloops*size);
  122. }
  123. #endif
  124. return 0;
  125. }