mpi_like_async.c 9.0 KB

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