acquire_release_to.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <starpu.h>
  17. #include "../helper.h"
  18. /*
  19. * Check that _release_to correctly interacts with tasks working on the same data
  20. */
  21. #ifdef STARPU_QUICK_CHECK
  22. static unsigned ntasks = 10;
  23. #elif !defined(STARPU_LONG_CHECK)
  24. static unsigned ntasks = 1000;
  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. void check_cpu(void *descr[], void *arg)
  56. {
  57. unsigned *val = arg;
  58. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  59. STARPU_ASSERT(*tokenptr == *val);
  60. }
  61. static struct starpu_codelet check_cl =
  62. {
  63. .modes = { STARPU_R },
  64. .cpu_funcs = {check_cpu},
  65. .cpu_funcs_name = {"check_cpu"},
  66. .nbuffers = 1
  67. };
  68. unsigned token = 0;
  69. starpu_data_handle_t token_handle;
  70. static
  71. int increment_token(void)
  72. {
  73. int ret;
  74. struct starpu_task *task = starpu_task_create();
  75. task->cl = &increment_cl;
  76. task->handles[0] = token_handle;
  77. ret = starpu_task_submit(task);
  78. return ret;
  79. }
  80. static
  81. int check_token(unsigned value)
  82. {
  83. unsigned *value_p;
  84. int ret;
  85. struct starpu_task *task = starpu_task_create();
  86. task->cl = &check_cl;
  87. task->handles[0] = token_handle;
  88. task->cl_arg = value_p = malloc(sizeof(*value_p));
  89. task->cl_arg_size = sizeof(*value_p);
  90. task->cl_arg_free = 1;
  91. *value_p = value;
  92. ret = starpu_task_submit(task);
  93. return ret;
  94. }
  95. static
  96. void callback(void *arg)
  97. {
  98. (void)arg;
  99. token++;
  100. starpu_data_release_to(token_handle, STARPU_W);
  101. starpu_sleep(0.001);
  102. starpu_data_release_to(token_handle, STARPU_R);
  103. starpu_sleep(0.001);
  104. starpu_data_release(token_handle);
  105. }
  106. #ifdef STARPU_USE_OPENCL
  107. struct starpu_opencl_program opencl_program;
  108. #endif
  109. int main(int argc, char **argv)
  110. {
  111. unsigned i;
  112. int ret;
  113. ret = starpu_initialize(NULL, &argc, &argv);
  114. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  116. #ifdef STARPU_USE_OPENCL
  117. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/acquire_release_opencl_kernel.cl",
  118. &opencl_program, NULL);
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  120. #endif
  121. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  122. FPRINTF(stderr, "Token: %u\n", token);
  123. for(i=0; i<ntasks; i++)
  124. {
  125. /* synchronize data in RAM */
  126. ret = starpu_data_acquire(token_handle, STARPU_RW);
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  128. token ++;
  129. ret = check_token(4*i+1);
  130. if (ret == -ENODEV) goto enodev_release;
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  132. ret = increment_token();
  133. if (ret == -ENODEV) goto enodev_release;
  134. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  135. ret = check_token(4*i+2);
  136. if (ret == -ENODEV) goto enodev_release;
  137. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  138. starpu_sleep(0.001);
  139. starpu_data_release_to(token_handle, STARPU_W);
  140. starpu_sleep(0.001);
  141. starpu_data_release_to(token_handle, STARPU_R);
  142. starpu_sleep(0.001);
  143. starpu_data_release(token_handle);
  144. ret = starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  146. ret = check_token(4*i+3);
  147. if (ret == -ENODEV) goto enodev;
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  149. ret = increment_token();
  150. if (ret == -ENODEV) goto enodev;
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  152. ret = check_token(4*i+4);
  153. if (ret == -ENODEV) goto enodev;
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. }
  156. starpu_data_unregister(token_handle);
  157. #ifdef STARPU_USE_OPENCL
  158. ret = starpu_opencl_unload_opencl(&opencl_program);
  159. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  160. #endif
  161. starpu_shutdown();
  162. FPRINTF(stderr, "Token: %u\n", token);
  163. if (token == ntasks * 4)
  164. ret = EXIT_SUCCESS;
  165. else
  166. ret = EXIT_FAILURE;
  167. return ret;
  168. enodev_release:
  169. starpu_data_release(token_handle);
  170. enodev:
  171. starpu_data_unregister(token_handle);
  172. fprintf(stderr, "WARNING: No one can execute this task\n");
  173. /* yes, we do not perform the computation but we did detect that no one
  174. * could perform the kernel, so this is not an error from StarPU */
  175. #ifdef STARPU_USE_OPENCL
  176. ret = starpu_opencl_unload_opencl(&opencl_program);
  177. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  178. #endif
  179. starpu_shutdown();
  180. return STARPU_TEST_SKIPPED;
  181. }