mpi_like_async.c 10 KB

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