dsm_stress.c 6.5 KB

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