broadcast.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014,2015,2017,2018 Université de Bordeaux
  4. * Copyright (C) 2013,2015,2017 CNRS
  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. void wait_CPU(void *descr[], void *args)
  20. {
  21. int *var = (int*) STARPU_VARIABLE_GET_PTR(descr[0]);
  22. int val;
  23. starpu_codelet_unpack_args(args, &val);
  24. *var = val;
  25. starpu_sleep(1);
  26. }
  27. static struct starpu_codelet cl =
  28. {
  29. .cpu_funcs = { wait_CPU },
  30. .cpu_funcs_name = { "wait_CPU" },
  31. .nbuffers = 1,
  32. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  33. .modes = { STARPU_W },
  34. };
  35. int main(int argc, char **argv)
  36. {
  37. int ret, rank, size;
  38. starpu_data_handle_t handle;
  39. int var;
  40. int mpi_init;
  41. MPI_Status status;
  42. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  43. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  44. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  45. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  46. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  47. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(var));
  48. if (rank == 0)
  49. {
  50. int val, n;
  51. val = 42;
  52. starpu_task_insert(&cl, STARPU_W, handle, STARPU_VALUE, &val, sizeof(val), 0);
  53. for(n = 1 ; n < size ; n++)
  54. {
  55. FPRINTF_MPI(stderr, "sending data to %d\n", n);
  56. starpu_mpi_isend_detached(handle, n, 0, MPI_COMM_WORLD, NULL, NULL);
  57. }
  58. val = 43;
  59. starpu_task_insert(&cl, STARPU_W, handle, STARPU_VALUE, &val, sizeof(val), 0);
  60. for(n = 1 ; n < size ; n++)
  61. {
  62. FPRINTF_MPI(stderr, "sending data to %d\n", n);
  63. starpu_mpi_isend_detached(handle, n, 0, MPI_COMM_WORLD, NULL, NULL);
  64. }
  65. }
  66. else
  67. {
  68. starpu_mpi_recv(handle, 0, 0, MPI_COMM_WORLD, &status);
  69. starpu_data_acquire(handle, STARPU_R);
  70. STARPU_ASSERT(var == 42);
  71. starpu_data_release(handle);
  72. starpu_mpi_recv(handle, 0, 0, MPI_COMM_WORLD, &status);
  73. starpu_data_acquire(handle, STARPU_R);
  74. STARPU_ASSERT(var == 43);
  75. starpu_data_release(handle);
  76. FPRINTF_MPI(stderr, "received data\n");
  77. }
  78. starpu_data_unregister(handle);
  79. starpu_mpi_shutdown();
  80. if (!mpi_init)
  81. MPI_Finalize();
  82. return 0;
  83. }