ring_sync.c 3.7 KB

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