mpi_like_async.c 9.2 KB

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