redux_acquire.c 2.6 KB

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