ring_async.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. #define NITER 2048
  20. unsigned token = 42;
  21. starpu_data_handle_t token_handle;
  22. #ifdef STARPU_USE_CUDA
  23. extern void increment_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  24. #endif
  25. void increment_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  26. {
  27. unsigned *tokenptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  28. (*tokenptr)++;
  29. }
  30. static struct starpu_codelet increment_cl =
  31. {
  32. .where = STARPU_CPU|STARPU_CUDA,
  33. #ifdef STARPU_USE_CUDA
  34. .cuda_func = increment_cuda,
  35. #endif
  36. .cpu_func = increment_cpu,
  37. .nbuffers = 1,
  38. .modes = {STARPU_RW}
  39. };
  40. void increment_token(void)
  41. {
  42. struct starpu_task *task = starpu_task_create();
  43. task->cl = &increment_cl;
  44. task->handles[0] = token_handle;
  45. task->synchronous = 1;
  46. starpu_task_submit(task);
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int ret, rank, size;
  51. MPI_Init(NULL, NULL);
  52. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  53. MPI_Comm_size(MPI_COMM_WORLD, &size);
  54. if (size < 2)
  55. {
  56. if (rank == 0)
  57. FPRINTF(stderr, "We need at least 2 processes.\n");
  58. MPI_Finalize();
  59. return STARPU_TEST_SKIPPED;
  60. }
  61. ret = starpu_init(NULL);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  63. ret = starpu_mpi_initialize();
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_initialize");
  65. starpu_vector_data_register(&token_handle, 0, (uintptr_t)&token, 1, sizeof(unsigned));
  66. unsigned nloops = NITER;
  67. unsigned loop;
  68. unsigned last_loop = nloops - 1;
  69. unsigned last_rank = size - 1;
  70. for (loop = 0; loop < nloops; loop++)
  71. {
  72. int tag = loop*size + rank;
  73. if (loop == 0 && rank == 0)
  74. {
  75. token = 0;
  76. FPRINTF(stdout, "Start with token value %d\n", token);
  77. }
  78. else
  79. {
  80. MPI_Status status;
  81. starpu_mpi_req req;
  82. starpu_mpi_irecv(token_handle, &req, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  83. starpu_mpi_wait(&req, &status);
  84. }
  85. increment_token();
  86. if (loop == last_loop && rank == last_rank)
  87. {
  88. starpu_data_acquire(token_handle, STARPU_R);
  89. FPRINTF(stdout, "Finished : token value %d\n", token);
  90. starpu_data_release(token_handle);
  91. }
  92. else {
  93. starpu_mpi_req req;
  94. MPI_Status status;
  95. starpu_mpi_isend(token_handle, &req, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  96. starpu_mpi_wait(&req, &status);
  97. }
  98. }
  99. starpu_mpi_shutdown();
  100. starpu_shutdown();
  101. MPI_Finalize();
  102. if (rank == last_rank)
  103. {
  104. STARPU_ASSERT(token == nloops*size);
  105. }
  106. return 0;
  107. }