reduction.c 12 KB

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