critical_section_with_void_interface.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. starpu_init(NULL);
  41. critical_var = 0;
  42. /* Create a void data which will be used as an exclusion mechanism. */
  43. starpu_void_data_register(&void_handle);
  44. int i;
  45. for (i = 0; i < ntasks; i++)
  46. {
  47. struct starpu_task *task = starpu_task_create();
  48. task->cl = &cl;
  49. task->buffers[0].handle = void_handle;
  50. task->buffers[0].mode = STARPU_RW;
  51. ret = starpu_task_submit(task);
  52. if (ret == -ENODEV)
  53. goto enodev;
  54. }
  55. starpu_data_unregister(void_handle);
  56. STARPU_ASSERT(critical_var == ntasks);
  57. starpu_shutdown();
  58. return 0;
  59. enodev:
  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. return 0;
  64. }