callback.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2015,2017 CNRS
  4. * Copyright (C) 2014-2015,2017 Université de Bordeaux
  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. static
  20. int expected_x=40;
  21. static
  22. int expected_y=12;
  23. void my_func(void *descr[], void *_args)
  24. {
  25. (void)descr;
  26. (void)_args;
  27. FPRINTF_MPI(stderr, "i am here\n");
  28. }
  29. struct starpu_codelet my_codelet =
  30. {
  31. .cpu_funcs = {my_func},
  32. .cuda_funcs = {my_func},
  33. .opencl_funcs = {my_func},
  34. .model = &starpu_perfmodel_nop,
  35. };
  36. static
  37. void callback(void *ptr)
  38. {
  39. int *x = (int *)ptr;
  40. FPRINTF_MPI(stderr, "x=%d\n", *x);
  41. STARPU_ASSERT_MSG(*x == expected_x, "%d != %d\n", *x, expected_x);
  42. (*x)++;
  43. }
  44. static
  45. void prologue_callback(void *ptr)
  46. {
  47. int *y = (int *)ptr;
  48. FPRINTF_MPI(stderr, "y=%d\n", *y);
  49. STARPU_ASSERT_MSG(*y == expected_y, "%d != %d\n", *y, expected_y);
  50. (*y)++;
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. int ret;
  55. int x=40;
  56. int y=12;
  57. int rank, size;
  58. ret = starpu_initialize(NULL, &argc, &argv);
  59. if (ret == -ENODEV)
  60. return STARPU_TEST_SKIPPED;
  61. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  62. ret = starpu_mpi_init(&argc, &argv, 1);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  64. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  65. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  66. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  67. NULL,
  68. STARPU_EXECUTE_ON_NODE, 0,
  69. STARPU_CALLBACK_WITH_ARG, callback, &x,
  70. 0);
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  72. if (rank == 0)
  73. expected_x ++;
  74. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  75. NULL,
  76. STARPU_EXECUTE_ON_NODE, 0,
  77. STARPU_CALLBACK, callback,
  78. STARPU_CALLBACK_ARG, &x,
  79. 0);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  81. if (rank == 0)
  82. expected_x ++;
  83. STARPU_ASSERT_MSG(x == expected_x, "x should be equal to %d and not %d\n", expected_x, x);
  84. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  85. NULL,
  86. STARPU_EXECUTE_ON_NODE, 0,
  87. STARPU_PROLOGUE_CALLBACK, prologue_callback,
  88. STARPU_PROLOGUE_CALLBACK_ARG, &y,
  89. 0);
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  91. if (rank == 0)
  92. expected_y ++;
  93. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  94. &my_codelet,
  95. STARPU_EXECUTE_ON_NODE, 0,
  96. STARPU_PROLOGUE_CALLBACK_POP, prologue_callback,
  97. STARPU_PROLOGUE_CALLBACK_POP_ARG, &y,
  98. 0);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  100. starpu_task_wait_for_all();
  101. if (rank == 0)
  102. expected_y ++;
  103. STARPU_ASSERT_MSG(y == expected_y, "y should be equal to %d and not %d\n", expected_y, y);
  104. starpu_mpi_shutdown();
  105. starpu_shutdown();
  106. return EXIT_SUCCESS;
  107. }