mpi_like_async.c 10 KB

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