data_concurrency.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  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 cuurrent 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. while (_starpu_spin_trylock(&handle->header_lock))
  84. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  85. }
  86. else
  87. {
  88. _starpu_spin_lock(&handle->header_lock);
  89. }
  90. /* If we have a request that is not used for the reduction, and that a
  91. * reduction is pending, we put it at the end of normal list, and we
  92. * use the reduction_req_list instead */
  93. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  94. unsigned frozen = 0;
  95. /* If we are currently performing a reduction, we freeze any request
  96. * that is not explicitely a reduction task. */
  97. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  98. if (pending_reduction && !is_a_reduction_task)
  99. frozen = 1;
  100. /* If there is currently nobody accessing the piece of data, or it's
  101. * not another writter and if this is the same type of access as the
  102. * current one, we can proceed. */
  103. unsigned put_in_list = 1;
  104. enum starpu_data_access_mode previous_mode = handle->current_mode;
  105. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  106. {
  107. /* Detect whether this is the end of a reduction phase */
  108. /* We don't want to start multiple reductions of the
  109. * same handle at the same time ! */
  110. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  111. {
  112. _starpu_data_end_reduction_mode(handle);
  113. /* Since we need to perform a mode change, we freeze
  114. * the request if needed. */
  115. put_in_list = (handle->reduction_refcnt > 0);
  116. }
  117. else
  118. {
  119. put_in_list = 0;
  120. }
  121. }
  122. if (put_in_list)
  123. {
  124. /* there cannot be multiple writers or a new writer
  125. * while the data is in read mode */
  126. handle->busy_count++;
  127. /* enqueue the request */
  128. struct _starpu_data_requester *r = _starpu_data_requester_new();
  129. r->mode = mode;
  130. r->is_requested_by_codelet = request_from_codelet;
  131. r->j = j;
  132. r->buffer_index = buffer_index;
  133. r->ready_data_callback = callback;
  134. r->argcb = argcb;
  135. /* We put the requester in a specific list if this is a reduction task */
  136. struct _starpu_data_requester_list *req_list =
  137. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  138. _starpu_data_requester_list_push_back(req_list, r);
  139. /* failed */
  140. put_in_list = 1;
  141. }
  142. else
  143. {
  144. handle->refcnt++;
  145. handle->busy_count++;
  146. handle->current_mode = mode;
  147. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  148. _starpu_data_start_reduction_mode(handle);
  149. /* success */
  150. put_in_list = 0;
  151. }
  152. _starpu_spin_unlock(&handle->header_lock);
  153. return put_in_list;
  154. }
  155. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  156. void (*callback)(void *), void *argcb)
  157. {
  158. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  159. }
  160. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  161. {
  162. /* Note that we do not access j->task->handles, but j->ordered_buffers
  163. * which is a sorted copy of it. */
  164. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  165. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  166. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  167. }
  168. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  169. {
  170. unsigned buf;
  171. unsigned nbuffers = j->task->cl->nbuffers;
  172. for (buf = start_buffer_index; buf < nbuffers; buf++)
  173. {
  174. if (buf)
  175. {
  176. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  177. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  178. if (handle_m1 == handle)
  179. /* We have already requested this data, skip it. This
  180. * depends on ordering putting writes before reads, see
  181. * _starpu_compar_handles. */
  182. continue;
  183. }
  184. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  185. if (attempt_to_submit_data_request_from_job(j, buf))
  186. {
  187. return 1;
  188. }
  189. }
  190. return 0;
  191. }
  192. /* When a new task is submitted, we make sure that there cannot be codelets
  193. with concurrent data-access at the same time in the scheduling engine (eg.
  194. there can be 2 tasks reading a piece of data, but there cannot be one
  195. reading and another writing) */
  196. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  197. {
  198. struct starpu_codelet *cl = j->task->cl;
  199. if ((cl == NULL) || (cl->nbuffers == 0))
  200. return 0;
  201. /* Compute an ordered list of the different pieces of data so that we
  202. * grab then according to a total order, thus avoiding a deadlock
  203. * condition */
  204. unsigned i;
  205. for (i=0 ; i<cl->nbuffers ; i++)
  206. {
  207. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, i);
  208. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  209. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(j->task->cl, i);
  210. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  211. }
  212. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), cl->nbuffers);
  213. return _submit_job_enforce_data_deps(j, 0);
  214. }
  215. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  216. {
  217. struct _starpu_job *j = r->j;
  218. unsigned nbuffers = j->task->cl->nbuffers;
  219. unsigned buffer_index = r->buffer_index;
  220. if (buffer_index + 1 < nbuffers)
  221. /* not all buffers are protected yet */
  222. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  223. else
  224. return 0;
  225. }
  226. /* The header lock must already be taken by the caller.
  227. * This may free the handle if it was lazily unregistered (1 is returned in
  228. * that case). The handle pointer thus becomes invalid for the caller.
  229. */
  230. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  231. {
  232. _starpu_spin_checklocked(&handle->header_lock);
  233. /* A data access has finished so we remove a reference. */
  234. STARPU_ASSERT(handle->refcnt > 0);
  235. handle->refcnt--;
  236. STARPU_ASSERT(handle->busy_count > 0);
  237. handle->busy_count--;
  238. if (_starpu_data_check_not_busy(handle))
  239. /* Handle was destroyed, nothing left to do. */
  240. return 1;
  241. /* In case there is a pending reduction, and that this is the last
  242. * requester, we may go back to a "normal" coherency model. */
  243. if (handle->reduction_refcnt > 0)
  244. {
  245. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  246. handle->reduction_refcnt--;
  247. if (handle->reduction_refcnt == 0)
  248. _starpu_data_end_reduction_mode_terminate(handle);
  249. }
  250. struct _starpu_data_requester *r;
  251. while ((r = may_unlock_data_req_list_head(handle)))
  252. {
  253. /* STARPU_RW accesses are treated as STARPU_W */
  254. enum starpu_data_access_mode r_mode = r->mode;
  255. if (r_mode == STARPU_RW)
  256. r_mode = STARPU_W;
  257. int put_in_list = 1;
  258. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  259. {
  260. _starpu_data_end_reduction_mode(handle);
  261. /* Since we need to perform a mode change, we freeze
  262. * the request if needed. */
  263. put_in_list = (handle->reduction_refcnt > 0);
  264. }
  265. else
  266. {
  267. put_in_list = 0;
  268. }
  269. if (put_in_list)
  270. {
  271. /* We need to put the request back because we must
  272. * perform a reduction before. */
  273. _starpu_data_requester_list_push_front(handle->req_list, r);
  274. }
  275. else
  276. {
  277. /* The data is now attributed to that request so we put a
  278. * reference on it. */
  279. handle->refcnt++;
  280. handle->busy_count++;
  281. enum starpu_data_access_mode previous_mode = handle->current_mode;
  282. handle->current_mode = r_mode;
  283. /* In case we enter in a reduction mode, we invalidate all per
  284. * worker replicates. Note that the "per_node" replicates are
  285. * kept intact because we'll reduce a valid copy of the
  286. * "per-node replicate" with the per-worker replicates .*/
  287. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  288. _starpu_data_start_reduction_mode(handle);
  289. _starpu_spin_unlock(&handle->header_lock);
  290. if (r->is_requested_by_codelet)
  291. {
  292. if (!unlock_one_requester(r))
  293. _starpu_push_task(r->j);
  294. }
  295. else
  296. {
  297. STARPU_ASSERT(r->ready_data_callback);
  298. /* execute the callback associated with the data requester */
  299. r->ready_data_callback(r->argcb);
  300. }
  301. _starpu_data_requester_delete(r);
  302. _starpu_spin_lock(&handle->header_lock);
  303. STARPU_ASSERT(handle->busy_count > 0);
  304. handle->busy_count--;
  305. if (_starpu_data_check_not_busy(handle))
  306. return 1;
  307. }
  308. }
  309. return 0;
  310. }