critical_section_with_void_interface.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "../common/helper.h"
  22. starpu_data_handle void_handle;
  23. int critical_var;
  24. static void critical_section(void *descr[], __attribute__ ((unused)) void *_args)
  25. {
  26. /* We do not protect this variable because it is only accessed when the
  27. * "void_handle" piece of data is accessed. */
  28. critical_var++;
  29. }
  30. static starpu_codelet cl = {
  31. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  32. .cpu_func = critical_section,
  33. .cuda_func = critical_section,
  34. .opencl_func = critical_section,
  35. .nbuffers = 1
  36. };
  37. int main(int argc, char **argv)
  38. {
  39. int ntasks = 1000;
  40. int ret;
  41. #ifdef STARPU_SLOW_MACHINE
  42. ntasks /= 10;
  43. #endif
  44. ret = starpu_init(NULL);
  45. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  46. critical_var = 0;
  47. /* Create a void data which will be used as an exclusion mechanism. */
  48. starpu_void_data_register(&void_handle);
  49. int i;
  50. for (i = 0; i < ntasks; i++)
  51. {
  52. struct starpu_task *task = starpu_task_create();
  53. task->cl = &cl;
  54. task->buffers[0].handle = void_handle;
  55. task->buffers[0].mode = STARPU_RW;
  56. ret = starpu_task_submit(task);
  57. if (ret == -ENODEV) goto enodev;
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  59. }
  60. starpu_data_unregister(void_handle);
  61. STARPU_ASSERT(critical_var == ntasks);
  62. starpu_shutdown();
  63. return 0;
  64. enodev:
  65. fprintf(stderr, "WARNING: No one can execute this task\n");
  66. /* yes, we do not perform the computation but we did detect that no one
  67. * could perform the kernel, so this is not an error from StarPU */
  68. starpu_shutdown();
  69. return 77;
  70. }