acquire_try.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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 "../helper.h"
  18. /*
  19. * Try to use data_acquire_try in parallel with tasks
  20. */
  21. void func(void *descr[], void *arg)
  22. {
  23. (void)descr;
  24. (void)arg;
  25. starpu_sleep(0.01);
  26. }
  27. static struct starpu_codelet cl =
  28. {
  29. .modes = { STARPU_RW },
  30. .cpu_funcs = {func},
  31. .cuda_funcs = {func},
  32. .opencl_funcs = {func},
  33. .cpu_funcs_name = {"func"},
  34. .nbuffers = 1
  35. };
  36. unsigned token = 0;
  37. starpu_data_handle_t token_handle;
  38. static
  39. void callback(void *arg)
  40. {
  41. (void)arg;
  42. starpu_data_release(token_handle);
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. int ret;
  47. ret = starpu_initialize(NULL, &argc, &argv);
  48. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  50. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  51. ret = starpu_task_insert(&cl, STARPU_RW, token_handle, 0);
  52. if (ret == -ENODEV)
  53. goto enodev;
  54. ret = starpu_data_acquire_try(token_handle, STARPU_R);
  55. STARPU_ASSERT(ret != 0);
  56. starpu_do_schedule();
  57. while ((ret = starpu_data_acquire_try(token_handle, STARPU_R)) != 0)
  58. {
  59. starpu_sleep(0.001);
  60. }
  61. ret = starpu_task_insert(&cl, STARPU_RW, token_handle, 0);
  62. if (ret == -ENODEV)
  63. goto enodev;
  64. starpu_data_release(token_handle);
  65. starpu_task_wait_for_all();
  66. ret = starpu_data_acquire_try(token_handle, STARPU_R);
  67. STARPU_ASSERT(ret == 0);
  68. starpu_data_release(token_handle);
  69. starpu_data_unregister(token_handle);
  70. starpu_shutdown();
  71. return 0;
  72. enodev:
  73. starpu_data_unregister(token_handle);
  74. fprintf(stderr, "WARNING: No one can execute this task\n");
  75. /* yes, we do not perform the computation but we did detect that no one
  76. * could perform the kernel, so this is not an error from StarPU */
  77. starpu_shutdown();
  78. return STARPU_TEST_SKIPPED;
  79. }