critical_section_with_void_interface.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. starpu_data_handle void_handle;
  22. int critical_var;
  23. static void critical_section(void *descr[], __attribute__ ((unused)) void *_args)
  24. {
  25. /* We do not protect this variable because it is only accessed when the
  26. * "void_handle" piece of data is accessed. */
  27. critical_var++;
  28. }
  29. static starpu_codelet cl = {
  30. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  31. .cpu_func = critical_section,
  32. .cuda_func = critical_section,
  33. .opencl_func = critical_section,
  34. .nbuffers = 1
  35. };
  36. int main(int argc, char **argv)
  37. {
  38. int ntasks = 1000;
  39. int ret;
  40. #ifdef STARPU_SLOW_MACHINE
  41. ntasks /= 10;
  42. #endif
  43. starpu_init(NULL);
  44. critical_var = 0;
  45. /* Create a void data which will be used as an exclusion mechanism. */
  46. starpu_void_data_register(&void_handle);
  47. int i;
  48. for (i = 0; i < ntasks; i++)
  49. {
  50. struct starpu_task *task = starpu_task_create();
  51. task->cl = &cl;
  52. task->buffers[0].handle = void_handle;
  53. task->buffers[0].mode = STARPU_RW;
  54. ret = starpu_task_submit(task);
  55. if (ret == -ENODEV)
  56. goto enodev;
  57. }
  58. starpu_data_unregister(void_handle);
  59. STARPU_ASSERT(critical_var == ntasks);
  60. starpu_shutdown();
  61. return 0;
  62. enodev:
  63. fprintf(stderr, "WARNING: No one can execute this task\n");
  64. /* yes, we do not perform the computation but we did detect that no one
  65. * could perform the kernel, so this is not an error from StarPU */
  66. return 77;
  67. }