dsm_stress.c 6.7 KB

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