ring_async_implicit.c 2.9 KB

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