redux_acquire.c 2.5 KB

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