mpi_like_async.c 8.9 KB

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