mpi_like_async.c 8.2 KB

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