readers_and_writers.c 2.7 KB

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