readers_and_writers.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2013,2014,2016 Université de Bordeaux
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Try mixing readers and writers on the same variable
  22. */
  23. static unsigned book = 0;
  24. static starpu_data_handle_t book_handle;
  25. void dummy_kernel(void *descr[], void *arg)
  26. {
  27. (void)descr;
  28. (void)arg;
  29. }
  30. static struct starpu_codelet r_cl =
  31. {
  32. .cuda_funcs = {dummy_kernel},
  33. .cpu_funcs = {dummy_kernel},
  34. .opencl_funcs = {dummy_kernel},
  35. .cpu_funcs_name = {"dummy_kernel"},
  36. .nbuffers = 1,
  37. .modes = {STARPU_R}
  38. };
  39. static struct starpu_codelet w_cl =
  40. {
  41. .cuda_funcs = {dummy_kernel},
  42. .cpu_funcs = {dummy_kernel},
  43. .opencl_funcs = {dummy_kernel},
  44. .cpu_funcs_name = {"dummy_kernel"},
  45. .nbuffers = 1,
  46. .modes = {STARPU_W}
  47. };
  48. int main(int argc, char **argv)
  49. {
  50. int ret;
  51. ret = starpu_initialize(NULL, &argc, &argv);
  52. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  54. /* initialize the resource */
  55. starpu_vector_data_register(&book_handle, STARPU_MAIN_RAM, (uintptr_t)&book, 1, sizeof(unsigned));
  56. #ifdef STARPU_QUICK_CHECK
  57. unsigned ntasks = 16;
  58. #else
  59. unsigned ntasks = 16*1024;
  60. #endif
  61. unsigned t;
  62. for (t = 0; t < ntasks; t++)
  63. {
  64. struct starpu_task *task = starpu_task_create();
  65. task->handles[0] = book_handle;
  66. /* we randomly select either a reader or a writer (give 10
  67. * times more chances to be a reader) */
  68. enum starpu_data_access_mode mode = ((rand() % 10)==0)?STARPU_W:STARPU_R;
  69. if (mode == STARPU_W)
  70. task->cl = &w_cl;
  71. else
  72. task->cl = &r_cl;
  73. ret = starpu_task_submit(task);
  74. if (ret == -ENODEV) goto enodev;
  75. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  76. }
  77. ret = starpu_task_wait_for_all();
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  79. starpu_data_unregister(book_handle);
  80. starpu_shutdown();
  81. return EXIT_SUCCESS;
  82. enodev:
  83. starpu_data_unregister(book_handle);
  84. fprintf(stderr, "WARNING: No one can execute this task\n");
  85. /* yes, we do not perform the computation but we did detect that no one
  86. * could perform the kernel, so this is not an error from StarPU */
  87. starpu_shutdown();
  88. return STARPU_TEST_SKIPPED;
  89. }