data_concurrency.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 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 <core/dependencies/data_concurrency.h>
  18. #include <datawizard/coherency.h>
  19. #include <core/sched_policy.h>
  20. #include <common/starpu_spinlock.h>
  21. #include <datawizard/sort_data_handles.h>
  22. /*
  23. * The different types of data accesses are STARPU_R, STARPU_RW, STARPU_W,
  24. * STARPU_SCRATCH and STARPU_REDUX. STARPU_RW is managed as a STARPU_W access.
  25. * - A single STARPU_W access is allowed at a time.
  26. * - Concurrent STARPU_R accesses are allowed.
  27. * - Concurrent STARPU_SCRATCH accesses are allowed.
  28. * - Concurrent STARPU_REDUX accesses are allowed.
  29. */
  30. /* the header lock must be taken by the caller */
  31. static struct _starpu_data_requester *may_unlock_data_req_list_head(starpu_data_handle_t handle)
  32. {
  33. struct _starpu_data_requester_list *req_list;
  34. if (handle->reduction_refcnt > 0)
  35. {
  36. req_list = handle->reduction_req_list;
  37. }
  38. else
  39. {
  40. if (_starpu_data_requester_list_empty(handle->reduction_req_list))
  41. req_list = handle->req_list;
  42. else
  43. req_list = handle->reduction_req_list;
  44. }
  45. /* if there is no one to unlock ... */
  46. if (_starpu_data_requester_list_empty(req_list))
  47. return NULL;
  48. /* if there is no reference to the data anymore, we can use it */
  49. if (handle->refcnt == 0)
  50. return _starpu_data_requester_list_pop_front(req_list);
  51. /* Already writing to it, do not let another write access through */
  52. if (handle->current_mode == STARPU_W)
  53. return NULL;
  54. /* data->current_mode == STARPU_R, so we can process more readers */
  55. struct _starpu_data_requester *r = _starpu_data_requester_list_front(req_list);
  56. enum starpu_data_access_mode r_mode = r->mode;
  57. if (r_mode == STARPU_RW)
  58. r_mode = STARPU_W;
  59. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  60. * access, we only proceed if the current mode is the same as the
  61. * requested mode. */
  62. if (r_mode == handle->current_mode)
  63. return _starpu_data_requester_list_pop_front(req_list);
  64. else
  65. return NULL;
  66. }
  67. /* Try to submit a data request, in case the request can be processed
  68. * immediatly, return 0, if there is still a dependency that is not compatible
  69. * with the current mode, the request is put in the per-handle list of
  70. * "requesters", and this function returns 1. */
  71. static unsigned _starpu_attempt_to_submit_data_request(unsigned request_from_codelet,
  72. starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  73. void (*callback)(void *), void *argcb,
  74. struct _starpu_job *j, unsigned buffer_index)
  75. {
  76. if (mode == STARPU_RW)
  77. mode = STARPU_W;
  78. /* Take the lock protecting the header. We try to do some progression
  79. * in case this is called from a worker, otherwise we just wait for the
  80. * lock to be available. */
  81. if (request_from_codelet)
  82. {
  83. int cpt = 0;
  84. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  85. {
  86. cpt++;
  87. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  88. }
  89. if (cpt == STARPU_SPIN_MAXTRY)
  90. _starpu_spin_lock(&handle->header_lock);
  91. }
  92. else
  93. {
  94. _starpu_spin_lock(&handle->header_lock);
  95. }
  96. /* If we have a request that is not used for the reduction, and that a
  97. * reduction is pending, we put it at the end of normal list, and we
  98. * use the reduction_req_list instead */
  99. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  100. unsigned frozen = 0;
  101. /* If we are currently performing a reduction, we freeze any request
  102. * that is not explicitely a reduction task. */
  103. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  104. if (pending_reduction && !is_a_reduction_task)
  105. frozen = 1;
  106. /* If there is currently nobody accessing the piece of data, or it's
  107. * not another writter and if this is the same type of access as the
  108. * current one, we can proceed. */
  109. unsigned put_in_list = 1;
  110. enum starpu_data_access_mode previous_mode = handle->current_mode;
  111. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  112. {
  113. /* Detect whether this is the end of a reduction phase */
  114. /* We don't want to start multiple reductions of the
  115. * same handle at the same time ! */
  116. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  117. {
  118. _starpu_data_end_reduction_mode(handle);
  119. /* Since we need to perform a mode change, we freeze
  120. * the request if needed. */
  121. put_in_list = (handle->reduction_refcnt > 0);
  122. }
  123. else
  124. {
  125. put_in_list = 0;
  126. }
  127. }
  128. if (put_in_list)
  129. {
  130. /* there cannot be multiple writers or a new writer
  131. * while the data is in read mode */
  132. handle->busy_count++;
  133. /* enqueue the request */
  134. struct _starpu_data_requester *r = _starpu_data_requester_new();
  135. r->mode = mode;
  136. r->is_requested_by_codelet = request_from_codelet;
  137. r->j = j;
  138. r->buffer_index = buffer_index;
  139. r->ready_data_callback = callback;
  140. r->argcb = argcb;
  141. /* We put the requester in a specific list if this is a reduction task */
  142. struct _starpu_data_requester_list *req_list =
  143. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  144. _starpu_data_requester_list_push_back(req_list, r);
  145. /* failed */
  146. put_in_list = 1;
  147. }
  148. else
  149. {
  150. handle->refcnt++;
  151. handle->busy_count++;
  152. handle->current_mode = mode;
  153. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  154. _starpu_data_start_reduction_mode(handle);
  155. /* success */
  156. put_in_list = 0;
  157. }
  158. _starpu_spin_unlock(&handle->header_lock);
  159. return put_in_list;
  160. }
  161. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  162. void (*callback)(void *), void *argcb)
  163. {
  164. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  165. }
  166. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  167. {
  168. /* Note that we do not access j->task->handles, but j->ordered_buffers
  169. * which is a sorted copy of it. */
  170. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  171. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  172. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  173. }
  174. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  175. {
  176. unsigned buf;
  177. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  178. for (buf = start_buffer_index; buf < nbuffers; buf++)
  179. {
  180. if (buf)
  181. {
  182. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  183. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  184. if (handle_m1 == handle)
  185. /* We have already requested this data, skip it. This
  186. * depends on ordering putting writes before reads, see
  187. * _starpu_compar_handles. */
  188. continue;
  189. }
  190. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  191. if (attempt_to_submit_data_request_from_job(j, buf))
  192. {
  193. return 1;
  194. }
  195. }
  196. return 0;
  197. }
  198. /* When a new task is submitted, we make sure that there cannot be codelets
  199. with concurrent data-access at the same time in the scheduling engine (eg.
  200. there can be 2 tasks reading a piece of data, but there cannot be one
  201. reading and another writing) */
  202. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  203. {
  204. struct starpu_codelet *cl = j->task->cl;
  205. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  206. return 0;
  207. /* Compute an ordered list of the different pieces of data so that we
  208. * grab then according to a total order, thus avoiding a deadlock
  209. * condition */
  210. unsigned i;
  211. for (i=0 ; i<STARPU_TASK_GET_NBUFFERS(j->task); i++)
  212. {
  213. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, i);
  214. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  215. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, i);
  216. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  217. int node = -1;
  218. if (j->task->cl->specific_nodes)
  219. node = STARPU_CODELET_GET_NODE(j->task->cl, i);
  220. _STARPU_JOB_SET_ORDERED_BUFFER_NODE(j, node, i);
  221. }
  222. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), STARPU_TASK_GET_NBUFFERS(j->task));
  223. return _submit_job_enforce_data_deps(j, 0);
  224. }
  225. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  226. {
  227. struct _starpu_job *j = r->j;
  228. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  229. unsigned buffer_index = r->buffer_index;
  230. if (buffer_index + 1 < nbuffers)
  231. /* not all buffers are protected yet */
  232. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  233. else
  234. return 0;
  235. }
  236. /* The header lock must already be taken by the caller.
  237. * This may free the handle if it was lazily unregistered (1 is returned in
  238. * that case). The handle pointer thus becomes invalid for the caller.
  239. */
  240. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  241. {
  242. _starpu_spin_checklocked(&handle->header_lock);
  243. /* A data access has finished so we remove a reference. */
  244. STARPU_ASSERT(handle->refcnt > 0);
  245. handle->refcnt--;
  246. STARPU_ASSERT(handle->busy_count > 0);
  247. handle->busy_count--;
  248. if (_starpu_data_check_not_busy(handle))
  249. /* Handle was destroyed, nothing left to do. */
  250. return 1;
  251. /* In case there is a pending reduction, and that this is the last
  252. * requester, we may go back to a "normal" coherency model. */
  253. if (handle->reduction_refcnt > 0)
  254. {
  255. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  256. handle->reduction_refcnt--;
  257. if (handle->reduction_refcnt == 0)
  258. _starpu_data_end_reduction_mode_terminate(handle);
  259. }
  260. struct _starpu_data_requester *r;
  261. while ((r = may_unlock_data_req_list_head(handle)))
  262. {
  263. /* STARPU_RW accesses are treated as STARPU_W */
  264. enum starpu_data_access_mode r_mode = r->mode;
  265. if (r_mode == STARPU_RW)
  266. r_mode = STARPU_W;
  267. int put_in_list = 1;
  268. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  269. {
  270. _starpu_data_end_reduction_mode(handle);
  271. /* Since we need to perform a mode change, we freeze
  272. * the request if needed. */
  273. put_in_list = (handle->reduction_refcnt > 0);
  274. }
  275. else
  276. {
  277. put_in_list = 0;
  278. }
  279. if (put_in_list)
  280. {
  281. /* We need to put the request back because we must
  282. * perform a reduction before. */
  283. _starpu_data_requester_list_push_front(handle->req_list, r);
  284. }
  285. else
  286. {
  287. /* The data is now attributed to that request so we put a
  288. * reference on it. */
  289. handle->refcnt++;
  290. handle->busy_count++;
  291. enum starpu_data_access_mode previous_mode = handle->current_mode;
  292. handle->current_mode = r_mode;
  293. /* In case we enter in a reduction mode, we invalidate all per
  294. * worker replicates. Note that the "per_node" replicates are
  295. * kept intact because we'll reduce a valid copy of the
  296. * "per-node replicate" with the per-worker replicates .*/
  297. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  298. _starpu_data_start_reduction_mode(handle);
  299. _starpu_spin_unlock(&handle->header_lock);
  300. if (r->is_requested_by_codelet)
  301. {
  302. if (!unlock_one_requester(r))
  303. _starpu_push_task(r->j);
  304. }
  305. else
  306. {
  307. STARPU_ASSERT(r->ready_data_callback);
  308. /* execute the callback associated with the data requester */
  309. r->ready_data_callback(r->argcb);
  310. }
  311. _starpu_data_requester_delete(r);
  312. _starpu_spin_lock(&handle->header_lock);
  313. STARPU_ASSERT(handle->busy_count > 0);
  314. handle->busy_count--;
  315. if (_starpu_data_check_not_busy(handle))
  316. return 1;
  317. }
  318. }
  319. return 0;
  320. }