acquire_release_to.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #else
  24. static unsigned ntasks = 1000;
  25. #endif
  26. #ifdef STARPU_USE_CUDA
  27. extern void increment_cuda(void *descr[], void *_args);
  28. #endif
  29. #ifdef STARPU_USE_OPENCL
  30. extern void increment_opencl(void *buffers[], void *args);
  31. #endif
  32. void increment_cpu(void *descr[], void *arg)
  33. {
  34. (void)arg;
  35. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  36. (*tokenptr)++;
  37. }
  38. static struct starpu_codelet increment_cl =
  39. {
  40. .modes = { STARPU_RW },
  41. .cpu_funcs = {increment_cpu},
  42. #ifdef STARPU_USE_CUDA
  43. .cuda_funcs = {increment_cuda},
  44. .cuda_flags = {STARPU_CUDA_ASYNC},
  45. #endif
  46. #ifdef STARPU_USE_OPENCL
  47. .opencl_funcs = {increment_opencl},
  48. .opencl_flags = {STARPU_OPENCL_ASYNC},
  49. #endif
  50. .cpu_funcs_name = {"increment_cpu"},
  51. .nbuffers = 1
  52. };
  53. void check_cpu(void *descr[], void *arg)
  54. {
  55. unsigned *val = arg;
  56. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  57. STARPU_ASSERT(*tokenptr == *val);
  58. }
  59. static struct starpu_codelet check_cl =
  60. {
  61. .modes = { STARPU_R },
  62. .cpu_funcs = {check_cpu},
  63. .cpu_funcs_name = {"check_cpu"},
  64. .nbuffers = 1
  65. };
  66. unsigned token = 0;
  67. starpu_data_handle_t token_handle;
  68. static
  69. int increment_token(void)
  70. {
  71. int ret;
  72. struct starpu_task *task = starpu_task_create();
  73. task->cl = &increment_cl;
  74. task->handles[0] = token_handle;
  75. ret = starpu_task_submit(task);
  76. return ret;
  77. }
  78. static
  79. int check_token(unsigned value)
  80. {
  81. unsigned *value_p;
  82. int ret;
  83. struct starpu_task *task = starpu_task_create();
  84. task->cl = &check_cl;
  85. task->handles[0] = token_handle;
  86. task->cl_arg = value_p = malloc(sizeof(*value_p));
  87. task->cl_arg_size = sizeof(*value_p);
  88. task->cl_arg_free = 1;
  89. *value_p = value;
  90. ret = starpu_task_submit(task);
  91. return ret;
  92. }
  93. static
  94. void callback(void *arg)
  95. {
  96. (void)arg;
  97. token++;
  98. starpu_data_release_to(token_handle, STARPU_W);
  99. starpu_sleep(0.001);
  100. starpu_data_release_to(token_handle, STARPU_R);
  101. starpu_sleep(0.001);
  102. starpu_data_release(token_handle);
  103. }
  104. #ifdef STARPU_USE_OPENCL
  105. struct starpu_opencl_program opencl_program;
  106. #endif
  107. int main(int argc, char **argv)
  108. {
  109. unsigned i;
  110. int ret;
  111. ret = starpu_initialize(NULL, &argc, &argv);
  112. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  114. #ifdef STARPU_USE_OPENCL
  115. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/acquire_release_opencl_kernel.cl",
  116. &opencl_program, NULL);
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  118. #endif
  119. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  120. FPRINTF(stderr, "Token: %u\n", token);
  121. for(i=0; i<ntasks; i++)
  122. {
  123. /* synchronize data in RAM */
  124. ret = starpu_data_acquire(token_handle, STARPU_RW);
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  126. token ++;
  127. ret = check_token(4*i+1);
  128. if (ret == -ENODEV) goto enodev_release;
  129. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  130. ret = increment_token();
  131. if (ret == -ENODEV) goto enodev_release;
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  133. ret = check_token(4*i+2);
  134. if (ret == -ENODEV) goto enodev_release;
  135. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  136. starpu_sleep(0.001);
  137. starpu_data_release_to(token_handle, STARPU_W);
  138. starpu_sleep(0.001);
  139. starpu_data_release_to(token_handle, STARPU_R);
  140. starpu_sleep(0.001);
  141. starpu_data_release(token_handle);
  142. ret = starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  144. ret = check_token(4*i+3);
  145. if (ret == -ENODEV) goto enodev;
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  147. ret = increment_token();
  148. if (ret == -ENODEV) goto enodev;
  149. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  150. ret = check_token(4*i+4);
  151. if (ret == -ENODEV) goto enodev;
  152. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  153. }
  154. starpu_data_unregister(token_handle);
  155. #ifdef STARPU_USE_OPENCL
  156. ret = starpu_opencl_unload_opencl(&opencl_program);
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  158. #endif
  159. starpu_shutdown();
  160. FPRINTF(stderr, "Token: %u\n", token);
  161. if (token == ntasks * 4)
  162. ret = EXIT_SUCCESS;
  163. else
  164. ret = EXIT_FAILURE;
  165. return ret;
  166. enodev_release:
  167. starpu_data_release(token_handle);
  168. enodev:
  169. starpu_data_unregister(token_handle);
  170. fprintf(stderr, "WARNING: No one can execute this task\n");
  171. /* yes, we do not perform the computation but we did detect that no one
  172. * could perform the kernel, so this is not an error from StarPU */
  173. #ifdef STARPU_USE_OPENCL
  174. ret = starpu_opencl_unload_opencl(&opencl_program);
  175. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  176. #endif
  177. starpu_shutdown();
  178. return STARPU_TEST_SKIPPED;
  179. }