redux_acquire.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017-2020 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.h>
  17. #include <math.h>
  18. #include "helper.h"
  19. void init_cpu_func(void *descr[], void *cl_arg)
  20. {
  21. (void)cl_arg;
  22. long int *dot = (long int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  23. *dot = 42;
  24. }
  25. void redux_cpu_func(void *descr[], void *cl_arg)
  26. {
  27. (void)cl_arg;
  28. long int *dota = (long int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  29. long int *dotb = (long int *)STARPU_VARIABLE_GET_PTR(descr[1]);
  30. *dota = *dota + *dotb;
  31. }
  32. static struct starpu_codelet init_codelet =
  33. {
  34. .cpu_funcs = {init_cpu_func},
  35. .nbuffers = 1,
  36. .modes = {STARPU_W},
  37. .name = "init_codelet"
  38. };
  39. static struct starpu_codelet redux_codelet =
  40. {
  41. .cpu_funcs = {redux_cpu_func},
  42. .modes = {STARPU_RW, STARPU_R},
  43. .nbuffers = 2,
  44. .name = "redux_codelet"
  45. };
  46. static void check_dot(void *dot_handle)
  47. {
  48. long int *x = starpu_data_get_local_ptr(dot_handle);
  49. STARPU_ASSERT_MSG(*x == 42, "Incorrect value %ld", *x);
  50. starpu_data_release(dot_handle);
  51. }
  52. int main(void)
  53. {
  54. starpu_data_handle_t dot_handle;
  55. int ret = starpu_init(NULL);
  56. if (ret == -ENODEV)
  57. goto skip;
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  59. if (starpu_cpu_worker_get_count() == 0)
  60. goto enodev;
  61. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(long int));
  62. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  63. starpu_data_acquire(dot_handle, STARPU_R);
  64. long int *x = starpu_data_get_local_ptr(dot_handle);
  65. STARPU_ASSERT_MSG(*x == 42, "Incorrect value %ld", *x);
  66. starpu_data_release(dot_handle);
  67. starpu_data_unregister(dot_handle);
  68. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(long int));
  69. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  70. starpu_data_acquire_cb(dot_handle, STARPU_R, check_dot, dot_handle);
  71. starpu_data_unregister(dot_handle);
  72. starpu_shutdown();
  73. return 0;
  74. enodev:
  75. starpu_shutdown();
  76. skip:
  77. return STARPU_TEST_SKIPPED;
  78. }