critical_section_with_void_interface.c 2.3 KB

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