ring_async_implicit.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu_mpi.h>
  17. #define NITER 2048
  18. unsigned token = 42;
  19. starpu_data_handle token_handle;
  20. #ifdef STARPU_USE_CUDA
  21. extern void increment_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  22. #endif
  23. void increment_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  24. {
  25. unsigned *tokenptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  26. (*tokenptr)++;
  27. }
  28. static starpu_codelet increment_cl = {
  29. .where = STARPU_CPU|STARPU_CUDA,
  30. #ifdef STARPU_USE_CUDA
  31. .cuda_func = increment_cuda,
  32. #endif
  33. .cpu_func = increment_cpu,
  34. .nbuffers = 1
  35. };
  36. void increment_token(void)
  37. {
  38. struct starpu_task *task = starpu_task_create();
  39. task->cl = &increment_cl;
  40. task->buffers[0].handle = token_handle;
  41. task->buffers[0].mode = STARPU_RW;
  42. starpu_task_submit(task);
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. MPI_Init(NULL, NULL);
  47. int rank, size;
  48. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  49. MPI_Comm_size(MPI_COMM_WORLD, &size);
  50. if (size < 2)
  51. {
  52. if (rank == 0)
  53. fprintf(stderr, "We need at least 2 processes.\n");
  54. MPI_Finalize();
  55. return 0;
  56. }
  57. starpu_init(NULL);
  58. starpu_mpi_initialize();
  59. starpu_vector_data_register(&token_handle, 0, (uintptr_t)&token, 1, sizeof(unsigned));
  60. unsigned nloops = NITER;
  61. unsigned loop;
  62. unsigned last_loop = nloops - 1;
  63. unsigned last_rank = size - 1;
  64. for (loop = 0; loop < nloops; loop++)
  65. {
  66. int tag = loop*size + rank;
  67. if (!((loop == 0) && (rank == 0)))
  68. {
  69. token = 0;
  70. starpu_mpi_irecv_detached(token_handle, (rank+size-1)%size, tag, MPI_COMM_WORLD, NULL, NULL);
  71. }
  72. else {
  73. token = 0;
  74. fprintf(stdout, "Start with token value %d\n", token);
  75. }
  76. increment_token();
  77. if (!((loop == last_loop) && (rank == last_rank)))
  78. {
  79. starpu_mpi_isend_detached(token_handle, (rank+1)%size, tag+1, MPI_COMM_WORLD, NULL, NULL);
  80. }
  81. else {
  82. starpu_data_acquire(token_handle, STARPU_R);
  83. fprintf(stdout, "Finished : token value %d\n", token);
  84. starpu_data_release(token_handle);
  85. }
  86. }
  87. starpu_task_wait_for_all();
  88. starpu_mpi_shutdown();
  89. starpu_shutdown();
  90. MPI_Finalize();
  91. if (rank == last_rank)
  92. {
  93. STARPU_ASSERT(token == nloops*size);
  94. }
  95. return 0;
  96. }