critical_section_with_void_interface.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  49. critical_var = 0;
  50. /* Create a void data which will be used as an exclusion mechanism. */
  51. starpu_void_data_register(&void_handle);
  52. int i;
  53. for (i = 0; i < ntasks; i++)
  54. {
  55. struct starpu_task *task = starpu_task_create();
  56. task->cl = &cl;
  57. task->handles[0] = void_handle;
  58. ret = starpu_task_submit(task);
  59. if (ret == -ENODEV) goto enodev;
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  61. }
  62. starpu_data_unregister(void_handle);
  63. STARPU_ASSERT(critical_var == ntasks);
  64. starpu_shutdown();
  65. return EXIT_SUCCESS;
  66. enodev:
  67. fprintf(stderr, "WARNING: No one can execute this task\n");
  68. /* yes, we do not perform the computation but we did detect that no one
  69. * could perform the kernel, so this is not an error from StarPU */
  70. starpu_shutdown();
  71. return STARPU_TEST_SKIPPED;
  72. }