critical_section_with_void_interface.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2016 Université de Bordeaux
  4. * Copyright (C) 2012, 2013 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 <config.h>
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  30. {
  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(int argc, char **argv)
  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. }