reduction.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2011, 2012 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 <core/task.h>
  20. #include <datawizard/datawizard.h>
  21. void starpu_data_set_reduction_methods(starpu_data_handle_t handle,
  22. struct starpu_codelet *redux_cl,
  23. struct starpu_codelet *init_cl)
  24. {
  25. _starpu_spin_lock(&handle->header_lock);
  26. _starpu_codelet_check_deprecated_fields(redux_cl);
  27. _starpu_codelet_check_deprecated_fields(init_cl);
  28. unsigned child;
  29. for (child = 0; child < handle->nchildren; child++)
  30. {
  31. /* make sure that the flags are applied to the children as well */
  32. struct _starpu_data_state *child_handle = &handle->children[child];
  33. if (child_handle->nchildren > 0)
  34. starpu_data_set_reduction_methods(child_handle, redux_cl, init_cl);
  35. }
  36. handle->redux_cl = redux_cl;
  37. handle->init_cl = init_cl;
  38. _starpu_spin_unlock(&handle->header_lock);
  39. }
  40. void _starpu_redux_init_data_replicate(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, int workerid)
  41. {
  42. STARPU_ASSERT(replicate);
  43. STARPU_ASSERT(replicate->allocated);
  44. struct starpu_codelet *init_cl = handle->init_cl;
  45. STARPU_ASSERT(init_cl);
  46. _starpu_cl_func_t init_func = NULL;
  47. /* TODO Check that worker may execute the codelet */
  48. switch (starpu_worker_get_type(workerid))
  49. {
  50. case STARPU_CPU_WORKER:
  51. init_func = _starpu_task_get_cpu_nth_implementation(init_cl, 0);
  52. break;
  53. case STARPU_CUDA_WORKER:
  54. init_func = _starpu_task_get_cuda_nth_implementation(init_cl, 0);
  55. break;
  56. case STARPU_OPENCL_WORKER:
  57. init_func = _starpu_task_get_opencl_nth_implementation(init_cl, 0);
  58. break;
  59. default:
  60. STARPU_ABORT();
  61. break;
  62. }
  63. STARPU_ASSERT(init_func);
  64. init_func(&replicate->data_interface, NULL);
  65. replicate->initialized = 1;
  66. }
  67. /* Enable reduction mode. This function must be called with the header lock
  68. * taken. */
  69. void _starpu_data_start_reduction_mode(starpu_data_handle_t handle)
  70. {
  71. STARPU_ASSERT(handle->reduction_refcnt == 0);
  72. unsigned worker;
  73. unsigned nworkers = starpu_worker_get_count();
  74. for (worker = 0; worker < nworkers; worker++)
  75. {
  76. struct _starpu_data_replicate *replicate;
  77. replicate = &handle->per_worker[worker];
  78. replicate->initialized = 0;
  79. replicate->relaxed_coherency = 2;
  80. if (replicate->mc)
  81. replicate->mc->relaxed_coherency = 2;
  82. }
  83. }
  84. //#define NO_TREE_REDUCTION
  85. /* Force reduction. The lock should already have been taken. */
  86. void _starpu_data_end_reduction_mode(starpu_data_handle_t handle)
  87. {
  88. unsigned worker;
  89. /* Put every valid replicate in the same array */
  90. unsigned replicate_count = 0;
  91. starpu_data_handle_t replicate_array[STARPU_NMAXWORKERS];
  92. /* Register all valid per-worker replicates */
  93. unsigned nworkers = starpu_worker_get_count();
  94. for (worker = 0; worker < nworkers; worker++)
  95. {
  96. if (handle->per_worker[worker].initialized)
  97. {
  98. /* Make sure the replicate is not removed */
  99. handle->per_worker[worker].refcnt++;
  100. uint32_t home_node = starpu_worker_get_memory_node(worker);
  101. starpu_data_register(&handle->reduction_tmp_handles[worker],
  102. home_node, handle->per_worker[worker].data_interface, handle->ops);
  103. starpu_data_set_sequential_consistency_flag(handle->reduction_tmp_handles[worker], 0);
  104. replicate_array[replicate_count++] = handle->reduction_tmp_handles[worker];
  105. }
  106. else
  107. {
  108. handle->reduction_tmp_handles[worker] = NULL;
  109. }
  110. }
  111. #ifndef NO_TREE_REDUCTION
  112. handle->reduction_refcnt = 1;
  113. #else
  114. /* We know that in this reduction algorithm there is exactly one task per valid replicate. */
  115. handle->reduction_refcnt = replicate_count;
  116. #endif
  117. // fprintf(stderr, "REDUX REFCNT = %d\n", handle->reduction_refcnt);
  118. if (replicate_count > 0)
  119. {
  120. /* Temporarily unlock the handle */
  121. _starpu_spin_unlock(&handle->header_lock);
  122. #ifndef NO_TREE_REDUCTION
  123. /* We will store a pointer to the last task which should modify the
  124. * replicate */
  125. struct starpu_task *last_replicate_deps[replicate_count];
  126. memset(last_replicate_deps, 0, replicate_count*sizeof(struct starpu_task *));
  127. unsigned step = 1;
  128. while (step <= replicate_count)
  129. {
  130. unsigned i;
  131. for (i = 0; i < replicate_count; i+=2*step)
  132. {
  133. if (i + step < replicate_count)
  134. {
  135. /* Perform the reduction between replicates i
  136. * and i+step and put the result in replicate i */
  137. struct starpu_task *redux_task = starpu_task_create();
  138. redux_task->cl = handle->redux_cl;
  139. STARPU_ASSERT(redux_task->cl);
  140. redux_task->handles[0] = replicate_array[i];
  141. redux_task->cl->modes[0] = STARPU_RW;
  142. redux_task->handles[1] = replicate_array[i+step];
  143. redux_task->cl->modes[1] = STARPU_R;
  144. redux_task->detach = 0;
  145. int ndeps = 0;
  146. struct starpu_task *task_deps[2];
  147. if (last_replicate_deps[i])
  148. task_deps[ndeps++] = last_replicate_deps[i];
  149. if (last_replicate_deps[i+step])
  150. task_deps[ndeps++] = last_replicate_deps[i+step];
  151. /* i depends on this task */
  152. last_replicate_deps[i] = redux_task;
  153. /* we don't perform the reduction until both replicates are ready */
  154. starpu_task_declare_deps_array(redux_task, ndeps, task_deps);
  155. int ret = starpu_task_submit(redux_task);
  156. STARPU_ASSERT(!ret);
  157. }
  158. }
  159. step *= 2;
  160. }
  161. struct starpu_task *redux_task = starpu_task_create();
  162. /* Mark these tasks so that StarPU does not block them
  163. * when they try to access the handle (normal tasks are
  164. * data requests to that handle are frozen until the
  165. * data is coherent again). */
  166. struct _starpu_job *j = _starpu_get_job_associated_to_task(redux_task);
  167. j->reduction_task = 1;
  168. redux_task->cl = handle->redux_cl;
  169. STARPU_ASSERT(redux_task->cl);
  170. redux_task->handles[0] = handle;
  171. redux_task->cl->modes[0] = STARPU_RW;
  172. redux_task->handles[1] = replicate_array[0];
  173. redux_task->cl->modes[1] = STARPU_R;
  174. if (last_replicate_deps[0])
  175. starpu_task_declare_deps_array(redux_task, 1, &last_replicate_deps[0]);
  176. int ret = starpu_task_submit(redux_task);
  177. STARPU_ASSERT(!ret);
  178. #else
  179. /* Create a set of tasks to perform the reduction */
  180. unsigned replicate;
  181. for (replicate = 0; replicate < replicate_count; replicate++)
  182. {
  183. struct starpu_task *redux_task = starpu_task_create();
  184. /* Mark these tasks so that StarPU does not block them
  185. * when they try to access the handle (normal tasks are
  186. * data requests to that handle are frozen until the
  187. * data is coherent again). */
  188. struct _starpu_job *j = _starpu_get_job_associated_to_task(redux_task);
  189. j->reduction_task = 1;
  190. redux_task->cl = handle->redux_cl;
  191. STARPU_ASSERT(redux_task->cl);
  192. #ifdef STARPU_DEVEL
  193. # warning the modes should already be set in the codelet. Only check they are valid?
  194. #endif
  195. redux_task->cl->modes[0] = STARPU_RW;
  196. redux_task->cl->modes[1] = STARPU_R;
  197. redux_task->handles[0] = handle;
  198. redux_task->handles[1] = replicate_array[replicate];
  199. int ret = starpu_task_submit(redux_task);
  200. STARPU_ASSERT(!ret);
  201. }
  202. #endif
  203. /* Get the header lock back */
  204. _starpu_spin_lock(&handle->header_lock);
  205. }
  206. for (worker = 0; worker < nworkers; worker++)
  207. {
  208. struct _starpu_data_replicate *replicate;
  209. replicate = &handle->per_worker[worker];
  210. replicate->relaxed_coherency = 1;
  211. if (replicate->mc)
  212. replicate->mc->relaxed_coherency = 1;
  213. }
  214. }
  215. void _starpu_data_end_reduction_mode_terminate(starpu_data_handle_t handle)
  216. {
  217. unsigned nworkers = starpu_worker_get_count();
  218. // fprintf(stderr, "_starpu_data_end_reduction_mode_terminate\n");
  219. unsigned worker;
  220. for (worker = 0; worker < nworkers; worker++)
  221. {
  222. struct _starpu_data_replicate *replicate;
  223. replicate = &handle->per_worker[worker];
  224. replicate->initialized = 0;
  225. if (handle->reduction_tmp_handles[worker])
  226. {
  227. // fprintf(stderr, "unregister handle %p\n", handle);
  228. handle->reduction_tmp_handles[worker]->lazy_unregister = 1;
  229. starpu_data_unregister_no_coherency(handle->reduction_tmp_handles[worker]);
  230. handle->per_worker[worker].refcnt--;
  231. /* TODO put in cache */
  232. }
  233. }
  234. }