mpi_like_async.c 9.8 KB

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