ring_async.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. static struct starpu_codelet increment_cl =
  35. {
  36. #ifdef STARPU_USE_CUDA
  37. .cuda_funcs = {increment_cuda, NULL},
  38. #endif
  39. .cpu_funcs = {increment_cpu, NULL},
  40. .nbuffers = 1,
  41. .modes = {STARPU_RW}
  42. };
  43. void increment_token(void)
  44. {
  45. struct starpu_task *task = starpu_task_create();
  46. task->cl = &increment_cl;
  47. task->handles[0] = token_handle;
  48. task->synchronous = 1;
  49. int ret = starpu_task_submit(task);
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. int ret, rank, size;
  55. MPI_Init(NULL, NULL);
  56. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  57. MPI_Comm_size(MPI_COMM_WORLD, &size);
  58. if (size < 2)
  59. {
  60. if (rank == 0)
  61. FPRINTF(stderr, "We need at least 2 processes.\n");
  62. MPI_Finalize();
  63. return STARPU_TEST_SKIPPED;
  64. }
  65. ret = starpu_init(NULL);
  66. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  67. ret = starpu_mpi_init(NULL, NULL, 0);
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  69. starpu_vector_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, 1, sizeof(token));
  70. int nloops = NITER;
  71. int loop;
  72. int last_loop = nloops - 1;
  73. int last_rank = size - 1;
  74. for (loop = 0; loop < nloops; loop++)
  75. {
  76. int tag = loop*size + rank;
  77. if (loop == 0 && rank == 0)
  78. {
  79. token = 0;
  80. FPRINTF(stdout, "Start with token value %u\n", token);
  81. }
  82. else
  83. {
  84. MPI_Status status;
  85. starpu_mpi_req req;
  86. starpu_mpi_irecv(token_handle, &req, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  87. starpu_mpi_wait(&req, &status);
  88. }
  89. increment_token();
  90. if (loop == last_loop && rank == last_rank)
  91. {
  92. starpu_data_acquire(token_handle, STARPU_R);
  93. FPRINTF(stdout, "Finished : token value %u\n", token);
  94. starpu_data_release(token_handle);
  95. }
  96. else {
  97. starpu_mpi_req req;
  98. MPI_Status status;
  99. starpu_mpi_isend(token_handle, &req, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  100. starpu_mpi_wait(&req, &status);
  101. }
  102. }
  103. starpu_data_unregister(token_handle);
  104. starpu_mpi_shutdown();
  105. starpu_shutdown();
  106. MPI_Finalize();
  107. if (rank == last_rank)
  108. {
  109. STARPU_ASSERT(token == nloops*size);
  110. }
  111. return 0;
  112. }