reduction.c 13 KB

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