critical_section_with_void_interface.c 2.4 KB

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