readers_and_writers.c 2.2 KB

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