reduction.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2017 Inria
  4. * Copyright (C) 2010-2014,2016-2017 Université de Bordeaux
  5. * Copyright (C) 2011-2013,2015-2017 CNRS
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <common/utils.h>
  21. #include <util/starpu_data_cpy.h>
  22. #include <core/task.h>
  23. #include <datawizard/datawizard.h>
  24. #include <drivers/mic/driver_mic_source.h>
  25. #include <drivers/mp_common/source_common.h>
  26. void starpu_data_set_reduction_methods(starpu_data_handle_t handle,
  27. struct starpu_codelet *redux_cl,
  28. struct starpu_codelet *init_cl)
  29. {
  30. _starpu_spin_lock(&handle->header_lock);
  31. _starpu_codelet_check_deprecated_fields(redux_cl);
  32. _starpu_codelet_check_deprecated_fields(init_cl);
  33. unsigned child;
  34. for (child = 0; child < handle->nchildren; child++)
  35. {
  36. /* make sure that the flags are applied to the children as well */
  37. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  38. if (child_handle->nchildren > 0)
  39. starpu_data_set_reduction_methods(child_handle, redux_cl, init_cl);
  40. }
  41. handle->redux_cl = redux_cl;
  42. handle->init_cl = init_cl;
  43. _starpu_spin_unlock(&handle->header_lock);
  44. }
  45. void _starpu_redux_init_data_replicate(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, int workerid)
  46. {
  47. STARPU_ASSERT(replicate);
  48. STARPU_ASSERT(replicate->allocated);
  49. struct starpu_codelet *init_cl = handle->init_cl;
  50. STARPU_ASSERT(init_cl);
  51. _starpu_cl_func_t init_func = NULL;
  52. /* TODO Check that worker may execute the codelet */
  53. switch (starpu_worker_get_type(workerid))
  54. {
  55. case STARPU_CPU_WORKER:
  56. init_func = _starpu_task_get_cpu_nth_implementation(init_cl, 0);
  57. break;
  58. case STARPU_CUDA_WORKER:
  59. init_func = _starpu_task_get_cuda_nth_implementation(init_cl, 0);
  60. #if defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  61. /* We make sure we do manipulate the proper device */
  62. starpu_cuda_set_device(starpu_worker_get_devid(workerid));
  63. #endif
  64. break;
  65. case STARPU_OPENCL_WORKER:
  66. init_func = _starpu_task_get_opencl_nth_implementation(init_cl, 0);
  67. break;
  68. #ifdef STARPU_USE_MIC
  69. case STARPU_MIC_WORKER:
  70. init_func = _starpu_mic_src_get_kernel_from_codelet(init_cl, 0);
  71. break;
  72. #endif
  73. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  74. case STARPU_MPI_MS_WORKER:
  75. init_func = _starpu_mpi_ms_src_get_kernel_from_codelet(init_cl, 0);
  76. break;
  77. #endif
  78. /* TODO: SCC */
  79. default:
  80. STARPU_ABORT();
  81. break;
  82. }
  83. STARPU_ASSERT(init_func);
  84. switch (starpu_worker_get_type(workerid))
  85. {
  86. #ifdef STARPU_USE_MIC
  87. case STARPU_MIC_WORKER:
  88. {
  89. struct _starpu_mp_node *node = _starpu_mic_src_get_actual_thread_mp_node();
  90. int devid = _starpu_get_worker_struct(workerid)->devid;
  91. void * arg;
  92. int arg_size;
  93. _starpu_src_common_execute_kernel(node,
  94. (void(*)(void))init_func, devid,
  95. STARPU_SEQ, 0, 0, &handle,
  96. &(replicate->data_interface), 1,
  97. NULL, 0, 1);
  98. _starpu_src_common_wait_completed_execution(node,devid,&arg,&arg_size);
  99. break;
  100. }
  101. #endif
  102. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  103. case STARPU_MPI_MS_WORKER:
  104. {
  105. struct _starpu_mp_node *node = _starpu_mpi_ms_src_get_actual_thread_mp_node();
  106. int devid = _starpu_get_worker_struct(workerid)->devid;
  107. void * arg;
  108. int arg_size;
  109. _starpu_src_common_execute_kernel(node,
  110. (void(*)(void))init_func, devid,
  111. STARPU_SEQ, 0, 0, &handle,
  112. &(replicate->data_interface), 1,
  113. NULL, 0 , 1);
  114. _starpu_src_common_wait_completed_execution(node,devid,&arg,&arg_size);
  115. break;
  116. }
  117. #endif
  118. default:
  119. init_func(&replicate->data_interface, NULL);
  120. break;
  121. }
  122. replicate->initialized = 1;
  123. }
  124. /* Enable reduction mode. This function must be called with the header lock
  125. * taken. */
  126. void _starpu_data_start_reduction_mode(starpu_data_handle_t handle)
  127. {
  128. STARPU_ASSERT(handle->reduction_refcnt == 0);
  129. if (!handle->per_worker)
  130. _starpu_data_initialize_per_worker(handle);
  131. unsigned worker;
  132. unsigned nworkers = starpu_worker_get_count();
  133. for (worker = 0; worker < nworkers; worker++)
  134. {
  135. struct _starpu_data_replicate *replicate;
  136. replicate = &handle->per_worker[worker];
  137. replicate->initialized = 0;
  138. replicate->relaxed_coherency = 2;
  139. if (replicate->mc)
  140. replicate->mc->relaxed_coherency = 2;
  141. }
  142. }
  143. //#define NO_TREE_REDUCTION
  144. /* Force reduction. The lock should already have been taken. */
  145. void _starpu_data_end_reduction_mode(starpu_data_handle_t handle)
  146. {
  147. unsigned worker;
  148. unsigned node;
  149. unsigned empty; /* Whether the handle is initially unallocated */
  150. /* Put every valid replicate in the same array */
  151. unsigned replicate_count = 0;
  152. starpu_data_handle_t replicate_array[1 + STARPU_NMAXWORKERS];
  153. _starpu_spin_checklocked(&handle->header_lock);
  154. for (node = 0; node < STARPU_MAXNODES; node++)
  155. {
  156. if (handle->per_node[node].state != STARPU_INVALID)
  157. break;
  158. }
  159. empty = node == STARPU_MAXNODES;
  160. #ifndef NO_TREE_REDUCTION
  161. if (!empty)
  162. /* Include the initial value into the reduction tree */
  163. replicate_array[replicate_count++] = handle;
  164. #endif
  165. /* Register all valid per-worker replicates */
  166. unsigned nworkers = starpu_worker_get_count();
  167. STARPU_ASSERT(!handle->reduction_tmp_handles);
  168. _STARPU_MALLOC(handle->reduction_tmp_handles, nworkers*sizeof(handle->reduction_tmp_handles[0]));
  169. for (worker = 0; worker < nworkers; worker++)
  170. {
  171. if (handle->per_worker[worker].initialized)
  172. {
  173. /* Make sure the replicate is not removed */
  174. handle->per_worker[worker].refcnt++;
  175. unsigned home_node = starpu_worker_get_memory_node(worker);
  176. starpu_data_register(&handle->reduction_tmp_handles[worker],
  177. home_node, handle->per_worker[worker].data_interface, handle->ops);
  178. starpu_data_set_sequential_consistency_flag(handle->reduction_tmp_handles[worker], 0);
  179. replicate_array[replicate_count++] = handle->reduction_tmp_handles[worker];
  180. }
  181. else
  182. {
  183. handle->reduction_tmp_handles[worker] = NULL;
  184. }
  185. }
  186. #ifndef NO_TREE_REDUCTION
  187. if (empty)
  188. {
  189. /* Only the final copy will touch the actual handle */
  190. handle->reduction_refcnt = 1;
  191. }
  192. else
  193. {
  194. unsigned step = 1;
  195. handle->reduction_refcnt = 0;
  196. while (step < replicate_count)
  197. {
  198. /* Each stage will touch the actual handle */
  199. handle->reduction_refcnt++;
  200. step *= 2;
  201. }
  202. }
  203. #else
  204. /* We know that in this reduction algorithm there is exactly one task per valid replicate. */
  205. handle->reduction_refcnt = replicate_count + empty;
  206. #endif
  207. // fprintf(stderr, "REDUX REFCNT = %d\n", handle->reduction_refcnt);
  208. if (replicate_count >
  209. #ifndef NO_TREE_REDUCTION
  210. !empty
  211. #else
  212. 0
  213. #endif
  214. )
  215. {
  216. /* Temporarily unlock the handle */
  217. _starpu_spin_unlock(&handle->header_lock);
  218. #ifndef NO_TREE_REDUCTION
  219. /* We will store a pointer to the last task which should modify the
  220. * replicate */
  221. struct starpu_task *last_replicate_deps[replicate_count];
  222. memset(last_replicate_deps, 0, replicate_count*sizeof(struct starpu_task *));
  223. struct starpu_task *redux_tasks[replicate_count];
  224. /* Redux step-by-step for step from 1 to replicate_count/2, i.e.
  225. * 1-by-1, then 2-by-2, then 4-by-4, etc. */
  226. unsigned step;
  227. unsigned redux_task_idx = 0;
  228. for (step = 1; step < replicate_count; step *=2)
  229. {
  230. unsigned i;
  231. for (i = 0; i < replicate_count; i+=2*step)
  232. {
  233. if (i + step < replicate_count)
  234. {
  235. /* Perform the reduction between replicates i
  236. * and i+step and put the result in replicate i */
  237. struct starpu_task *redux_task = starpu_task_create();
  238. redux_task->name = "redux_task_between_replicates";
  239. /* Mark these tasks so that StarPU does not block them
  240. * when they try to access the handle (normal tasks are
  241. * data requests to that handle are frozen until the
  242. * data is coherent again). */
  243. struct _starpu_job *j = _starpu_get_job_associated_to_task(redux_task);
  244. j->reduction_task = 1;
  245. redux_task->cl = handle->redux_cl;
  246. STARPU_ASSERT(redux_task->cl);
  247. if (!(STARPU_CODELET_GET_MODE(redux_task->cl, 0)))
  248. STARPU_CODELET_SET_MODE(redux_task->cl, STARPU_RW, 0);
  249. if (!(STARPU_CODELET_GET_MODE(redux_task->cl, 1)))
  250. STARPU_CODELET_SET_MODE(redux_task->cl, STARPU_R, 1);
  251. STARPU_ASSERT_MSG(STARPU_CODELET_GET_MODE(redux_task->cl, 0) == STARPU_RW, "First parameter of reduction codelet %p has to be RW", redux_task->cl);
  252. STARPU_ASSERT_MSG(STARPU_CODELET_GET_MODE(redux_task->cl, 1) == STARPU_R, "Second parameter of reduction codelet %p has to be R", redux_task->cl);
  253. STARPU_TASK_SET_HANDLE(redux_task, replicate_array[i], 0);
  254. STARPU_TASK_SET_HANDLE(redux_task, replicate_array[i+step], 1);
  255. int ndeps = 0;
  256. struct starpu_task *task_deps[2];
  257. if (last_replicate_deps[i])
  258. task_deps[ndeps++] = last_replicate_deps[i];
  259. if (last_replicate_deps[i+step])
  260. task_deps[ndeps++] = last_replicate_deps[i+step];
  261. /* i depends on this task */
  262. last_replicate_deps[i] = redux_task;
  263. /* we don't perform the reduction until both replicates are ready */
  264. starpu_task_declare_deps_array(redux_task, ndeps, task_deps);
  265. /* We cannot submit tasks here : we do
  266. * not want to depend on tasks that have
  267. * been completed, so we juste store
  268. * this task : it will be submitted
  269. * later. */
  270. redux_tasks[redux_task_idx++] = redux_task;
  271. }
  272. }
  273. }
  274. if (empty)
  275. /* The handle was empty, we just need to copy the reduced value. */
  276. _starpu_data_cpy(handle, replicate_array[0], 1, NULL, 0, 1, last_replicate_deps[0]);
  277. /* Let's submit all the reduction tasks. */
  278. unsigned i;
  279. for (i = 0; i < redux_task_idx; i++)
  280. {
  281. int ret = _starpu_task_submit_internally(redux_tasks[i]);
  282. STARPU_ASSERT(ret == 0);
  283. }
  284. #else
  285. if (empty)
  286. {
  287. struct starpu_task *redux_task = starpu_task_create();
  288. redux_task->name = "redux_task_empty";
  289. /* Mark these tasks so that StarPU does not block them
  290. * when they try to access the handle (normal tasks are
  291. * data requests to that handle are frozen until the
  292. * data is coherent again). */
  293. struct _starpu_job *j = _starpu_get_job_associated_to_task(redux_task);
  294. j->reduction_task = 1;
  295. redux_task->cl = handle->init_cl;
  296. STARPU_ASSERT(redux_task->cl);
  297. if (!(STARPU_CODELET_GET_MODE(redux_task->cl, 0)))
  298. STARPU_CODELET_SET_MODE(redux_task->cl, STARPU_W, 0);
  299. STARPU_ASSERT_MSG(STARPU_CODELET_GET_MODE(redux_task->cl, 0) == STARPU_W, "Parameter of initialization codelet %p has to be W", redux_task->cl);
  300. STARPU_TASK_SET_HANDLE(redux_task, handle, 0);
  301. int ret = _starpu_task_submit_internally(redux_task);
  302. STARPU_ASSERT(!ret);
  303. }
  304. /* Create a set of tasks to perform the reduction */
  305. unsigned replicate;
  306. for (replicate = 0; replicate < replicate_count; replicate++)
  307. {
  308. struct starpu_task *redux_task = starpu_task_create();
  309. redux_task->name = "redux_task_reduction";
  310. /* Mark these tasks so that StarPU does not block them
  311. * when they try to access the handle (normal tasks are
  312. * data requests to that handle are frozen until the
  313. * data is coherent again). */
  314. struct _starpu_job *j = _starpu_get_job_associated_to_task(redux_task);
  315. j->reduction_task = 1;
  316. redux_task->cl = handle->redux_cl;
  317. STARPU_ASSERT(redux_task->cl);
  318. if (!(STARPU_CODELET_GET_MODE(redux_task->cl, 0)))
  319. STARPU_CODELET_SET_MODE(redux_task->cl, STARPU_RW, 0);
  320. if (!(STARPU_CODELET_GET_MODE(redux_task->cl, 1)))
  321. STARPU_CODELET_SET_MODE(redux_task->cl, STARPU_R, 1);
  322. STARPU_ASSERT_MSG(STARPU_CODELET_GET_MODE(redux_task->cl, 0) == STARPU_RW, "First parameter of reduction codelet %p has to be RW", redux_task->cl);
  323. STARPU_ASSERT_MSG(STARPU_CODELET_GET_MODE(redux_task->cl, 1) == STARPU_R, "Second parameter of reduction codelet %p has to be R", redux_task->cl);
  324. STARPU_TASK_SET_HANDLE(redux_task, handle, 0);
  325. STARPU_TASK_SET_HANDLE(redux_task, replicate_array[replicate], 1);
  326. int ret = _starpu_task_submit_internally(redux_task);
  327. STARPU_ASSERT(!ret);
  328. }
  329. #endif
  330. /* Get the header lock back */
  331. _starpu_spin_lock(&handle->header_lock);
  332. }
  333. for (worker = 0; worker < nworkers; worker++)
  334. {
  335. struct _starpu_data_replicate *replicate;
  336. replicate = &handle->per_worker[worker];
  337. replicate->relaxed_coherency = 1;
  338. if (replicate->mc)
  339. replicate->mc->relaxed_coherency = 1;
  340. }
  341. }
  342. void _starpu_data_end_reduction_mode_terminate(starpu_data_handle_t handle)
  343. {
  344. unsigned nworkers = starpu_worker_get_count();
  345. // fprintf(stderr, "_starpu_data_end_reduction_mode_terminate\n");
  346. unsigned worker;
  347. _starpu_spin_checklocked(&handle->header_lock);
  348. for (worker = 0; worker < nworkers; worker++)
  349. {
  350. struct _starpu_data_replicate *replicate;
  351. replicate = &handle->per_worker[worker];
  352. replicate->initialized = 0;
  353. if (handle->reduction_tmp_handles[worker])
  354. {
  355. // fprintf(stderr, "unregister handle %p\n", handle);
  356. _starpu_spin_lock(&handle->reduction_tmp_handles[worker]->header_lock);
  357. handle->reduction_tmp_handles[worker]->lazy_unregister = 1;
  358. _starpu_spin_unlock(&handle->reduction_tmp_handles[worker]->header_lock);
  359. starpu_data_unregister_no_coherency(handle->reduction_tmp_handles[worker]);
  360. handle->per_worker[worker].refcnt--;
  361. /* TODO put in cache */
  362. }
  363. }
  364. free(handle->reduction_tmp_handles);
  365. handle->reduction_tmp_handles = NULL;
  366. }