callback.c 3.4 KB

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