comm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU 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. * StarPU 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. #include <math.h>
  18. #include "helper.h"
  19. #include <starpu_mpi_cache.h>
  20. void func_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  21. {
  22. int *value = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  23. FPRINTF_MPI(stderr, "Executing codelet with value %d\n", *value);
  24. *value = *value * 2;
  25. }
  26. struct starpu_codelet mycodelet =
  27. {
  28. .cpu_funcs = {func_cpu},
  29. .nbuffers = 1,
  30. .modes = {STARPU_RW}
  31. };
  32. int main(int argc, char **argv)
  33. {
  34. int size;
  35. int color;
  36. MPI_Comm newcomm;
  37. int rank, newrank;
  38. int ret;
  39. unsigned val = 42;
  40. starpu_data_handle_t data;
  41. MPI_Init(&argc, &argv);
  42. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  43. MPI_Comm_size(MPI_COMM_WORLD, &size);
  44. if (size < 4)
  45. {
  46. if (rank == 0)
  47. FPRINTF(stderr, "We need at least 4 processes.\n");
  48. MPI_Finalize();
  49. return STARPU_TEST_SKIPPED;
  50. }
  51. color = rank%2;
  52. MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
  53. MPI_Comm_rank(newcomm, &newrank);
  54. FPRINTF_MPI(stderr, "[%d] color %d\n", newrank, color);
  55. ret = starpu_init(NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. ret = starpu_mpi_init(NULL, NULL, 0);
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  60. if (newrank == 0)
  61. {
  62. val = rank+1;
  63. starpu_variable_data_register(&data, 0, (uintptr_t)&val, sizeof(val));
  64. }
  65. else
  66. starpu_variable_data_register(&data, -1, (uintptr_t)NULL, sizeof(unsigned));
  67. starpu_mpi_data_register_comm(data, 42, 0, newcomm);
  68. FPRINTF_MPI(stderr, "[%d] Registering data %p with tag %d and node %d\n", newrank, data, 42, 0);
  69. if (newrank == 0)
  70. {
  71. FPRINTF_MPI(stderr, "[%d] sending %d\n", newrank, rank);
  72. MPI_Send(&rank, 1, MPI_INT, 1, 10, newcomm);
  73. starpu_mpi_send(data, 1, 42, newcomm);
  74. }
  75. else
  76. {
  77. int x;
  78. MPI_Recv(&x, 1, MPI_INT, 0, 10, newcomm, NULL);
  79. FPRINTF_MPI(stderr, "[%d] received %d\n", newrank, x);
  80. starpu_mpi_recv(data, 0, 42, newcomm, NULL);
  81. }
  82. starpu_mpi_insert_task(newcomm, &mycodelet,
  83. STARPU_RW, data,
  84. STARPU_EXECUTE_ON_NODE, 1,
  85. 0);
  86. FPRINTF_MPI(stderr, "Waiting ...\n");
  87. starpu_task_wait_for_all();
  88. starpu_data_unregister(data);
  89. if (newrank == 0)
  90. {
  91. FPRINTF_MPI(stderr, "[%d] new value %u\n", newrank, val);
  92. }
  93. starpu_mpi_shutdown();
  94. starpu_shutdown();
  95. MPI_Finalize();
  96. return 0;
  97. }