critical_section_with_void_interface.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. starpu_data_handle_t void_handle;
  25. int critical_var;
  26. static void critical_section(void *descr[], __attribute__ ((unused)) void *_args)
  27. {
  28. STARPU_SKIP_IF_VALGRIND;
  29. /* We do not protect this variable because it is only accessed when the
  30. * "void_handle" piece of data is accessed. */
  31. critical_var++;
  32. }
  33. static struct starpu_codelet cl =
  34. {
  35. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  36. .cpu_funcs = {critical_section, NULL},
  37. .cuda_funcs = {critical_section, NULL},
  38. .opencl_funcs = {critical_section, NULL},
  39. .nbuffers = 1,
  40. .modes = {STARPU_RW}
  41. };
  42. int main(int argc, char **argv)
  43. {
  44. int ntasks = 1000;
  45. int ret;
  46. #ifdef STARPU_SLOW_MACHINE
  47. ntasks /= 10;
  48. #endif
  49. ret = starpu_init(NULL);
  50. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  52. critical_var = 0;
  53. /* Create a void data which will be used as an exclusion mechanism. */
  54. starpu_void_data_register(&void_handle);
  55. int i;
  56. for (i = 0; i < ntasks; i++)
  57. {
  58. struct starpu_task *task = starpu_task_create();
  59. task->cl = &cl;
  60. task->handles[0] = void_handle;
  61. ret = starpu_task_submit(task);
  62. if (ret == -ENODEV) goto enodev;
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  64. }
  65. starpu_data_unregister(void_handle);
  66. STARPU_ASSERT(critical_var == ntasks);
  67. starpu_shutdown();
  68. return EXIT_SUCCESS;
  69. enodev:
  70. fprintf(stderr, "WARNING: No one can execute this task\n");
  71. /* yes, we do not perform the computation but we did detect that no one
  72. * could perform the kernel, so this is not an error from StarPU */
  73. starpu_shutdown();
  74. return STARPU_TEST_SKIPPED;
  75. }