redux_acquire.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. void init_cpu_func(void *descr[], void *cl_arg)
  19. {
  20. long int *dot = (long int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  21. *dot = 42;
  22. }
  23. void redux_cpu_func(void *descr[], void *cl_arg)
  24. {
  25. long int *dota = (long int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  26. long int *dotb = (long int *)STARPU_VARIABLE_GET_PTR(descr[1]);
  27. *dota = *dota + *dotb;
  28. }
  29. static struct starpu_codelet init_codelet =
  30. {
  31. .cpu_funcs = {init_cpu_func},
  32. .nbuffers = 1,
  33. .modes = {STARPU_W},
  34. .name = "init_codelet"
  35. };
  36. static struct starpu_codelet redux_codelet =
  37. {
  38. .cpu_funcs = {redux_cpu_func},
  39. .modes = {STARPU_RW, STARPU_R},
  40. .nbuffers = 2,
  41. .name = "redux_codelet"
  42. };
  43. static void check_dot(void *dot_handle)
  44. {
  45. long int *x = starpu_data_get_local_ptr(dot_handle);
  46. STARPU_ASSERT_MSG(*x == 42, "Incorrect value %ld", *x);
  47. starpu_data_release(dot_handle);
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. starpu_data_handle_t dot_handle;
  52. int ret = starpu_init(NULL);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  54. starpu_variable_data_register(&dot_handle, -1, (uintptr_t)NULL, sizeof(long int));
  55. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  56. starpu_data_acquire(dot_handle, STARPU_R);
  57. long int *x = starpu_data_get_local_ptr(dot_handle);
  58. STARPU_ASSERT_MSG(*x == 42, "Incorrect value %ld", *x);
  59. starpu_data_release(dot_handle);
  60. starpu_data_unregister(dot_handle);
  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_cb(dot_handle, STARPU_R, check_dot, dot_handle);
  64. starpu_data_unregister(dot_handle);
  65. starpu_shutdown();
  66. return 0;
  67. }