dsm_stress.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <pthread.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. #ifdef STARPU_QUICK_CHECK
  26. # define N 100
  27. #else
  28. # define N 10000
  29. #endif
  30. #define VECTORSIZE 1024
  31. static _starpu_pthread_mutex_t mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  32. static _starpu_pthread_cond_t cond = _STARPU_PTHREAD_COND_INITIALIZER;
  33. static unsigned finished = 0;
  34. static unsigned cnt = N;
  35. starpu_data_handle_t v_handle, v_handle2;
  36. static unsigned *v;
  37. static unsigned *v2;
  38. static void callback(void *arg)
  39. {
  40. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  41. if (res == 0)
  42. {
  43. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  44. finished = 1;
  45. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  46. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  47. }
  48. }
  49. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  50. {
  51. }
  52. static void opencl_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  53. {
  54. }
  55. static void cpu_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  56. {
  57. }
  58. static enum starpu_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. .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. .nbuffers = 2,
  86. .modes = {STARPU_R, STARPU_W}
  87. };
  88. static struct starpu_codelet cl_r_rw =
  89. {
  90. .cpu_funcs = {cpu_codelet_null, NULL},
  91. .cuda_funcs = {cuda_codelet_null, NULL},
  92. .opencl_funcs = {opencl_codelet_null, NULL},
  93. .nbuffers = 2,
  94. .modes = {STARPU_R, STARPU_RW}
  95. };
  96. static struct starpu_codelet cl_w_r =
  97. {
  98. .cpu_funcs = {cpu_codelet_null, NULL},
  99. .cuda_funcs = {cuda_codelet_null, NULL},
  100. .opencl_funcs = {opencl_codelet_null, NULL},
  101. .nbuffers = 2,
  102. .modes = {STARPU_W, STARPU_R}
  103. };
  104. static struct starpu_codelet cl_w_w =
  105. {
  106. .cpu_funcs = {cpu_codelet_null, NULL},
  107. .cuda_funcs = {cuda_codelet_null, NULL},
  108. .opencl_funcs = {opencl_codelet_null, NULL},
  109. .nbuffers = 2,
  110. .modes = {STARPU_W, STARPU_W}
  111. };
  112. static struct starpu_codelet cl_w_rw =
  113. {
  114. .cpu_funcs = {cpu_codelet_null, NULL},
  115. .cuda_funcs = {cuda_codelet_null, NULL},
  116. .opencl_funcs = {opencl_codelet_null, NULL},
  117. .nbuffers = 2,
  118. .modes = {STARPU_W, STARPU_RW}
  119. };
  120. static struct starpu_codelet cl_rw_r =
  121. {
  122. .cpu_funcs = {cpu_codelet_null, NULL},
  123. .cuda_funcs = {cuda_codelet_null, NULL},
  124. .opencl_funcs = {opencl_codelet_null, NULL},
  125. .nbuffers = 2,
  126. .modes = {STARPU_RW, STARPU_R}
  127. };
  128. static struct starpu_codelet cl_rw_w =
  129. {
  130. .cpu_funcs = {cpu_codelet_null, NULL},
  131. .cuda_funcs = {cuda_codelet_null, NULL},
  132. .opencl_funcs = {opencl_codelet_null, NULL},
  133. .nbuffers = 2,
  134. .modes = {STARPU_RW, STARPU_W}
  135. };
  136. static struct starpu_codelet cl_rw_rw =
  137. {
  138. .cpu_funcs = {cpu_codelet_null, NULL},
  139. .cuda_funcs = {cuda_codelet_null, NULL},
  140. .opencl_funcs = {opencl_codelet_null, NULL},
  141. .nbuffers = 2,
  142. .modes = {STARPU_RW, STARPU_RW}
  143. };
  144. int main(int argc, char **argv)
  145. {
  146. int ret;
  147. ret = starpu_init(NULL);
  148. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  149. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  150. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  152. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  154. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  155. starpu_vector_data_register(&v_handle2, 0, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  156. unsigned iter;
  157. for (iter = 0; iter < N; iter++)
  158. {
  159. struct starpu_task *task = starpu_task_create();
  160. task->handles[0] = v_handle;
  161. task->handles[1] = v_handle2;
  162. enum starpu_access_mode mode0 = select_random_mode();
  163. enum starpu_access_mode mode1 = select_random_mode();
  164. if (mode0 == STARPU_R && mode1 == STARPU_R)
  165. task->cl = &cl_r_r;
  166. else if (mode0 == STARPU_R && mode1 == STARPU_W)
  167. task->cl = &cl_r_w;
  168. else if (mode0 == STARPU_R && mode1 == STARPU_RW)
  169. task->cl = &cl_r_rw;
  170. else if (mode0 == STARPU_W && mode1 == STARPU_R)
  171. task->cl = &cl_w_r;
  172. else if (mode0 == STARPU_W && mode1 == STARPU_W)
  173. task->cl = &cl_w_w;
  174. else if (mode0 == STARPU_W && mode1 == STARPU_RW)
  175. task->cl = &cl_w_rw;
  176. else if (mode0 == STARPU_RW && mode1 == STARPU_R)
  177. task->cl = &cl_rw_r;
  178. else if (mode0 == STARPU_RW && mode1 == STARPU_W)
  179. task->cl = &cl_rw_w;
  180. else if (mode0 == STARPU_RW && mode1 == STARPU_RW)
  181. task->cl = &cl_rw_rw;
  182. task->callback_func = callback;
  183. task->callback_arg = NULL;
  184. ret = starpu_task_submit(task);
  185. if (ret == -ENODEV) goto enodev;
  186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  187. }
  188. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  189. if (!finished)
  190. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  191. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  192. starpu_data_unregister(v_handle);
  193. starpu_data_unregister(v_handle2);
  194. starpu_free(v);
  195. starpu_free(v2);
  196. starpu_shutdown();
  197. return EXIT_SUCCESS;
  198. enodev:
  199. starpu_data_unregister(v_handle);
  200. starpu_data_unregister(v_handle2);
  201. starpu_free(v);
  202. starpu_free(v2);
  203. starpu_shutdown();
  204. fprintf(stderr, "WARNING: No one can execute this task\n");
  205. /* yes, we do not perform the computation but we did detect that no one
  206. * could perform the kernel, so this is not an error from StarPU */
  207. return STARPU_TEST_SKIPPED;
  208. }