critical_section_with_void_interface.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. starpu_data_handle_t void_handle;
  23. int critical_var;
  24. static void critical_section(void *descr[], __attribute__ ((unused)) void *_args)
  25. {
  26. /* We do not protect this variable because it is only accessed when the
  27. * "void_handle" piece of data is accessed. */
  28. critical_var++;
  29. }
  30. static struct starpu_codelet cl =
  31. {
  32. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  33. .cpu_funcs = {critical_section, NULL},
  34. .cuda_funcs = {critical_section, NULL},
  35. .opencl_funcs = {critical_section, NULL},
  36. .nbuffers = 1
  37. };
  38. int main(int argc, char **argv)
  39. {
  40. int ntasks = 1000;
  41. int ret;
  42. #ifdef STARPU_SLOW_MACHINE
  43. ntasks /= 10;
  44. #endif
  45. ret = starpu_init(NULL);
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  47. critical_var = 0;
  48. /* Create a void data which will be used as an exclusion mechanism. */
  49. starpu_void_data_register(&void_handle);
  50. int i;
  51. for (i = 0; i < ntasks; i++)
  52. {
  53. struct starpu_task *task = starpu_task_create();
  54. task->cl = &cl;
  55. task->buffers[0].handle = void_handle;
  56. task->buffers[0].mode = STARPU_RW;
  57. ret = starpu_task_submit(task);
  58. if (ret == -ENODEV) goto enodev;
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  60. }
  61. starpu_data_unregister(void_handle);
  62. STARPU_ASSERT(critical_var == ntasks);
  63. starpu_shutdown();
  64. return EXIT_SUCCESS;
  65. enodev:
  66. fprintf(stderr, "WARNING: No one can execute this task\n");
  67. /* yes, we do not perform the computation but we did detect that no one
  68. * could perform the kernel, so this is not an error from StarPU */
  69. starpu_shutdown();
  70. return STARPU_TEST_SKIPPED;
  71. }