mpi_like_async.c 10 KB

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