readers_and_writers.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "../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 rw_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. };
  32. int main(int argc, char **argv)
  33. {
  34. int ret;
  35. ret = starpu_init(NULL);
  36. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  37. /* initialize the resource */
  38. starpu_vector_data_register(&book_handle, 0, (uintptr_t)&book, 1, sizeof(unsigned));
  39. unsigned ntasks = 16*1024;
  40. unsigned t;
  41. for (t = 0; t < ntasks; t++)
  42. {
  43. struct starpu_task *task = starpu_task_create();
  44. task->cl = &rw_cl;
  45. /* we randomly select either a reader or a writer (give 10
  46. * times more chances to be a reader) */
  47. task->buffers[0].mode = ((rand() % 10)==0)?STARPU_W:STARPU_R;
  48. task->buffers[0].handle = book_handle;
  49. int ret = starpu_task_submit(task);
  50. if (ret == -ENODEV) goto enodev;
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  52. }
  53. ret = starpu_task_wait_for_all();
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  55. starpu_data_unregister(book_handle);
  56. starpu_shutdown();
  57. return EXIT_SUCCESS;
  58. enodev:
  59. starpu_data_unregister(book_handle);
  60. fprintf(stderr, "WARNING: No one can execute this task\n");
  61. /* yes, we do not perform the computation but we did detect that no one
  62. * could perform the kernel, so this is not an error from StarPU */
  63. starpu_shutdown();
  64. return STARPU_TEST_SKIPPED;
  65. }