dsm_stress.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. #include <common/thread.h>
  23. /*
  24. * Trigger various combinations of access modes
  25. */
  26. #ifdef STARPU_QUICK_CHECK
  27. # define N 100
  28. #else
  29. # define N 10000
  30. #endif
  31. #define VECTORSIZE 1024
  32. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  33. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  34. static unsigned finished = 0;
  35. static unsigned cnt = N;
  36. starpu_data_handle_t v_handle, v_handle2;
  37. static unsigned *v;
  38. static unsigned *v2;
  39. static void callback(void *arg)
  40. {
  41. (void)arg;
  42. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  43. ANNOTATE_HAPPENS_BEFORE(&cnt);
  44. if (res == 0)
  45. {
  46. ANNOTATE_HAPPENS_AFTER(&cnt);
  47. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  48. finished = 1;
  49. STARPU_PTHREAD_COND_SIGNAL(&cond);
  50. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  51. }
  52. }
  53. static void cuda_codelet_null(void *descr[], void *_args)
  54. {
  55. (void)descr;
  56. (void)_args;
  57. }
  58. static void opencl_codelet_null(void *descr[], void *_args)
  59. {
  60. (void)descr;
  61. (void)_args;
  62. }
  63. void cpu_codelet_null(void *descr[], void *_args)
  64. {
  65. (void)descr;
  66. (void)_args;
  67. }
  68. static enum starpu_data_access_mode select_random_mode(void)
  69. {
  70. int r = rand();
  71. switch (r % 3)
  72. {
  73. case 0:
  74. return STARPU_R;
  75. case 1:
  76. return STARPU_W;
  77. case 2:
  78. return STARPU_RW;
  79. };
  80. return STARPU_RW;
  81. }
  82. static struct starpu_codelet cl_r_r =
  83. {
  84. .cpu_funcs = {cpu_codelet_null},
  85. .cuda_funcs = {cuda_codelet_null},
  86. .opencl_funcs = {opencl_codelet_null},
  87. .cpu_funcs_name = {"cpu_codelet_null"},
  88. .nbuffers = 2,
  89. .modes = {STARPU_R, STARPU_R}
  90. };
  91. static struct starpu_codelet cl_r_w =
  92. {
  93. .cpu_funcs = {cpu_codelet_null},
  94. .cuda_funcs = {cuda_codelet_null},
  95. .opencl_funcs = {opencl_codelet_null},
  96. .cpu_funcs_name = {"cpu_codelet_null"},
  97. .nbuffers = 2,
  98. .modes = {STARPU_R, STARPU_W}
  99. };
  100. static struct starpu_codelet cl_r_rw =
  101. {
  102. .cpu_funcs = {cpu_codelet_null},
  103. .cuda_funcs = {cuda_codelet_null},
  104. .opencl_funcs = {opencl_codelet_null},
  105. .cpu_funcs_name = {"cpu_codelet_null"},
  106. .nbuffers = 2,
  107. .modes = {STARPU_R, STARPU_RW}
  108. };
  109. static struct starpu_codelet cl_w_r =
  110. {
  111. .cpu_funcs = {cpu_codelet_null},
  112. .cuda_funcs = {cuda_codelet_null},
  113. .opencl_funcs = {opencl_codelet_null},
  114. .cpu_funcs_name = {"cpu_codelet_null"},
  115. .nbuffers = 2,
  116. .modes = {STARPU_W, STARPU_R}
  117. };
  118. static struct starpu_codelet cl_w_w =
  119. {
  120. .cpu_funcs = {cpu_codelet_null},
  121. .cuda_funcs = {cuda_codelet_null},
  122. .opencl_funcs = {opencl_codelet_null},
  123. .cpu_funcs_name = {"cpu_codelet_null"},
  124. .nbuffers = 2,
  125. .modes = {STARPU_W, STARPU_W}
  126. };
  127. static struct starpu_codelet cl_w_rw =
  128. {
  129. .cpu_funcs = {cpu_codelet_null},
  130. .cuda_funcs = {cuda_codelet_null},
  131. .opencl_funcs = {opencl_codelet_null},
  132. .cpu_funcs_name = {"cpu_codelet_null"},
  133. .nbuffers = 2,
  134. .modes = {STARPU_W, STARPU_RW}
  135. };
  136. static struct starpu_codelet cl_rw_r =
  137. {
  138. .cpu_funcs = {cpu_codelet_null},
  139. .cuda_funcs = {cuda_codelet_null},
  140. .opencl_funcs = {opencl_codelet_null},
  141. .cpu_funcs_name = {"cpu_codelet_null"},
  142. .nbuffers = 2,
  143. .modes = {STARPU_RW, STARPU_R}
  144. };
  145. static struct starpu_codelet cl_rw_w =
  146. {
  147. .cpu_funcs = {cpu_codelet_null},
  148. .cuda_funcs = {cuda_codelet_null},
  149. .opencl_funcs = {opencl_codelet_null},
  150. .cpu_funcs_name = {"cpu_codelet_null"},
  151. .nbuffers = 2,
  152. .modes = {STARPU_RW, STARPU_W}
  153. };
  154. static struct starpu_codelet cl_rw_rw =
  155. {
  156. .cpu_funcs = {cpu_codelet_null},
  157. .cuda_funcs = {cuda_codelet_null},
  158. .opencl_funcs = {opencl_codelet_null},
  159. .cpu_funcs_name = {"cpu_codelet_null"},
  160. .nbuffers = 2,
  161. .modes = {STARPU_RW, STARPU_RW}
  162. };
  163. int main(int argc, char **argv)
  164. {
  165. int ret;
  166. ret = starpu_initialize(NULL, &argc, &argv);
  167. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  169. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  171. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  172. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  173. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  174. starpu_vector_data_register(&v_handle2, STARPU_MAIN_RAM, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  175. unsigned iter;
  176. for (iter = 0; iter < N; iter++)
  177. {
  178. struct starpu_task *task = starpu_task_create();
  179. task->handles[0] = v_handle;
  180. task->handles[1] = v_handle2;
  181. enum starpu_data_access_mode mode0 = select_random_mode();
  182. enum starpu_data_access_mode mode1 = select_random_mode();
  183. if (mode0 == STARPU_R && mode1 == STARPU_R)
  184. task->cl = &cl_r_r;
  185. else if (mode0 == STARPU_R && mode1 == STARPU_W)
  186. task->cl = &cl_r_w;
  187. else if (mode0 == STARPU_R && mode1 == STARPU_RW)
  188. task->cl = &cl_r_rw;
  189. else if (mode0 == STARPU_W && mode1 == STARPU_R)
  190. task->cl = &cl_w_r;
  191. else if (mode0 == STARPU_W && mode1 == STARPU_W)
  192. task->cl = &cl_w_w;
  193. else if (mode0 == STARPU_W && mode1 == STARPU_RW)
  194. task->cl = &cl_w_rw;
  195. else if (mode0 == STARPU_RW && mode1 == STARPU_R)
  196. task->cl = &cl_rw_r;
  197. else if (mode0 == STARPU_RW && mode1 == STARPU_W)
  198. task->cl = &cl_rw_w;
  199. else if (mode0 == STARPU_RW && mode1 == STARPU_RW)
  200. task->cl = &cl_rw_rw;
  201. task->callback_func = callback;
  202. task->callback_arg = NULL;
  203. ret = starpu_task_submit(task);
  204. if (ret == -ENODEV) goto enodev;
  205. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  206. }
  207. starpu_do_schedule();
  208. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  209. if (!finished)
  210. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  211. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  212. starpu_data_unregister(v_handle);
  213. starpu_data_unregister(v_handle2);
  214. starpu_free(v);
  215. starpu_free(v2);
  216. starpu_shutdown();
  217. return EXIT_SUCCESS;
  218. enodev:
  219. starpu_data_unregister(v_handle);
  220. starpu_data_unregister(v_handle2);
  221. starpu_free(v);
  222. starpu_free(v2);
  223. starpu_shutdown();
  224. fprintf(stderr, "WARNING: No one can execute this task\n");
  225. /* yes, we do not perform the computation but we did detect that no one
  226. * could perform the kernel, so this is not an error from StarPU */
  227. return STARPU_TEST_SKIPPED;
  228. }