data_concurrency.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /*
  53. * Check to see whether the first queued request can proceed, and return it in
  54. * such case.
  55. */
  56. /* the header lock must be taken by the caller */
  57. static struct _starpu_data_requester *may_unlock_data_req_list_head(starpu_data_handle_t handle)
  58. {
  59. struct _starpu_data_requester_list *req_list;
  60. if (handle->reduction_refcnt > 0)
  61. {
  62. req_list = handle->reduction_req_list;
  63. }
  64. else
  65. {
  66. if (_starpu_data_requester_list_empty(handle->reduction_req_list))
  67. req_list = handle->req_list;
  68. else
  69. req_list = handle->reduction_req_list;
  70. }
  71. /* if there is no one to unlock ... */
  72. if (_starpu_data_requester_list_empty(req_list))
  73. return NULL;
  74. /* if there is no reference to the data anymore, we can use it */
  75. if (handle->refcnt == 0)
  76. return _starpu_data_requester_list_pop_front(req_list);
  77. /* Already writing to it, do not let another write access through */
  78. if (handle->current_mode & STARPU_W)
  79. return NULL;
  80. /* data->current_mode == STARPU_R, so we can process more readers */
  81. struct _starpu_data_requester *r = _starpu_data_requester_list_front(req_list);
  82. enum starpu_data_access_mode r_mode = r->mode;
  83. if ((r_mode & STARPU_RW) == STARPU_RW)
  84. r_mode &= ~STARPU_R;
  85. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  86. * access, we only proceed if the current mode is the same as the
  87. * requested mode. */
  88. if (r_mode == handle->current_mode)
  89. return _starpu_data_requester_list_pop_front(req_list);
  90. else
  91. return NULL;
  92. }
  93. /* Try to submit a data request, in case the request can be processed
  94. * immediatly, return 0, if there is still a dependency that is not compatible
  95. * with the current mode, the request is put in the per-handle list of
  96. * "requesters", and this function returns 1. */
  97. static unsigned _starpu_attempt_to_submit_data_request(unsigned request_from_codelet,
  98. starpu_data_handle_t handle, enum starpu_data_access_mode current_mode,
  99. void (*callback)(void *), void *argcb,
  100. struct _starpu_job *j, unsigned buffer_index)
  101. {
  102. // WIP_COMMUTE Begin
  103. enum starpu_data_access_mode mode = (current_mode & ~STARPU_COMMUTE);
  104. // WIP_COMMUTE End
  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. // WIP_COMMUTE Was
  140. //enum starpu_data_access_mode previous_mode = handle->current_mode;
  141. // WIP_COMMUTE Begin
  142. enum starpu_data_access_mode previous_mode = (handle->current_mode & ~STARPU_COMMUTE);
  143. // WIP_COMMUTE End
  144. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  145. {
  146. /* Detect whether this is the end of a reduction phase */
  147. /* We don't want to start multiple reductions of the
  148. * same handle at the same time ! */
  149. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  150. {
  151. _starpu_data_end_reduction_mode(handle);
  152. /* Since we need to perform a mode change, we freeze
  153. * the request if needed. */
  154. put_in_list = (handle->reduction_refcnt > 0);
  155. }
  156. else
  157. {
  158. put_in_list = 0;
  159. }
  160. }
  161. if (put_in_list)
  162. {
  163. /* there cannot be multiple writers or a new writer
  164. * while the data is in read mode */
  165. handle->busy_count++;
  166. /* enqueue the request */
  167. struct _starpu_data_requester *r = _starpu_data_requester_new();
  168. r->mode = current_mode;
  169. r->is_requested_by_codelet = request_from_codelet;
  170. r->j = j;
  171. r->buffer_index = buffer_index;
  172. r->ready_data_callback = callback;
  173. r->argcb = argcb;
  174. /* We put the requester in a specific list if this is a reduction task */
  175. struct _starpu_data_requester_list *req_list =
  176. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  177. _starpu_data_requester_list_push_back(req_list, r);
  178. /* failed */
  179. put_in_list = 1;
  180. }
  181. else
  182. {
  183. handle->refcnt++;
  184. handle->busy_count++;
  185. /* Do not write to handle->current_mode if it is already
  186. * R. This avoids a spurious warning from helgrind when
  187. * the following happens:
  188. * acquire(R) in thread A
  189. * acquire(R) in thread B
  190. * release_data_on_node() in thread A
  191. * helgrind would shout that the latter reads current_mode
  192. * unsafely.
  193. *
  194. * This actually basically explains helgrind that it is a
  195. * shared R acquisition.
  196. */
  197. if (mode != STARPU_R || handle->current_mode != mode)
  198. handle->current_mode = current_mode;
  199. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  200. _starpu_data_start_reduction_mode(handle);
  201. /* success */
  202. put_in_list = 0;
  203. }
  204. _starpu_spin_unlock(&handle->header_lock);
  205. return put_in_list;
  206. }
  207. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  208. void (*callback)(void *), void *argcb)
  209. {
  210. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  211. }
  212. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  213. {
  214. /* Note that we do not access j->task->handles, but j->ordered_buffers
  215. * which is a sorted copy of it. */
  216. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  217. // WIP_COMMUTE Was
  218. // enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  219. // WIP_COMMUTE Begin
  220. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index);
  221. // WIP_COMMUTE End
  222. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  223. }
  224. /* Acquire all data of the given job, one by one in handle pointer value order
  225. */
  226. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  227. {
  228. unsigned buf;
  229. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  230. for (buf = start_buffer_index; buf < nbuffers; buf++)
  231. {
  232. if (buf)
  233. {
  234. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  235. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  236. if (handle_m1 == handle)
  237. /* We have already requested this data, skip it. This
  238. * depends on ordering putting writes before reads, see
  239. * _starpu_compar_handles. */
  240. continue;
  241. }
  242. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  243. // WIP_COMMUTE Begin
  244. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buf);
  245. if(mode & STARPU_COMMUTE)
  246. {
  247. /* We arrived on the commute we stop and do not proceed as usual */
  248. break;
  249. }
  250. // WIP_COMMUTE End
  251. if (attempt_to_submit_data_request_from_job(j, buf))
  252. {
  253. return 1;
  254. }
  255. }
  256. // WIP_COMMUTE Begin
  257. /* We arrive on the commutes */
  258. if(buf != nbuffers)
  259. {
  260. #ifndef NO_LOCK_OR_DELEGATE
  261. struct starpu_enforce_commute_args* args = (struct starpu_enforce_commute_args*)malloc(sizeof(struct starpu_enforce_commute_args));
  262. args->j = j;
  263. args->buf = buf;
  264. args->nbuffers = nbuffers;
  265. /* The function will delete args */
  266. _starpu_LockOrDelegatePostOrPerform(&_starpu_submit_job_enforce_commute_deps, args);
  267. #else // NO_LOCK_OR_DELEGATE
  268. _starpu_submit_job_enforce_commute_deps(j, buf, nbuffers);
  269. #endif
  270. return 1;
  271. }
  272. // WIP_COMMUTE End
  273. return 0;
  274. }
  275. /* Sort the data used by the given job by handle pointer value order, and
  276. * acquire them in that order */
  277. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  278. {
  279. struct starpu_codelet *cl = j->task->cl;
  280. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  281. return 0;
  282. /* Compute an ordered list of the different pieces of data so that we
  283. * grab then according to a total order, thus avoiding a deadlock
  284. * condition */
  285. unsigned i;
  286. for (i=0 ; i<STARPU_TASK_GET_NBUFFERS(j->task); i++)
  287. {
  288. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, i);
  289. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  290. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, i);
  291. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  292. int node = -1;
  293. if (j->task->cl->specific_nodes)
  294. node = STARPU_CODELET_GET_NODE(j->task->cl, i);
  295. _STARPU_JOB_SET_ORDERED_BUFFER_NODE(j, node, i);
  296. }
  297. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), STARPU_TASK_GET_NBUFFERS(j->task));
  298. return _submit_job_enforce_data_deps(j, 0);
  299. }
  300. /* This request got fulfilled, continue with the other requests of the
  301. * corresponding job */
  302. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  303. {
  304. struct _starpu_job *j = r->j;
  305. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  306. unsigned buffer_index = r->buffer_index;
  307. if (buffer_index + 1 < nbuffers)
  308. /* not all buffers are protected yet */
  309. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  310. else
  311. return 0;
  312. }
  313. /* This is called when a task is finished with a piece of data
  314. * (or on starpu_data_release)
  315. *
  316. * The header lock must already be taken by the caller.
  317. * This may free the handle if it was lazily unregistered (1 is returned in
  318. * that case). The handle pointer thus becomes invalid for the caller.
  319. */
  320. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  321. {
  322. _starpu_spin_checklocked(&handle->header_lock);
  323. /* A data access has finished so we remove a reference. */
  324. STARPU_ASSERT(handle->refcnt > 0);
  325. handle->refcnt--;
  326. STARPU_ASSERT(handle->busy_count > 0);
  327. handle->busy_count--;
  328. if (_starpu_data_check_not_busy(handle))
  329. /* Handle was destroyed, nothing left to do. */
  330. return 1;
  331. /* In case there is a pending reduction, and that this is the last
  332. * requester, we may go back to a "normal" coherency model. */
  333. if (handle->reduction_refcnt > 0)
  334. {
  335. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  336. handle->reduction_refcnt--;
  337. if (handle->reduction_refcnt == 0)
  338. _starpu_data_end_reduction_mode_terminate(handle);
  339. }
  340. struct _starpu_data_requester *r;
  341. while ((r = may_unlock_data_req_list_head(handle)))
  342. {
  343. /* STARPU_RW accesses are treated as STARPU_W */
  344. enum starpu_data_access_mode r_mode = r->mode;
  345. if (r_mode == STARPU_RW)
  346. r_mode = STARPU_W;
  347. int put_in_list = 1;
  348. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  349. {
  350. _starpu_data_end_reduction_mode(handle);
  351. /* Since we need to perform a mode change, we freeze
  352. * the request if needed. */
  353. put_in_list = (handle->reduction_refcnt > 0);
  354. }
  355. else
  356. {
  357. put_in_list = 0;
  358. }
  359. if (put_in_list)
  360. {
  361. /* We need to put the request back because we must
  362. * perform a reduction before. */
  363. _starpu_data_requester_list_push_front(handle->req_list, r);
  364. }
  365. else
  366. {
  367. /* The data is now attributed to that request so we put a
  368. * reference on it. */
  369. handle->refcnt++;
  370. handle->busy_count++;
  371. enum starpu_data_access_mode previous_mode = handle->current_mode;
  372. handle->current_mode = r_mode;
  373. /* In case we enter in a reduction mode, we invalidate all per
  374. * worker replicates. Note that the "per_node" replicates are
  375. * kept intact because we'll reduce a valid copy of the
  376. * "per-node replicate" with the per-worker replicates .*/
  377. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  378. _starpu_data_start_reduction_mode(handle);
  379. _starpu_spin_unlock(&handle->header_lock);
  380. if (r->is_requested_by_codelet)
  381. {
  382. if (!unlock_one_requester(r))
  383. _starpu_push_task(r->j);
  384. }
  385. else
  386. {
  387. STARPU_ASSERT(r->ready_data_callback);
  388. /* execute the callback associated with the data requester */
  389. r->ready_data_callback(r->argcb);
  390. }
  391. _starpu_data_requester_delete(r);
  392. _starpu_spin_lock(&handle->header_lock);
  393. STARPU_ASSERT(handle->busy_count > 0);
  394. handle->busy_count--;
  395. if (_starpu_data_check_not_busy(handle))
  396. return 1;
  397. }
  398. }
  399. // WIP_COMMUTE Begin
  400. if(handle->refcnt == 0 && handle->commute_req_list != NULL)
  401. {
  402. /* We need to delloc current handle because it is currently locked
  403. * but we alloc fist the global mutex and than the handles mutex
  404. */
  405. _starpu_spin_unlock(&handle->header_lock);
  406. #ifndef NO_LOCK_OR_DELEGATE
  407. _starpu_LockOrDelegatePostOrPerform(&_starpu_notify_commute_dependencies, handle);
  408. #else // NO_LOCK_OR_DELEGATE
  409. _starpu_notify_commute_dependencies(handle);
  410. #endif
  411. /* We need to lock when returning 0 */
  412. return 1;
  413. }
  414. // WIP_COMMUTE End
  415. return 0;
  416. }