callback.c 3.0 KB

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