ring.c 3.7 KB

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