mpi_like_async.c 8.9 KB

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