critical_section_with_void_interface.c 2.4 KB

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