dsm_stress.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012, 2015 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. #ifdef STARPU_QUICK_CHECK
  25. # define N 100
  26. #else
  27. # define N 10000
  28. #endif
  29. #define VECTORSIZE 1024
  30. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  31. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  32. static unsigned finished = 0;
  33. static unsigned cnt = N;
  34. starpu_data_handle_t v_handle, v_handle2;
  35. static unsigned *v;
  36. static unsigned *v2;
  37. static void callback(void *arg)
  38. {
  39. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  40. ANNOTATE_HAPPENS_BEFORE(&cnt);
  41. if (res == 0)
  42. {
  43. ANNOTATE_HAPPENS_AFTER(&cnt);
  44. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  45. finished = 1;
  46. STARPU_PTHREAD_COND_SIGNAL(&cond);
  47. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  48. }
  49. }
  50. static void cuda_codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  51. {
  52. }
  53. static void opencl_codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  54. {
  55. }
  56. void cpu_codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  57. {
  58. }
  59. static enum starpu_data_access_mode select_random_mode(void)
  60. {
  61. int r = rand();
  62. switch (r % 3)
  63. {
  64. case 0:
  65. return STARPU_R;
  66. case 1:
  67. return STARPU_W;
  68. case 2:
  69. return STARPU_RW;
  70. };
  71. return STARPU_RW;
  72. }
  73. static struct starpu_codelet cl_r_r =
  74. {
  75. .cpu_funcs = {cpu_codelet_null},
  76. .cuda_funcs = {cuda_codelet_null},
  77. .opencl_funcs = {opencl_codelet_null},
  78. .cpu_funcs_name = {"cpu_codelet_null"},
  79. .nbuffers = 2,
  80. .modes = {STARPU_R, STARPU_R}
  81. };
  82. static struct starpu_codelet cl_r_w =
  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_W}
  90. };
  91. static struct starpu_codelet cl_r_rw =
  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_RW}
  99. };
  100. static struct starpu_codelet cl_w_r =
  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_W, STARPU_R}
  108. };
  109. static struct starpu_codelet cl_w_w =
  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_W}
  117. };
  118. static struct starpu_codelet cl_w_rw =
  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_RW}
  126. };
  127. static struct starpu_codelet cl_rw_r =
  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_RW, STARPU_R}
  135. };
  136. static struct starpu_codelet cl_rw_w =
  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_W}
  144. };
  145. static struct starpu_codelet cl_rw_rw =
  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_RW}
  153. };
  154. int main(int argc, char **argv)
  155. {
  156. int ret;
  157. ret = starpu_initialize(NULL, &argc, &argv);
  158. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  159. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  160. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  162. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  163. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  164. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  165. starpu_vector_data_register(&v_handle2, STARPU_MAIN_RAM, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  166. unsigned iter;
  167. for (iter = 0; iter < N; iter++)
  168. {
  169. struct starpu_task *task = starpu_task_create();
  170. task->handles[0] = v_handle;
  171. task->handles[1] = v_handle2;
  172. enum starpu_data_access_mode mode0 = select_random_mode();
  173. enum starpu_data_access_mode mode1 = select_random_mode();
  174. if (mode0 == STARPU_R && mode1 == STARPU_R)
  175. task->cl = &cl_r_r;
  176. else if (mode0 == STARPU_R && mode1 == STARPU_W)
  177. task->cl = &cl_r_w;
  178. else if (mode0 == STARPU_R && mode1 == STARPU_RW)
  179. task->cl = &cl_r_rw;
  180. else if (mode0 == STARPU_W && mode1 == STARPU_R)
  181. task->cl = &cl_w_r;
  182. else if (mode0 == STARPU_W && mode1 == STARPU_W)
  183. task->cl = &cl_w_w;
  184. else if (mode0 == STARPU_W && mode1 == STARPU_RW)
  185. task->cl = &cl_w_rw;
  186. else if (mode0 == STARPU_RW && mode1 == STARPU_R)
  187. task->cl = &cl_rw_r;
  188. else if (mode0 == STARPU_RW && mode1 == STARPU_W)
  189. task->cl = &cl_rw_w;
  190. else if (mode0 == STARPU_RW && mode1 == STARPU_RW)
  191. task->cl = &cl_rw_rw;
  192. task->callback_func = callback;
  193. task->callback_arg = NULL;
  194. ret = starpu_task_submit(task);
  195. if (ret == -ENODEV) goto enodev;
  196. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  197. }
  198. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  199. if (!finished)
  200. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  201. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  202. starpu_data_unregister(v_handle);
  203. starpu_data_unregister(v_handle2);
  204. starpu_free(v);
  205. starpu_free(v2);
  206. starpu_shutdown();
  207. return EXIT_SUCCESS;
  208. enodev:
  209. starpu_data_unregister(v_handle);
  210. starpu_data_unregister(v_handle2);
  211. starpu_free(v);
  212. starpu_free(v2);
  213. starpu_shutdown();
  214. fprintf(stderr, "WARNING: No one can execute this task\n");
  215. /* yes, we do not perform the computation but we did detect that no one
  216. * could perform the kernel, so this is not an error from StarPU */
  217. return STARPU_TEST_SKIPPED;
  218. }