dsm_stress.c 6.7 KB

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