dsm_stress.c 6.3 KB

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