dsm_stress.c 6.7 KB

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