data_concurrency.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. /* TODO: implement */
  106. if (handle->arbiter)
  107. _STARPU_DISP("data acquisition not completely safe with arbitered handles\n");
  108. if (mode == STARPU_RW)
  109. mode = STARPU_W;
  110. /* Take the lock protecting the header. We try to do some progression
  111. * in case this is called from a worker, otherwise we just wait for the
  112. * lock to be available. */
  113. if (request_from_codelet)
  114. {
  115. int cpt = 0;
  116. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  117. {
  118. cpt++;
  119. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  120. }
  121. if (cpt == STARPU_SPIN_MAXTRY)
  122. _starpu_spin_lock(&handle->header_lock);
  123. }
  124. else
  125. {
  126. _starpu_spin_lock(&handle->header_lock);
  127. }
  128. /* If we have a request that is not used for the reduction, and that a
  129. * reduction is pending, we put it at the end of normal list, and we
  130. * use the reduction_req_list instead */
  131. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  132. unsigned frozen = 0;
  133. /* If we are currently performing a reduction, we freeze any request
  134. * that is not explicitely a reduction task. */
  135. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  136. if (pending_reduction && !is_a_reduction_task)
  137. frozen = 1;
  138. /* If there is currently nobody accessing the piece of data, or it's
  139. * not another writter and if this is the same type of access as the
  140. * current one, we can proceed. */
  141. unsigned put_in_list = 1;
  142. enum starpu_data_access_mode previous_mode = handle->current_mode;
  143. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  144. {
  145. /* Detect whether this is the end of a reduction phase */
  146. /* We don't want to start multiple reductions of the
  147. * same handle at the same time ! */
  148. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  149. {
  150. _starpu_data_end_reduction_mode(handle);
  151. /* Since we need to perform a mode change, we freeze
  152. * the request if needed. */
  153. put_in_list = (handle->reduction_refcnt > 0);
  154. }
  155. else
  156. {
  157. put_in_list = 0;
  158. }
  159. }
  160. if (put_in_list)
  161. {
  162. /* there cannot be multiple writers or a new writer
  163. * while the data is in read mode */
  164. handle->busy_count++;
  165. /* enqueue the request */
  166. struct _starpu_data_requester *r = _starpu_data_requester_new();
  167. r->mode = mode;
  168. r->is_requested_by_codelet = request_from_codelet;
  169. r->j = j;
  170. r->buffer_index = buffer_index;
  171. r->ready_data_callback = callback;
  172. r->argcb = argcb;
  173. /* We put the requester in a specific list if this is a reduction task */
  174. struct _starpu_data_requester_list *req_list =
  175. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  176. _starpu_data_requester_list_push_back(req_list, r);
  177. /* failed */
  178. put_in_list = 1;
  179. }
  180. else
  181. {
  182. handle->refcnt++;
  183. handle->busy_count++;
  184. /* Do not write to handle->current_mode if it is already
  185. * R. This avoids a spurious warning from helgrind when
  186. * the following happens:
  187. * acquire(R) in thread A
  188. * acquire(R) in thread B
  189. * release_data_on_node() in thread A
  190. * helgrind would shout that the latter reads current_mode
  191. * unsafely.
  192. *
  193. * This actually basically explains helgrind that it is a
  194. * shared R acquisition.
  195. */
  196. if (mode != STARPU_R || handle->current_mode != mode)
  197. handle->current_mode = mode;
  198. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  199. _starpu_data_start_reduction_mode(handle);
  200. /* success */
  201. put_in_list = 0;
  202. }
  203. _starpu_spin_unlock(&handle->header_lock);
  204. return put_in_list;
  205. }
  206. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  207. void (*callback)(void *), void *argcb)
  208. {
  209. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  210. }
  211. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  212. {
  213. /* Note that we do not access j->task->handles, but j->ordered_buffers
  214. * which is a sorted copy of it. */
  215. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  216. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  217. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  218. }
  219. /* Acquire all data of the given job, one by one in handle pointer value order
  220. */
  221. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  222. {
  223. unsigned buf;
  224. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  225. for (buf = start_buffer_index; buf < nbuffers; buf++)
  226. {
  227. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  228. if (buf)
  229. {
  230. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  231. if (handle_m1 == handle)
  232. /* We have already requested this data, skip it. This
  233. * depends on ordering putting writes before reads, see
  234. * _starpu_compar_handles. */
  235. continue;
  236. }
  237. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  238. // WIP_COMMUTE Begin
  239. if(handle->arbiter)
  240. {
  241. /* We arrived on an arbitered data, we stop and proceed
  242. * with the arbiter second step. */
  243. _starpu_submit_job_enforce_arbitered_deps(j, buf, nbuffers);
  244. return 1;
  245. }
  246. // WIP_COMMUTE End
  247. if (attempt_to_submit_data_request_from_job(j, buf))
  248. {
  249. return 1;
  250. }
  251. }
  252. return 0;
  253. }
  254. /* Sort the data used by the given job by handle pointer value order, and
  255. * acquire them in that order */
  256. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  257. {
  258. struct starpu_codelet *cl = j->task->cl;
  259. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  260. return 0;
  261. /* Compute an ordered list of the different pieces of data so that we
  262. * grab then according to a total order, thus avoiding a deadlock
  263. * condition */
  264. unsigned i;
  265. for (i=0 ; i<STARPU_TASK_GET_NBUFFERS(j->task); i++)
  266. {
  267. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, i);
  268. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  269. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, i);
  270. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  271. int node = -1;
  272. if (j->task->cl->specific_nodes)
  273. node = STARPU_CODELET_GET_NODE(j->task->cl, i);
  274. _STARPU_JOB_SET_ORDERED_BUFFER_NODE(j, node, i);
  275. }
  276. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), STARPU_TASK_GET_NBUFFERS(j->task));
  277. return _submit_job_enforce_data_deps(j, 0);
  278. }
  279. /* This request got fulfilled, continue with the other requests of the
  280. * corresponding job */
  281. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  282. {
  283. struct _starpu_job *j = r->j;
  284. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  285. unsigned buffer_index = r->buffer_index;
  286. if (buffer_index + 1 < nbuffers)
  287. /* not all buffers are protected yet */
  288. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  289. else
  290. return 0;
  291. }
  292. /* This is called when a task is finished with a piece of data
  293. * (or on starpu_data_release)
  294. *
  295. * The header lock must already be taken by the caller.
  296. * This may free the handle if it was lazily unregistered (1 is returned in
  297. * that case). The handle pointer thus becomes invalid for the caller.
  298. */
  299. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  300. {
  301. _starpu_spin_checklocked(&handle->header_lock);
  302. /* A data access has finished so we remove a reference. */
  303. STARPU_ASSERT(handle->refcnt > 0);
  304. handle->refcnt--;
  305. STARPU_ASSERT(handle->busy_count > 0);
  306. handle->busy_count--;
  307. if (_starpu_data_check_not_busy(handle))
  308. /* Handle was destroyed, nothing left to do. */
  309. return 1;
  310. /* In case there is a pending reduction, and that this is the last
  311. * requester, we may go back to a "normal" coherency model. */
  312. if (handle->reduction_refcnt > 0)
  313. {
  314. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  315. handle->reduction_refcnt--;
  316. if (handle->reduction_refcnt == 0)
  317. _starpu_data_end_reduction_mode_terminate(handle);
  318. }
  319. struct _starpu_data_requester *r;
  320. while ((r = may_unlock_data_req_list_head(handle)))
  321. {
  322. /* STARPU_RW accesses are treated as STARPU_W */
  323. enum starpu_data_access_mode r_mode = r->mode;
  324. if (r_mode == STARPU_RW)
  325. r_mode = STARPU_W;
  326. int put_in_list = 1;
  327. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  328. {
  329. _starpu_data_end_reduction_mode(handle);
  330. /* Since we need to perform a mode change, we freeze
  331. * the request if needed. */
  332. put_in_list = (handle->reduction_refcnt > 0);
  333. }
  334. else
  335. {
  336. put_in_list = 0;
  337. }
  338. if (put_in_list)
  339. {
  340. /* We need to put the request back because we must
  341. * perform a reduction before. */
  342. _starpu_data_requester_list_push_front(handle->req_list, r);
  343. }
  344. else
  345. {
  346. /* The data is now attributed to that request so we put a
  347. * reference on it. */
  348. handle->refcnt++;
  349. handle->busy_count++;
  350. enum starpu_data_access_mode previous_mode = handle->current_mode;
  351. handle->current_mode = r_mode;
  352. /* In case we enter in a reduction mode, we invalidate all per
  353. * worker replicates. Note that the "per_node" replicates are
  354. * kept intact because we'll reduce a valid copy of the
  355. * "per-node replicate" with the per-worker replicates .*/
  356. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  357. _starpu_data_start_reduction_mode(handle);
  358. _starpu_spin_unlock(&handle->header_lock);
  359. if (r->is_requested_by_codelet)
  360. {
  361. if (!unlock_one_requester(r))
  362. _starpu_push_task(r->j);
  363. }
  364. else
  365. {
  366. STARPU_ASSERT(r->ready_data_callback);
  367. /* execute the callback associated with the data requester */
  368. r->ready_data_callback(r->argcb);
  369. }
  370. _starpu_data_requester_delete(r);
  371. _starpu_spin_lock(&handle->header_lock);
  372. STARPU_ASSERT(handle->busy_count > 0);
  373. handle->busy_count--;
  374. if (_starpu_data_check_not_busy(handle))
  375. return 1;
  376. }
  377. }
  378. // WIP_COMMUTE Begin
  379. if(handle->refcnt == 0 && handle->arbitered_req_list != NULL)
  380. {
  381. _starpu_spin_unlock(&handle->header_lock);
  382. /* _starpu_notify_arbitered_dependencies will handle its own locking */
  383. _starpu_notify_arbitered_dependencies(handle);
  384. /* We have already unlocked */
  385. return 1;
  386. }
  387. // WIP_COMMUTE End
  388. return 0;
  389. }