ring_async_implicit.c 2.8 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. int rank, size;
  47. #if 0
  48. MPI_Init(NULL, NULL);
  49. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  50. MPI_Comm_size(MPI_COMM_WORLD, &size);
  51. #endif
  52. starpu_init(NULL);
  53. starpu_mpi_initialize_extended(1, &rank, &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 0;
  60. }
  61. starpu_vector_data_register(&token_handle, 0, (uintptr_t)&token, 1, sizeof(unsigned));
  62. unsigned nloops = NITER;
  63. unsigned loop;
  64. unsigned last_loop = nloops - 1;
  65. unsigned last_rank = size - 1;
  66. for (loop = 0; loop < nloops; loop++)
  67. {
  68. int tag = loop*size + rank;
  69. if (!((loop == 0) && (rank == 0)))
  70. {
  71. starpu_mpi_irecv_detached(token_handle, (rank+size-1)%size, tag, MPI_COMM_WORLD, NULL, NULL);
  72. }
  73. else {
  74. token = 0;
  75. fprintf(stdout, "Start with token value %d\n", token);
  76. }
  77. increment_token();
  78. if (!((loop == last_loop) && (rank == last_rank)))
  79. {
  80. starpu_mpi_isend_detached(token_handle, (rank+1)%size, tag+1, MPI_COMM_WORLD, NULL, NULL);
  81. }
  82. else {
  83. starpu_data_acquire(token_handle, STARPU_R);
  84. fprintf(stdout, "Finished : token value %d\n", token);
  85. starpu_data_release(token_handle);
  86. }
  87. }
  88. starpu_task_wait_for_all();
  89. starpu_mpi_shutdown();
  90. starpu_shutdown();
  91. MPI_Finalize();
  92. if (rank == last_rank)
  93. {
  94. fprintf(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  95. STARPU_ASSERT(token == nloops*size);
  96. }
  97. return 0;
  98. }