mpi_like_async.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 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 <config.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. #include <common/thread.h>
  21. #ifdef STARPU_QUICK_CHECK
  22. # define NTHREADS_DEFAULT 4
  23. # define NITER_DEFAULT 8
  24. #else
  25. # define NTHREADS_DEFAULT 16
  26. # define NITER_DEFAULT 128
  27. #endif
  28. static unsigned nthreads = NTHREADS_DEFAULT;
  29. static unsigned niter = NITER_DEFAULT;
  30. //#define DEBUG_MESSAGES 1
  31. //static starpu_pthread_cond_t cond;
  32. //static starpu_pthread_mutex_t mutex;
  33. struct thread_data
  34. {
  35. unsigned index;
  36. unsigned val;
  37. starpu_data_handle_t handle;
  38. starpu_pthread_t thread;
  39. starpu_pthread_mutex_t recv_mutex;
  40. unsigned recv_flag; // set when a message is received
  41. unsigned recv_buf;
  42. struct thread_data *neighbour;
  43. };
  44. struct data_req
  45. {
  46. int (*test_func)(void *);
  47. void *test_arg;
  48. struct data_req *next;
  49. };
  50. static starpu_pthread_mutex_t data_req_mutex;
  51. static starpu_pthread_cond_t data_req_cond;
  52. struct data_req *data_req_list;
  53. unsigned progress_thread_running;
  54. static struct thread_data problem_data[NTHREADS_DEFAULT];
  55. /* We implement some ring transfer, every thread will try to receive a piece of
  56. * data from its neighbour and increment it before transmitting it to its
  57. * successor. */
  58. #ifdef STARPU_USE_CUDA
  59. void cuda_codelet_unsigned_inc(void *descr[], STARPU_ATTRIBUTE_UNUSED void *cl_arg);
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  63. #endif
  64. void increment_handle_cpu_kernel(void *descr[], void *cl_arg STARPU_ATTRIBUTE_UNUSED)
  65. {
  66. STARPU_SKIP_IF_VALGRIND;
  67. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  68. *val += 1;
  69. // FPRINTF(stderr, "VAL %d (&val = %p)\n", *val, val);
  70. }
  71. static struct starpu_codelet increment_handle_cl =
  72. {
  73. .modes = { STARPU_RW },
  74. .cpu_funcs = {increment_handle_cpu_kernel, NULL},
  75. #ifdef STARPU_USE_CUDA
  76. .cuda_funcs = {cuda_codelet_unsigned_inc, NULL},
  77. #endif
  78. #ifdef STARPU_USE_OPENCL
  79. .opencl_funcs = { opencl_codelet_unsigned_inc, NULL},
  80. #endif
  81. .cpu_funcs_name = {"increment_handle_cpu_kernel", NULL},
  82. .nbuffers = 1
  83. };
  84. static void increment_handle_async(struct thread_data *thread_data)
  85. {
  86. struct starpu_task *task = starpu_task_create();
  87. task->cl = &increment_handle_cl;
  88. task->handles[0] = thread_data->handle;
  89. task->detach = 1;
  90. task->destroy = 1;
  91. int ret = starpu_task_submit(task);
  92. if (ret == -ENODEV)
  93. exit(STARPU_TEST_SKIPPED);
  94. STARPU_ASSERT(!ret);
  95. }
  96. static int test_recv_handle_async(void *arg)
  97. {
  98. // FPRINTF(stderr, "test_recv_handle_async\n");
  99. int ret;
  100. struct thread_data *thread_data = (struct thread_data *) arg;
  101. STARPU_PTHREAD_MUTEX_LOCK(&thread_data->recv_mutex);
  102. ret = (thread_data->recv_flag == 1);
  103. if (ret)
  104. {
  105. thread_data->recv_flag = 0;
  106. thread_data->val = thread_data->recv_buf;
  107. }
  108. STARPU_PTHREAD_MUTEX_UNLOCK(&thread_data->recv_mutex);
  109. if (ret)
  110. {
  111. #ifdef DEBUG_MESSAGES
  112. FPRINTF(stderr, "Thread %d received value %d from thread %d\n",
  113. thread_data->index, thread_data->val, (thread_data->index - 1)%nthreads);
  114. #endif
  115. starpu_data_release(thread_data->handle);
  116. }
  117. return ret;
  118. }
  119. static void recv_handle_async(void *_thread_data)
  120. {
  121. struct thread_data *thread_data = (struct thread_data *) _thread_data;
  122. struct data_req *req = (struct data_req *) malloc(sizeof(struct data_req));
  123. req->test_func = test_recv_handle_async;
  124. req->test_arg = thread_data;
  125. req->next = NULL;
  126. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  127. req->next = data_req_list;
  128. data_req_list = req;
  129. STARPU_PTHREAD_COND_SIGNAL(&data_req_cond);
  130. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  131. }
  132. static int test_send_handle_async(void *arg)
  133. {
  134. int ret;
  135. struct thread_data *thread_data = (struct thread_data *) arg;
  136. struct thread_data *neighbour_data = thread_data->neighbour;
  137. STARPU_PTHREAD_MUTEX_LOCK(&neighbour_data->recv_mutex);
  138. ret = (neighbour_data->recv_flag == 0);
  139. STARPU_PTHREAD_MUTEX_UNLOCK(&neighbour_data->recv_mutex);
  140. if (ret)
  141. {
  142. #ifdef DEBUG_MESSAGES
  143. FPRINTF(stderr, "Thread %d sends value %d to thread %d\n", thread_data->index, thread_data->val, neighbour_data->index);
  144. #endif
  145. starpu_data_release(thread_data->handle);
  146. }
  147. return ret;
  148. }
  149. static void send_handle_async(void *_thread_data)
  150. {
  151. struct thread_data *thread_data = (struct thread_data *) _thread_data;
  152. struct thread_data *neighbour_data = thread_data->neighbour;
  153. // FPRINTF(stderr, "send_handle_async\n");
  154. /* send the message */
  155. STARPU_PTHREAD_MUTEX_LOCK(&neighbour_data->recv_mutex);
  156. neighbour_data->recv_buf = thread_data->val;
  157. neighbour_data->recv_flag = 1;
  158. STARPU_PTHREAD_MUTEX_UNLOCK(&neighbour_data->recv_mutex);
  159. struct data_req *req = (struct data_req *) malloc(sizeof(struct data_req));
  160. req->test_func = test_send_handle_async;
  161. req->test_arg = thread_data;
  162. req->next = NULL;
  163. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  164. req->next = data_req_list;
  165. data_req_list = req;
  166. STARPU_PTHREAD_COND_SIGNAL(&data_req_cond);
  167. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  168. }
  169. static void *progress_func(void *arg)
  170. {
  171. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  172. progress_thread_running = 1;
  173. STARPU_PTHREAD_COND_SIGNAL(&data_req_cond);
  174. while (progress_thread_running)
  175. {
  176. struct data_req *req;
  177. if (data_req_list == NULL)
  178. STARPU_PTHREAD_COND_WAIT(&data_req_cond, &data_req_mutex);
  179. req = data_req_list;
  180. if (req)
  181. {
  182. data_req_list = req->next;
  183. req->next = NULL;
  184. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  185. int ret = req->test_func(req->test_arg);
  186. if (ret)
  187. {
  188. free(req);
  189. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  190. }
  191. else
  192. {
  193. /* ret = 0 : the request is not finished, we put it back at the end of the list */
  194. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  195. struct data_req *req_aux = data_req_list;
  196. if (!req_aux)
  197. {
  198. /* The list is empty */
  199. data_req_list = req;
  200. }
  201. else
  202. {
  203. while (req_aux)
  204. {
  205. if (req_aux->next == NULL)
  206. {
  207. req_aux->next = req;
  208. break;
  209. }
  210. req_aux = req_aux->next;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  217. return NULL;
  218. }
  219. static void *thread_func(void *arg)
  220. {
  221. unsigned iter;
  222. struct thread_data *thread_data = (struct thread_data *) arg;
  223. unsigned index = thread_data->index;
  224. int ret;
  225. starpu_variable_data_register(&thread_data->handle, 0, (uintptr_t)&thread_data->val, sizeof(unsigned));
  226. for (iter = 0; iter < niter; iter++)
  227. {
  228. /* The first thread initiates the first transfer */
  229. if (!((index == 0) && (iter == 0)))
  230. {
  231. starpu_data_acquire_cb(
  232. thread_data->handle, STARPU_W,
  233. recv_handle_async, thread_data
  234. );
  235. }
  236. increment_handle_async(thread_data);
  237. if (!((index == (nthreads - 1)) && (iter == (niter - 1))))
  238. {
  239. starpu_data_acquire_cb(
  240. thread_data->handle, STARPU_R,
  241. send_handle_async, thread_data
  242. );
  243. }
  244. }
  245. ret = starpu_task_wait_for_all();
  246. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  247. return NULL;
  248. }
  249. #ifdef STARPU_USE_OPENCL
  250. struct starpu_opencl_program opencl_program;
  251. #endif
  252. int main(int argc, char **argv)
  253. {
  254. int ret;
  255. void *retval;
  256. #ifdef STARPU_QUICK_CHECK
  257. niter /= 16;
  258. nthreads /= 4;
  259. #endif
  260. ret = starpu_initialize(NULL, &argc, &argv);
  261. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  262. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  263. #ifdef STARPU_USE_OPENCL
  264. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  265. &opencl_program, NULL);
  266. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  267. #endif
  268. /* Create a thread to perform blocking calls */
  269. starpu_pthread_t progress_thread;
  270. STARPU_PTHREAD_MUTEX_INIT(&data_req_mutex, NULL);
  271. STARPU_PTHREAD_COND_INIT(&data_req_cond, NULL);
  272. data_req_list = NULL;
  273. progress_thread_running = 0;
  274. unsigned t;
  275. for (t = 0; t < nthreads; t++)
  276. {
  277. problem_data[t].index = t;
  278. problem_data[t].val = 0;
  279. STARPU_PTHREAD_MUTEX_INIT(&problem_data[t].recv_mutex, NULL);
  280. problem_data[t].recv_flag = 0;
  281. problem_data[t].neighbour = &problem_data[(t+1)%nthreads];
  282. }
  283. starpu_pthread_create(&progress_thread, NULL, progress_func, NULL);
  284. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  285. while (!progress_thread_running)
  286. STARPU_PTHREAD_COND_WAIT(&data_req_cond, &data_req_mutex);
  287. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  288. for (t = 0; t < nthreads; t++)
  289. {
  290. ret = starpu_pthread_create(&problem_data[t].thread, NULL, thread_func, &problem_data[t]);
  291. STARPU_ASSERT(!ret);
  292. }
  293. for (t = 0; t < nthreads; t++)
  294. {
  295. ret = starpu_pthread_join(problem_data[t].thread, &retval);
  296. STARPU_ASSERT(!ret);
  297. STARPU_ASSERT(retval == NULL);
  298. }
  299. STARPU_PTHREAD_MUTEX_LOCK(&data_req_mutex);
  300. progress_thread_running = 0;
  301. STARPU_PTHREAD_COND_SIGNAL(&data_req_cond);
  302. STARPU_PTHREAD_MUTEX_UNLOCK(&data_req_mutex);
  303. ret = starpu_pthread_join(progress_thread, &retval);
  304. STARPU_ASSERT(!ret);
  305. STARPU_ASSERT(retval == NULL);
  306. /* We check that the value in the "last" thread is valid */
  307. starpu_data_handle_t last_handle = problem_data[nthreads - 1].handle;
  308. starpu_data_acquire(last_handle, STARPU_R);
  309. #ifdef STARPU_USE_OPENCL
  310. ret = starpu_opencl_unload_opencl(&opencl_program);
  311. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  312. #endif
  313. ret = EXIT_SUCCESS;
  314. if (problem_data[nthreads - 1].val != (nthreads * niter))
  315. {
  316. FPRINTF(stderr, "Final value : %u should be %d\n", problem_data[nthreads - 1].val, (nthreads * niter));
  317. ret = EXIT_FAILURE;
  318. }
  319. starpu_data_release(last_handle);
  320. for (t = 0; t < nthreads; t++)
  321. {
  322. starpu_data_unregister(problem_data[t].handle);
  323. }
  324. starpu_shutdown();
  325. STARPU_RETURN(ret);
  326. }