dsm_stress.c 6.7 KB

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