reduction.c 12 KB

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