acquire_try.c 2.5 KB

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