critical_section_with_void_interface.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "../helper.h"
  22. /*
  23. * Use a void interface to protect the access to a variable that is not declared to StarPU
  24. */
  25. starpu_data_handle_t void_handle;
  26. int critical_var;
  27. void critical_section(void *descr[], void *_args)
  28. {
  29. (void)descr;
  30. (void)_args;
  31. /* We do not protect this variable because it is only accessed when the
  32. * "void_handle" piece of data is accessed. */
  33. critical_var++;
  34. }
  35. static struct starpu_codelet cl =
  36. {
  37. .cpu_funcs = {critical_section},
  38. .cuda_funcs = {critical_section},
  39. .opencl_funcs = {critical_section},
  40. .nbuffers = 1,
  41. .modes = {STARPU_RW}
  42. };
  43. int main(void)
  44. {
  45. #ifdef STARPU_QUICK_CHECK
  46. int ntasks = 10;
  47. #else
  48. int ntasks = 1000;
  49. #endif
  50. int ret;
  51. ret = starpu_init(NULL);
  52. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  54. critical_var = 0;
  55. /* Create a void data which will be used as an exclusion mechanism. */
  56. starpu_void_data_register(&void_handle);
  57. int i;
  58. for (i = 0; i < ntasks; i++)
  59. {
  60. struct starpu_task *task = starpu_task_create();
  61. task->cl = &cl;
  62. task->handles[0] = void_handle;
  63. ret = starpu_task_submit(task);
  64. if (ret == -ENODEV) goto enodev;
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  66. }
  67. starpu_data_unregister(void_handle);
  68. ret = (critical_var == ntasks) ? EXIT_SUCCESS : EXIT_FAILURE;
  69. starpu_shutdown();
  70. return ret;
  71. enodev:
  72. fprintf(stderr, "WARNING: No one can execute this task\n");
  73. /* yes, we do not perform the computation but we did detect that no one
  74. * could perform the kernel, so this is not an error from StarPU */
  75. starpu_shutdown();
  76. return STARPU_TEST_SKIPPED;
  77. }