readers_and_writers.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 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 mixing readers and writers on the same variable
  20. */
  21. static unsigned book = 0;
  22. static starpu_data_handle_t book_handle;
  23. void dummy_kernel(void *descr[], void *arg)
  24. {
  25. (void)descr;
  26. (void)arg;
  27. }
  28. static struct starpu_codelet r_cl =
  29. {
  30. .cuda_funcs = {dummy_kernel},
  31. .cpu_funcs = {dummy_kernel},
  32. .opencl_funcs = {dummy_kernel},
  33. .cpu_funcs_name = {"dummy_kernel"},
  34. .nbuffers = 1,
  35. .modes = {STARPU_R}
  36. };
  37. static struct starpu_codelet w_cl =
  38. {
  39. .cuda_funcs = {dummy_kernel},
  40. .cpu_funcs = {dummy_kernel},
  41. .opencl_funcs = {dummy_kernel},
  42. .cpu_funcs_name = {"dummy_kernel"},
  43. .nbuffers = 1,
  44. .modes = {STARPU_W}
  45. };
  46. int main(int argc, char **argv)
  47. {
  48. int ret;
  49. ret = starpu_initialize(NULL, &argc, &argv);
  50. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  52. /* initialize the resource */
  53. starpu_vector_data_register(&book_handle, STARPU_MAIN_RAM, (uintptr_t)&book, 1, sizeof(unsigned));
  54. #ifdef STARPU_QUICK_CHECK
  55. unsigned ntasks = 16;
  56. #else
  57. unsigned ntasks = 16*1024;
  58. #endif
  59. unsigned t;
  60. for (t = 0; t < ntasks; t++)
  61. {
  62. struct starpu_task *task = starpu_task_create();
  63. task->handles[0] = book_handle;
  64. /* we randomly select either a reader or a writer (give 10
  65. * times more chances to be a reader) */
  66. enum starpu_data_access_mode mode = ((rand() % 10)==0)?STARPU_W:STARPU_R;
  67. if (mode == STARPU_W)
  68. task->cl = &w_cl;
  69. else
  70. task->cl = &r_cl;
  71. ret = starpu_task_submit(task);
  72. if (ret == -ENODEV) goto enodev;
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  74. }
  75. ret = starpu_task_wait_for_all();
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  77. starpu_data_unregister(book_handle);
  78. starpu_shutdown();
  79. return EXIT_SUCCESS;
  80. enodev:
  81. starpu_data_unregister(book_handle);
  82. fprintf(stderr, "WARNING: No one can execute this task\n");
  83. /* yes, we do not perform the computation but we did detect that no one
  84. * could perform the kernel, so this is not an error from StarPU */
  85. starpu_shutdown();
  86. return STARPU_TEST_SKIPPED;
  87. }