ring_async.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2013-2018 Université de Bordeaux
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2010-2013,2015-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include "helper.h"
  20. #ifdef STARPU_QUICK_CHECK
  21. # define NITER 32
  22. #elif !defined(STARPU_LONG_CHECK)
  23. # define NITER 256
  24. #else
  25. # define NITER 2048
  26. #endif
  27. #ifdef STARPU_USE_CUDA
  28. extern void increment_cuda(void *descr[], void *_args);
  29. #endif
  30. void increment_cpu(void *descr[], void *_args)
  31. {
  32. (void)_args;
  33. int *tokenptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  34. (*tokenptr)++;
  35. }
  36. static struct starpu_codelet increment_cl =
  37. {
  38. #ifdef STARPU_USE_CUDA
  39. .cuda_funcs = {increment_cuda},
  40. #endif
  41. .cpu_funcs = {increment_cpu},
  42. .nbuffers = 1,
  43. .modes = {STARPU_RW},
  44. .model = &starpu_perfmodel_nop,
  45. };
  46. void increment_token(starpu_data_handle_t token_handle)
  47. {
  48. struct starpu_task *task = starpu_task_create();
  49. task->cl = &increment_cl;
  50. task->handles[0] = token_handle;
  51. task->synchronous = 1;
  52. int ret = starpu_task_submit(task);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. int ret, rank, size;
  58. int mpi_init;
  59. int token = 42;
  60. starpu_data_handle_t token_handle;
  61. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  62. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  64. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  65. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  66. if (size < 2 || (starpu_cpu_worker_get_count() + starpu_cuda_worker_get_count() == 0))
  67. {
  68. if (rank == 0)
  69. {
  70. if (size < 2)
  71. FPRINTF(stderr, "We need at least 2 processes.\n");
  72. else
  73. FPRINTF(stderr, "We need at least 1 CPU or CUDA worker.\n");
  74. }
  75. starpu_mpi_shutdown();
  76. if (!mpi_init)
  77. MPI_Finalize();
  78. return STARPU_TEST_SKIPPED;
  79. }
  80. starpu_vector_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, 1, sizeof(token));
  81. int nloops = NITER;
  82. int loop;
  83. int last_loop = nloops - 1;
  84. int last_rank = size - 1;
  85. for (loop = 0; loop < nloops; loop++)
  86. {
  87. int tag = loop*size + rank;
  88. if (loop == 0 && rank == 0)
  89. {
  90. starpu_data_acquire(token_handle, STARPU_W);
  91. token = 0;
  92. FPRINTF(stdout, "Start with token value %d\n", token);
  93. starpu_data_release(token_handle);
  94. }
  95. else
  96. {
  97. MPI_Status status;
  98. starpu_mpi_req req;
  99. starpu_mpi_irecv(token_handle, &req, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  100. starpu_mpi_wait(&req, &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_isend(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. if (!mpi_init)
  120. MPI_Finalize();
  121. #ifndef STARPU_SIMGRID
  122. if (rank == last_rank)
  123. {
  124. FPRINTF(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  125. STARPU_ASSERT(token == nloops*size);
  126. }
  127. #endif
  128. return 0;
  129. }