ring_async.c 3.7 KB

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