dsm_stress.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 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. 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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  54. {
  55. }
  56. static void opencl_codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  57. {
  58. }
  59. void cpu_codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  60. {
  61. }
  62. static enum starpu_data_access_mode select_random_mode(void)
  63. {
  64. int r = rand();
  65. switch (r % 3)
  66. {
  67. case 0:
  68. return STARPU_R;
  69. case 1:
  70. return STARPU_W;
  71. case 2:
  72. return STARPU_RW;
  73. };
  74. return STARPU_RW;
  75. }
  76. static struct starpu_codelet cl_r_r =
  77. {
  78. .cpu_funcs = {cpu_codelet_null},
  79. .cuda_funcs = {cuda_codelet_null},
  80. .opencl_funcs = {opencl_codelet_null},
  81. .cpu_funcs_name = {"cpu_codelet_null"},
  82. .nbuffers = 2,
  83. .modes = {STARPU_R, STARPU_R}
  84. };
  85. static struct starpu_codelet cl_r_w =
  86. {
  87. .cpu_funcs = {cpu_codelet_null},
  88. .cuda_funcs = {cuda_codelet_null},
  89. .opencl_funcs = {opencl_codelet_null},
  90. .cpu_funcs_name = {"cpu_codelet_null"},
  91. .nbuffers = 2,
  92. .modes = {STARPU_R, STARPU_W}
  93. };
  94. static struct starpu_codelet cl_r_rw =
  95. {
  96. .cpu_funcs = {cpu_codelet_null},
  97. .cuda_funcs = {cuda_codelet_null},
  98. .opencl_funcs = {opencl_codelet_null},
  99. .cpu_funcs_name = {"cpu_codelet_null"},
  100. .nbuffers = 2,
  101. .modes = {STARPU_R, STARPU_RW}
  102. };
  103. static struct starpu_codelet cl_w_r =
  104. {
  105. .cpu_funcs = {cpu_codelet_null},
  106. .cuda_funcs = {cuda_codelet_null},
  107. .opencl_funcs = {opencl_codelet_null},
  108. .cpu_funcs_name = {"cpu_codelet_null"},
  109. .nbuffers = 2,
  110. .modes = {STARPU_W, STARPU_R}
  111. };
  112. static struct starpu_codelet cl_w_w =
  113. {
  114. .cpu_funcs = {cpu_codelet_null},
  115. .cuda_funcs = {cuda_codelet_null},
  116. .opencl_funcs = {opencl_codelet_null},
  117. .cpu_funcs_name = {"cpu_codelet_null"},
  118. .nbuffers = 2,
  119. .modes = {STARPU_W, STARPU_W}
  120. };
  121. static struct starpu_codelet cl_w_rw =
  122. {
  123. .cpu_funcs = {cpu_codelet_null},
  124. .cuda_funcs = {cuda_codelet_null},
  125. .opencl_funcs = {opencl_codelet_null},
  126. .cpu_funcs_name = {"cpu_codelet_null"},
  127. .nbuffers = 2,
  128. .modes = {STARPU_W, STARPU_RW}
  129. };
  130. static struct starpu_codelet cl_rw_r =
  131. {
  132. .cpu_funcs = {cpu_codelet_null},
  133. .cuda_funcs = {cuda_codelet_null},
  134. .opencl_funcs = {opencl_codelet_null},
  135. .cpu_funcs_name = {"cpu_codelet_null"},
  136. .nbuffers = 2,
  137. .modes = {STARPU_RW, STARPU_R}
  138. };
  139. static struct starpu_codelet cl_rw_w =
  140. {
  141. .cpu_funcs = {cpu_codelet_null},
  142. .cuda_funcs = {cuda_codelet_null},
  143. .opencl_funcs = {opencl_codelet_null},
  144. .cpu_funcs_name = {"cpu_codelet_null"},
  145. .nbuffers = 2,
  146. .modes = {STARPU_RW, STARPU_W}
  147. };
  148. static struct starpu_codelet cl_rw_rw =
  149. {
  150. .cpu_funcs = {cpu_codelet_null},
  151. .cuda_funcs = {cuda_codelet_null},
  152. .opencl_funcs = {opencl_codelet_null},
  153. .cpu_funcs_name = {"cpu_codelet_null"},
  154. .nbuffers = 2,
  155. .modes = {STARPU_RW, STARPU_RW}
  156. };
  157. int main(int argc, char **argv)
  158. {
  159. int ret;
  160. ret = starpu_initialize(NULL, &argc, &argv);
  161. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  163. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  164. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  165. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  166. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  167. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  168. starpu_vector_data_register(&v_handle2, STARPU_MAIN_RAM, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  169. unsigned iter;
  170. for (iter = 0; iter < N; iter++)
  171. {
  172. struct starpu_task *task = starpu_task_create();
  173. task->handles[0] = v_handle;
  174. task->handles[1] = v_handle2;
  175. enum starpu_data_access_mode mode0 = select_random_mode();
  176. enum starpu_data_access_mode mode1 = select_random_mode();
  177. if (mode0 == STARPU_R && mode1 == STARPU_R)
  178. task->cl = &cl_r_r;
  179. else if (mode0 == STARPU_R && mode1 == STARPU_W)
  180. task->cl = &cl_r_w;
  181. else if (mode0 == STARPU_R && mode1 == STARPU_RW)
  182. task->cl = &cl_r_rw;
  183. else if (mode0 == STARPU_W && mode1 == STARPU_R)
  184. task->cl = &cl_w_r;
  185. else if (mode0 == STARPU_W && mode1 == STARPU_W)
  186. task->cl = &cl_w_w;
  187. else if (mode0 == STARPU_W && mode1 == STARPU_RW)
  188. task->cl = &cl_w_rw;
  189. else if (mode0 == STARPU_RW && mode1 == STARPU_R)
  190. task->cl = &cl_rw_r;
  191. else if (mode0 == STARPU_RW && mode1 == STARPU_W)
  192. task->cl = &cl_rw_w;
  193. else if (mode0 == STARPU_RW && mode1 == STARPU_RW)
  194. task->cl = &cl_rw_rw;
  195. task->callback_func = callback;
  196. task->callback_arg = NULL;
  197. ret = starpu_task_submit(task);
  198. if (ret == -ENODEV) goto enodev;
  199. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  200. }
  201. starpu_do_schedule();
  202. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  203. if (!finished)
  204. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  205. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  206. starpu_data_unregister(v_handle);
  207. starpu_data_unregister(v_handle2);
  208. starpu_free(v);
  209. starpu_free(v2);
  210. starpu_shutdown();
  211. return EXIT_SUCCESS;
  212. enodev:
  213. starpu_data_unregister(v_handle);
  214. starpu_data_unregister(v_handle2);
  215. starpu_free(v);
  216. starpu_free(v2);
  217. starpu_shutdown();
  218. fprintf(stderr, "WARNING: No one can execute this task\n");
  219. /* yes, we do not perform the computation but we did detect that no one
  220. * could perform the kernel, so this is not an error from StarPU */
  221. return STARPU_TEST_SKIPPED;
  222. }