acquire_release.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011,2013,2014,2016,2017 Université de Bordeaux
  4. * Copyright (C) 2011-2013 Inria
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Call acquire/release in competition with inserting task working on the same data
  22. */
  23. #ifdef STARPU_QUICK_CHECK
  24. static unsigned ntasks = 10;
  25. #else
  26. static unsigned ntasks = 10000;
  27. #endif
  28. #ifdef STARPU_USE_CUDA
  29. extern void increment_cuda(void *descr[], void *_args);
  30. #endif
  31. #ifdef STARPU_USE_OPENCL
  32. extern void increment_opencl(void *buffers[], void *args);
  33. #endif
  34. void increment_cpu(void *descr[], void *arg)
  35. {
  36. (void)arg;
  37. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  38. (*tokenptr)++;
  39. }
  40. static struct starpu_codelet increment_cl =
  41. {
  42. .modes = { STARPU_RW },
  43. .cpu_funcs = {increment_cpu},
  44. #ifdef STARPU_USE_CUDA
  45. .cuda_funcs = {increment_cuda},
  46. .cuda_flags = {STARPU_CUDA_ASYNC},
  47. #endif
  48. #ifdef STARPU_USE_OPENCL
  49. .opencl_funcs = {increment_opencl},
  50. .opencl_flags = {STARPU_OPENCL_ASYNC},
  51. #endif
  52. .cpu_funcs_name = {"increment_cpu"},
  53. .nbuffers = 1
  54. };
  55. unsigned token = 0;
  56. starpu_data_handle_t token_handle;
  57. static
  58. int increment_token(void)
  59. {
  60. int ret;
  61. struct starpu_task *task = starpu_task_create();
  62. task->synchronous = 1;
  63. task->cl = &increment_cl;
  64. task->handles[0] = token_handle;
  65. ret = starpu_task_submit(task);
  66. return ret;
  67. }
  68. static
  69. void callback(void *arg)
  70. {
  71. (void)arg;
  72. token++;
  73. starpu_data_release(token_handle);
  74. }
  75. #ifdef STARPU_USE_OPENCL
  76. struct starpu_opencl_program opencl_program;
  77. #endif
  78. int main(int argc, char **argv)
  79. {
  80. unsigned i;
  81. int ret;
  82. ret = starpu_initialize(NULL, &argc, &argv);
  83. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  85. #ifdef STARPU_USE_OPENCL
  86. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/acquire_release_opencl_kernel.cl",
  87. &opencl_program, NULL);
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  89. #endif
  90. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  91. FPRINTF(stderr, "Token: %u\n", token);
  92. for(i=0; i<ntasks; i++)
  93. {
  94. /* synchronize data in RAM */
  95. ret = starpu_data_acquire(token_handle, STARPU_RW);
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  97. token ++;
  98. starpu_data_release(token_handle);
  99. ret = increment_token();
  100. if (ret == -ENODEV) goto enodev;
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  102. ret = starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  104. }
  105. starpu_data_unregister(token_handle);
  106. #ifdef STARPU_USE_OPENCL
  107. ret = starpu_opencl_unload_opencl(&opencl_program);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  109. #endif
  110. starpu_shutdown();
  111. FPRINTF(stderr, "Token: %u\n", token);
  112. if (token == ntasks * 3)
  113. ret = EXIT_SUCCESS;
  114. else
  115. ret = EXIT_FAILURE;
  116. return ret;
  117. enodev:
  118. starpu_data_unregister(token_handle);
  119. fprintf(stderr, "WARNING: No one can execute this task\n");
  120. /* yes, we do not perform the computation but we did detect that no one
  121. * could perform the kernel, so this is not an error from StarPU */
  122. #ifdef STARPU_USE_OPENCL
  123. ret = starpu_opencl_unload_opencl(&opencl_program);
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  125. #endif
  126. starpu_shutdown();
  127. return STARPU_TEST_SKIPPED;
  128. }