mpi_like_async.c 10 KB

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