reduction.c 13 KB

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