critical_section_with_void_interface.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2012 Centre National de la Recherche Scientifique
  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. #if STARPU_HAVE_VALGRIND_H
  19. #include <valgrind/valgrind.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <starpu.h>
  25. #include <stdlib.h>
  26. #include "../helper.h"
  27. starpu_data_handle_t void_handle;
  28. int critical_var;
  29. static void critical_section(void *descr[], __attribute__ ((unused)) void *_args)
  30. {
  31. STARPU_SKIP_IF_VALGRIND;
  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. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  39. .cpu_funcs = {critical_section, NULL},
  40. .cuda_funcs = {critical_section, NULL},
  41. .opencl_funcs = {critical_section, NULL},
  42. .nbuffers = 1,
  43. .modes = {STARPU_RW}
  44. };
  45. int main(int argc, char **argv)
  46. {
  47. int ntasks = 1000;
  48. int ret;
  49. #ifdef STARPU_SLOW_MACHINE
  50. ntasks /= 10;
  51. #endif
  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. STARPU_ASSERT(critical_var == ntasks);
  70. starpu_shutdown();
  71. return EXIT_SUCCESS;
  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. }