reduction.c 13 KB

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