acquire_try.c 2.5 KB

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