readers_and_writers.c 2.6 KB

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