acquire_try.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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(STARPU_ATTRIBUTE_UNUSED void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  24. {
  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 STARPU_ATTRIBUTE_UNUSED)
  40. {
  41. starpu_data_release(token_handle);
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. unsigned i;
  46. int ret;
  47. int nacquired;
  48. ret = starpu_initialize(NULL, &argc, &argv);
  49. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  51. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  52. ret = starpu_task_insert(&cl, STARPU_RW, token_handle, 0);
  53. if (ret == -ENODEV)
  54. goto enodev;
  55. ret = starpu_data_acquire_try(token_handle, STARPU_R);
  56. STARPU_ASSERT(ret != 0);
  57. starpu_do_schedule();
  58. while ((ret = starpu_data_acquire_try(token_handle, STARPU_R)) != 0)
  59. {
  60. starpu_sleep(0.001);
  61. }
  62. ret = starpu_task_insert(&cl, STARPU_RW, token_handle, 0);
  63. if (ret == -ENODEV)
  64. goto enodev;
  65. starpu_data_release(token_handle);
  66. starpu_task_wait_for_all();
  67. ret = starpu_data_acquire_try(token_handle, STARPU_R);
  68. STARPU_ASSERT(ret == 0);
  69. starpu_data_release(token_handle);
  70. starpu_data_unregister(token_handle);
  71. starpu_shutdown();
  72. return 0;
  73. enodev:
  74. starpu_data_unregister(token_handle);
  75. fprintf(stderr, "WARNING: No one can execute this task\n");
  76. /* yes, we do not perform the computation but we did detect that no one
  77. * could perform the kernel, so this is not an error from StarPU */
  78. starpu_shutdown();
  79. return STARPU_TEST_SKIPPED;
  80. }