data_concurrency.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2015 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <core/dependencies/data_concurrency.h>
  19. #include <datawizard/coherency.h>
  20. #include <core/sched_policy.h>
  21. #include <common/starpu_spinlock.h>
  22. #include <datawizard/sort_data_handles.h>
  23. /*
  24. * We have a kind of dining philosophers problem: various tasks are accessing
  25. * various data concurrently in different modes: STARPU_R, STARPU_RW, STARPU_W,
  26. * STARPU_SCRATCH and STARPU_REDUX. STARPU_RW is managed as a STARPU_W access.
  27. * We have the following constraints:
  28. *
  29. * - A single STARPU_W access is allowed at a time.
  30. * - Concurrent STARPU_R accesses are allowed.
  31. * - Concurrent STARPU_SCRATCH accesses are allowed.
  32. * - Concurrent STARPU_REDUX accesses are allowed.
  33. *
  34. * What we do here is implementing the Dijkstra solutions: handles are sorted
  35. * by pointer value order, and tasks call
  36. * _starpu_attempt_to_submit_data_request for each requested data in that order
  37. * (see _starpu_sort_task_handles call in _starpu_submit_job_enforce_data_deps).
  38. *
  39. * _starpu_attempt_to_submit_data_request will either:
  40. * - obtain access to the data, and thus the task can proceed with acquiring
  41. * other data (see _submit_job_enforce_data_deps)
  42. * - queue a request on the data handle
  43. *
  44. * When a task finishes, it calls _starpu_notify_data_dependencies for each
  45. * data, to free its acquisitions. This will look whether the first queued
  46. * request can be fulfilled, and in such case make the task try to acquire its
  47. * next data.
  48. *
  49. * The same mechanism is used for application data aquisition
  50. * (starpu_data_acquire).
  51. *
  52. * For data with an arbiter, we have a second step, performed after this first
  53. * step, implemented in data_commute_concurrency.c
  54. */
  55. /*
  56. * Check to see whether the first queued request can proceed, and return it in
  57. * such case.
  58. */
  59. /* the header lock must be taken by the caller */
  60. static struct _starpu_data_requester *may_unlock_data_req_list_head(starpu_data_handle_t handle)
  61. {
  62. struct _starpu_data_requester_list *req_list;
  63. if (handle->reduction_refcnt > 0)
  64. {
  65. req_list = handle->reduction_req_list;
  66. }
  67. else
  68. {
  69. if (_starpu_data_requester_list_empty(handle->reduction_req_list))
  70. req_list = handle->req_list;
  71. else
  72. req_list = handle->reduction_req_list;
  73. }
  74. /* if there is no one to unlock ... */
  75. if (_starpu_data_requester_list_empty(req_list))
  76. return NULL;
  77. /* if there is no reference to the data anymore, we can use it */
  78. if (handle->refcnt == 0)
  79. return _starpu_data_requester_list_pop_front(req_list);
  80. /* Already writing to it, do not let another write access through */
  81. if (handle->current_mode == STARPU_W)
  82. return NULL;
  83. /* data->current_mode == STARPU_R, so we can process more readers */
  84. struct _starpu_data_requester *r = _starpu_data_requester_list_front(req_list);
  85. enum starpu_data_access_mode r_mode = r->mode;
  86. if (r_mode == STARPU_RW)
  87. r_mode = STARPU_W;
  88. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  89. * access, we only proceed if the current mode is the same as the
  90. * requested mode. */
  91. if (r_mode == handle->current_mode)
  92. return _starpu_data_requester_list_pop_front(req_list);
  93. else
  94. return NULL;
  95. }
  96. /* Try to submit a data request, in case the request can be processed
  97. * immediatly, return 0, if there is still a dependency that is not compatible
  98. * with the current mode, the request is put in the per-handle list of
  99. * "requesters", and this function returns 1. */
  100. static unsigned _starpu_attempt_to_submit_data_request(unsigned request_from_codelet,
  101. starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  102. void (*callback)(void *), void *argcb,
  103. struct _starpu_job *j, unsigned buffer_index)
  104. {
  105. if (mode == STARPU_RW)
  106. mode = STARPU_W;
  107. /* Take the lock protecting the header. We try to do some progression
  108. * in case this is called from a worker, otherwise we just wait for the
  109. * lock to be available. */
  110. if (request_from_codelet)
  111. {
  112. int cpt = 0;
  113. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  114. {
  115. cpt++;
  116. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  117. }
  118. if (cpt == STARPU_SPIN_MAXTRY)
  119. _starpu_spin_lock(&handle->header_lock);
  120. }
  121. else
  122. {
  123. _starpu_spin_lock(&handle->header_lock);
  124. }
  125. /* If we have a request that is not used for the reduction, and that a
  126. * reduction is pending, we put it at the end of normal list, and we
  127. * use the reduction_req_list instead */
  128. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  129. unsigned frozen = 0;
  130. /* If we are currently performing a reduction, we freeze any request
  131. * that is not explicitely a reduction task. */
  132. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  133. if (pending_reduction && !is_a_reduction_task)
  134. frozen = 1;
  135. /* If there is currently nobody accessing the piece of data, or it's
  136. * not another writter and if this is the same type of access as the
  137. * current one, we can proceed. */
  138. unsigned put_in_list = 1;
  139. enum starpu_data_access_mode previous_mode = handle->current_mode;
  140. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  141. {
  142. /* Detect whether this is the end of a reduction phase */
  143. /* We don't want to start multiple reductions of the
  144. * same handle at the same time ! */
  145. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  146. {
  147. _starpu_data_end_reduction_mode(handle);
  148. /* Since we need to perform a mode change, we freeze
  149. * the request if needed. */
  150. put_in_list = (handle->reduction_refcnt > 0);
  151. }
  152. else
  153. {
  154. put_in_list = 0;
  155. }
  156. }
  157. if (put_in_list)
  158. {
  159. /* there cannot be multiple writers or a new writer
  160. * while the data is in read mode */
  161. handle->busy_count++;
  162. /* enqueue the request */
  163. struct _starpu_data_requester *r = _starpu_data_requester_new();
  164. r->mode = mode;
  165. r->is_requested_by_codelet = request_from_codelet;
  166. r->j = j;
  167. r->buffer_index = buffer_index;
  168. r->ready_data_callback = callback;
  169. r->argcb = argcb;
  170. /* We put the requester in a specific list if this is a reduction task */
  171. struct _starpu_data_requester_list *req_list =
  172. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  173. _starpu_data_requester_list_push_back(req_list, r);
  174. /* failed */
  175. put_in_list = 1;
  176. }
  177. else
  178. {
  179. handle->refcnt++;
  180. handle->busy_count++;
  181. /* Do not write to handle->current_mode if it is already
  182. * R. This avoids a spurious warning from helgrind when
  183. * the following happens:
  184. * acquire(R) in thread A
  185. * acquire(R) in thread B
  186. * release_data_on_node() in thread A
  187. * helgrind would shout that the latter reads current_mode
  188. * unsafely.
  189. *
  190. * This actually basically explains helgrind that it is a
  191. * shared R acquisition.
  192. */
  193. if (mode != STARPU_R || handle->current_mode != mode)
  194. handle->current_mode = mode;
  195. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  196. _starpu_data_start_reduction_mode(handle);
  197. /* success */
  198. put_in_list = 0;
  199. }
  200. _starpu_spin_unlock(&handle->header_lock);
  201. return put_in_list;
  202. }
  203. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  204. void (*callback)(void *), void *argcb)
  205. {
  206. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  207. }
  208. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  209. {
  210. /* Note that we do not access j->task->handles, but j->ordered_buffers
  211. * which is a sorted copy of it. */
  212. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  213. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  214. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  215. }
  216. /* Acquire all data of the given job, one by one in handle pointer value order
  217. */
  218. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  219. {
  220. unsigned buf;
  221. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  222. for (buf = start_buffer_index; buf < nbuffers; buf++)
  223. {
  224. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  225. if (buf)
  226. {
  227. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  228. if (handle_m1 == handle)
  229. /* We have already requested this data, skip it. This
  230. * depends on ordering putting writes before reads, see
  231. * _starpu_compar_handles. */
  232. continue;
  233. }
  234. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  235. // WIP_COMMUTE Begin
  236. if(handle->arbiter)
  237. {
  238. /* We arrived on an arbitered data, we stop and proceed
  239. * with the arbiter second step. */
  240. _starpu_submit_job_enforce_arbitered_deps(j, buf, nbuffers);
  241. return 1;
  242. }
  243. // WIP_COMMUTE End
  244. if (attempt_to_submit_data_request_from_job(j, buf))
  245. {
  246. return 1;
  247. }
  248. }
  249. return 0;
  250. }
  251. /* Sort the data used by the given job by handle pointer value order, and
  252. * acquire them in that order */
  253. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  254. {
  255. struct starpu_codelet *cl = j->task->cl;
  256. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  257. return 0;
  258. /* Compute an ordered list of the different pieces of data so that we
  259. * grab then according to a total order, thus avoiding a deadlock
  260. * condition */
  261. unsigned i;
  262. for (i=0 ; i<STARPU_TASK_GET_NBUFFERS(j->task); i++)
  263. {
  264. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, i);
  265. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  266. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, i);
  267. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  268. int node = -1;
  269. if (j->task->cl->specific_nodes)
  270. node = STARPU_CODELET_GET_NODE(j->task->cl, i);
  271. _STARPU_JOB_SET_ORDERED_BUFFER_NODE(j, node, i);
  272. }
  273. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), STARPU_TASK_GET_NBUFFERS(j->task));
  274. return _submit_job_enforce_data_deps(j, 0);
  275. }
  276. /* This request got fulfilled, continue with the other requests of the
  277. * corresponding job */
  278. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  279. {
  280. struct _starpu_job *j = r->j;
  281. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  282. unsigned buffer_index = r->buffer_index;
  283. if (buffer_index + 1 < nbuffers)
  284. /* not all buffers are protected yet */
  285. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  286. else
  287. return 0;
  288. }
  289. /* This is called when a task is finished with a piece of data
  290. * (or on starpu_data_release)
  291. *
  292. * The header lock must already be taken by the caller.
  293. * This may free the handle if it was lazily unregistered (1 is returned in
  294. * that case). The handle pointer thus becomes invalid for the caller.
  295. */
  296. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  297. {
  298. _starpu_spin_checklocked(&handle->header_lock);
  299. /* A data access has finished so we remove a reference. */
  300. STARPU_ASSERT(handle->refcnt > 0);
  301. handle->refcnt--;
  302. STARPU_ASSERT(handle->busy_count > 0);
  303. handle->busy_count--;
  304. if (_starpu_data_check_not_busy(handle))
  305. /* Handle was destroyed, nothing left to do. */
  306. return 1;
  307. /* In case there is a pending reduction, and that this is the last
  308. * requester, we may go back to a "normal" coherency model. */
  309. if (handle->reduction_refcnt > 0)
  310. {
  311. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  312. handle->reduction_refcnt--;
  313. if (handle->reduction_refcnt == 0)
  314. _starpu_data_end_reduction_mode_terminate(handle);
  315. }
  316. struct _starpu_data_requester *r;
  317. while ((r = may_unlock_data_req_list_head(handle)))
  318. {
  319. /* STARPU_RW accesses are treated as STARPU_W */
  320. enum starpu_data_access_mode r_mode = r->mode;
  321. if (r_mode == STARPU_RW)
  322. r_mode = STARPU_W;
  323. int put_in_list = 1;
  324. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  325. {
  326. _starpu_data_end_reduction_mode(handle);
  327. /* Since we need to perform a mode change, we freeze
  328. * the request if needed. */
  329. put_in_list = (handle->reduction_refcnt > 0);
  330. }
  331. else
  332. {
  333. put_in_list = 0;
  334. }
  335. if (put_in_list)
  336. {
  337. /* We need to put the request back because we must
  338. * perform a reduction before. */
  339. _starpu_data_requester_list_push_front(handle->req_list, r);
  340. }
  341. else
  342. {
  343. /* The data is now attributed to that request so we put a
  344. * reference on it. */
  345. handle->refcnt++;
  346. handle->busy_count++;
  347. enum starpu_data_access_mode previous_mode = handle->current_mode;
  348. handle->current_mode = r_mode;
  349. /* In case we enter in a reduction mode, we invalidate all per
  350. * worker replicates. Note that the "per_node" replicates are
  351. * kept intact because we'll reduce a valid copy of the
  352. * "per-node replicate" with the per-worker replicates .*/
  353. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  354. _starpu_data_start_reduction_mode(handle);
  355. _starpu_spin_unlock(&handle->header_lock);
  356. if (r->is_requested_by_codelet)
  357. {
  358. if (!unlock_one_requester(r))
  359. _starpu_push_task(r->j);
  360. }
  361. else
  362. {
  363. STARPU_ASSERT(r->ready_data_callback);
  364. /* execute the callback associated with the data requester */
  365. r->ready_data_callback(r->argcb);
  366. }
  367. _starpu_data_requester_delete(r);
  368. _starpu_spin_lock(&handle->header_lock);
  369. STARPU_ASSERT(handle->busy_count > 0);
  370. handle->busy_count--;
  371. if (_starpu_data_check_not_busy(handle))
  372. return 1;
  373. }
  374. }
  375. // WIP_COMMUTE Begin
  376. if(handle->refcnt == 0 && handle->arbitered_req_list != NULL)
  377. {
  378. _starpu_spin_unlock(&handle->header_lock);
  379. /* _starpu_notify_arbitered_dependencies will handle its own locking */
  380. _starpu_notify_arbitered_dependencies(handle);
  381. /* We have already unlocked */
  382. return 1;
  383. }
  384. // WIP_COMMUTE End
  385. return 0;
  386. }