critical_section_with_void_interface.c 2.5 KB

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