dsm_stress.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 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. #define N 10000
  25. #define VECTORSIZE 1024
  26. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  27. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  28. static unsigned finished = 0;
  29. static unsigned cnt = N;
  30. starpu_data_handle_t v_handle, v_handle2;
  31. static unsigned *v;
  32. static unsigned *v2;
  33. static void callback(void *arg)
  34. {
  35. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  36. if (res == 0)
  37. {
  38. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  39. finished = 1;
  40. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  41. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  42. }
  43. }
  44. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  45. {
  46. }
  47. static void opencl_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  48. {
  49. }
  50. static void cpu_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  51. {
  52. }
  53. static enum starpu_access_mode select_random_mode(void)
  54. {
  55. int r = rand();
  56. switch (r % 3)
  57. {
  58. case 0:
  59. return STARPU_R;
  60. case 1:
  61. return STARPU_W;
  62. case 2:
  63. return STARPU_RW;
  64. };
  65. return STARPU_RW;
  66. }
  67. static struct starpu_codelet cl_r_r =
  68. {
  69. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  70. .cpu_funcs = {cpu_codelet_null, NULL},
  71. .cuda_funcs = {cuda_codelet_null, NULL},
  72. .opencl_funcs = {opencl_codelet_null, NULL},
  73. .nbuffers = 2,
  74. .modes = {STARPU_R, STARPU_R}
  75. };
  76. static struct starpu_codelet cl_r_w =
  77. {
  78. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  79. .cpu_funcs = {cpu_codelet_null, NULL},
  80. .cuda_funcs = {cuda_codelet_null, NULL},
  81. .opencl_funcs = {opencl_codelet_null, NULL},
  82. .nbuffers = 2,
  83. .modes = {STARPU_R, STARPU_W}
  84. };
  85. static struct starpu_codelet cl_r_rw =
  86. {
  87. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  88. .cpu_funcs = {cpu_codelet_null, NULL},
  89. .cuda_funcs = {cuda_codelet_null, NULL},
  90. .opencl_funcs = {opencl_codelet_null, NULL},
  91. .nbuffers = 2,
  92. .modes = {STARPU_R, STARPU_RW}
  93. };
  94. static struct starpu_codelet cl_w_r =
  95. {
  96. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  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. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  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. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  115. .cpu_funcs = {cpu_codelet_null, NULL},
  116. .cuda_funcs = {cuda_codelet_null, NULL},
  117. .opencl_funcs = {opencl_codelet_null, NULL},
  118. .nbuffers = 2,
  119. .modes = {STARPU_W, STARPU_RW}
  120. };
  121. static struct starpu_codelet cl_rw_r =
  122. {
  123. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  124. .cpu_funcs = {cpu_codelet_null, NULL},
  125. .cuda_funcs = {cuda_codelet_null, NULL},
  126. .opencl_funcs = {opencl_codelet_null, NULL},
  127. .nbuffers = 2,
  128. .modes = {STARPU_RW, STARPU_R}
  129. };
  130. static struct starpu_codelet cl_rw_w =
  131. {
  132. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  133. .cpu_funcs = {cpu_codelet_null, NULL},
  134. .cuda_funcs = {cuda_codelet_null, NULL},
  135. .opencl_funcs = {opencl_codelet_null, NULL},
  136. .nbuffers = 2,
  137. .modes = {STARPU_RW, STARPU_W}
  138. };
  139. static struct starpu_codelet cl_rw_rw =
  140. {
  141. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  142. .cpu_funcs = {cpu_codelet_null, NULL},
  143. .cuda_funcs = {cuda_codelet_null, NULL},
  144. .opencl_funcs = {opencl_codelet_null, NULL},
  145. .nbuffers = 2,
  146. .modes = {STARPU_RW, STARPU_RW}
  147. };
  148. int main(int argc, char **argv)
  149. {
  150. int ret;
  151. ret = starpu_init(NULL);
  152. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  154. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  155. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  156. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  158. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  159. starpu_vector_data_register(&v_handle2, 0, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  160. unsigned iter;
  161. for (iter = 0; iter < N; iter++)
  162. {
  163. struct starpu_task *task = starpu_task_create();
  164. task->handles[0] = v_handle;
  165. task->handles[1] = v_handle2;
  166. enum starpu_access_mode mode0 = select_random_mode();
  167. enum starpu_access_mode mode1 = select_random_mode();
  168. if (mode0 == STARPU_R && mode1 == STARPU_R)
  169. task->cl = &cl_r_r;
  170. else if (mode0 == STARPU_R && mode1 == STARPU_W)
  171. task->cl = &cl_r_w;
  172. else if (mode0 == STARPU_R && mode1 == STARPU_RW)
  173. task->cl = &cl_r_rw;
  174. else if (mode0 == STARPU_W && mode1 == STARPU_R)
  175. task->cl = &cl_w_r;
  176. else if (mode0 == STARPU_W && mode1 == STARPU_W)
  177. task->cl = &cl_w_w;
  178. else if (mode0 == STARPU_W && mode1 == STARPU_RW)
  179. task->cl = &cl_w_rw;
  180. else if (mode0 == STARPU_RW && mode1 == STARPU_R)
  181. task->cl = &cl_rw_r;
  182. else if (mode0 == STARPU_RW && mode1 == STARPU_W)
  183. task->cl = &cl_rw_w;
  184. else if (mode0 == STARPU_RW && mode1 == STARPU_RW)
  185. task->cl = &cl_rw_rw;
  186. task->callback_func = callback;
  187. task->callback_arg = NULL;
  188. int ret = starpu_task_submit(task);
  189. if (ret == -ENODEV) goto enodev;
  190. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  191. }
  192. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  193. if (!finished)
  194. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  195. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  196. starpu_data_unregister(v_handle);
  197. starpu_data_unregister(v_handle2);
  198. starpu_free(v);
  199. starpu_free(v2);
  200. starpu_shutdown();
  201. return EXIT_SUCCESS;
  202. enodev:
  203. starpu_data_unregister(v_handle);
  204. starpu_data_unregister(v_handle2);
  205. starpu_free(v);
  206. starpu_free(v2);
  207. starpu_shutdown();
  208. fprintf(stderr, "WARNING: No one can execute this task\n");
  209. /* yes, we do not perform the computation but we did detect that no one
  210. * could perform the kernel, so this is not an error from StarPU */
  211. return STARPU_TEST_SKIPPED;
  212. }