callback.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2015,2017,2019 CNRS
  4. * Copyright (C) 2014,2015,2017,2018 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_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  60. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  61. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  62. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  63. NULL,
  64. STARPU_EXECUTE_ON_NODE, 0,
  65. STARPU_CALLBACK_WITH_ARG_NFREE, callback, &x,
  66. 0);
  67. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  68. if (rank == 0)
  69. expected_x ++;
  70. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  71. NULL,
  72. STARPU_EXECUTE_ON_NODE, 0,
  73. STARPU_CALLBACK, callback,
  74. STARPU_CALLBACK_ARG_NFREE, &x,
  75. 0);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  77. if (rank == 0)
  78. expected_x ++;
  79. STARPU_ASSERT_MSG(x == expected_x, "x should be equal to %d and not %d\n", expected_x, x);
  80. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  81. NULL,
  82. STARPU_EXECUTE_ON_NODE, 0,
  83. STARPU_PROLOGUE_CALLBACK, prologue_callback,
  84. STARPU_PROLOGUE_CALLBACK_ARG_NFREE, &y,
  85. 0);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  87. if (rank == 0)
  88. expected_y ++;
  89. ret = starpu_mpi_task_insert(MPI_COMM_WORLD,
  90. &my_codelet,
  91. STARPU_EXECUTE_ON_NODE, 0,
  92. STARPU_PROLOGUE_CALLBACK_POP, prologue_callback,
  93. STARPU_PROLOGUE_CALLBACK_POP_ARG_NFREE, &y,
  94. 0);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  96. starpu_task_wait_for_all();
  97. if (rank == 0)
  98. expected_y ++;
  99. STARPU_ASSERT_MSG(y == expected_y, "y should be equal to %d and not %d\n", expected_y, y);
  100. starpu_mpi_shutdown();
  101. return EXIT_SUCCESS;
  102. }