data_concurrency.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015,2017 Inria
  4. * Copyright (C) 2009-2015,2017-2018 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017,2018 CNRS
  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. #include <datawizard/memory_nodes.h>
  24. /*
  25. * We have a kind of dining philosophers problem: various tasks are accessing
  26. * various data concurrently in different modes: STARPU_R, STARPU_RW, STARPU_W,
  27. * STARPU_SCRATCH and STARPU_REDUX. STARPU_RW is managed as a STARPU_W access.
  28. * We have the following constraints:
  29. *
  30. * - A single STARPU_W access is allowed at a time.
  31. * - Concurrent STARPU_R accesses are allowed.
  32. * - Concurrent STARPU_SCRATCH accesses are allowed.
  33. * - Concurrent STARPU_REDUX accesses are allowed.
  34. *
  35. * What we do here is implementing the Dijkstra solutions: handles are sorted
  36. * by pointer value order, and tasks call
  37. * _starpu_attempt_to_submit_data_request for each requested data in that order
  38. * (see _starpu_sort_task_handles call in _starpu_submit_job_enforce_data_deps).
  39. *
  40. * _starpu_attempt_to_submit_data_request will either:
  41. * - obtain access to the data, and thus the task can proceed with acquiring
  42. * other data (see _submit_job_enforce_data_deps)
  43. * - queue a request on the data handle
  44. *
  45. * When a task finishes, it calls _starpu_notify_data_dependencies for each
  46. * data, to free its acquisitions. This will look whether the first queued
  47. * request can be fulfilled, and in such case make the task try to acquire its
  48. * next data.
  49. *
  50. * The same mechanism is used for application data aquisition
  51. * (starpu_data_acquire).
  52. *
  53. * For data with an arbiter, we have a second step, performed after this first
  54. * step, implemented in data_arbiter_concurrency.c
  55. */
  56. /*
  57. * Check to see whether the first queued request can proceed, and return it in
  58. * such case.
  59. */
  60. /* the handle header lock must be taken by the caller */
  61. static struct _starpu_data_requester *may_unlock_data_req_list_head(starpu_data_handle_t handle)
  62. {
  63. struct _starpu_data_requester_list *req_list;
  64. if (handle->reduction_refcnt > 0)
  65. {
  66. req_list = &handle->reduction_req_list;
  67. }
  68. else
  69. {
  70. if (_starpu_data_requester_list_empty(&handle->reduction_req_list))
  71. req_list = &handle->req_list;
  72. else
  73. req_list = &handle->reduction_req_list;
  74. }
  75. /* if there is no one to unlock ... */
  76. if (_starpu_data_requester_list_empty(req_list))
  77. return NULL;
  78. /* if there is no reference to the data anymore, we can use it */
  79. if (handle->refcnt == 0)
  80. return _starpu_data_requester_list_pop_front(req_list);
  81. /* Already writing to it, do not let another write access through */
  82. if (handle->current_mode == STARPU_W)
  83. return NULL;
  84. /* data->current_mode == STARPU_R, so we can process more readers */
  85. struct _starpu_data_requester *r = _starpu_data_requester_list_front(req_list);
  86. enum starpu_data_access_mode r_mode = r->mode;
  87. if (r_mode == STARPU_RW)
  88. r_mode = STARPU_W;
  89. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  90. * access, we only proceed if the current mode is the same as the
  91. * requested mode. */
  92. if (r_mode == handle->current_mode)
  93. return _starpu_data_requester_list_pop_front(req_list);
  94. else
  95. return NULL;
  96. }
  97. /* Try to submit a data request, in case the request can be processed
  98. * immediatly, return 0, if there is still a dependency that is not compatible
  99. * with the current mode, the request is put in the per-handle list of
  100. * "requesters", and this function returns 1. */
  101. /* No lock is held, this acquires and releases the handle header lock */
  102. static unsigned _starpu_attempt_to_submit_data_request(unsigned request_from_codelet,
  103. starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  104. void (*callback)(void *), void *argcb,
  105. struct _starpu_job *j, unsigned buffer_index)
  106. {
  107. if (handle->arbiter)
  108. return _starpu_attempt_to_submit_arbitered_data_request(request_from_codelet, handle, mode, callback, argcb, j, buffer_index);
  109. /* Do not care about some flags */
  110. mode &= ~STARPU_COMMUTE;
  111. mode &= ~STARPU_SSEND;
  112. mode &= ~STARPU_LOCALITY;
  113. if (mode == STARPU_RW)
  114. mode = STARPU_W;
  115. /* Take the lock protecting the header. We try to do some progression
  116. * in case this is called from a worker, otherwise we just wait for the
  117. * lock to be available. */
  118. if (request_from_codelet)
  119. {
  120. int cpt = 0;
  121. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  122. {
  123. cpt++;
  124. _starpu_datawizard_progress(0);
  125. }
  126. if (cpt == STARPU_SPIN_MAXTRY)
  127. _starpu_spin_lock(&handle->header_lock);
  128. }
  129. else
  130. {
  131. _starpu_spin_lock(&handle->header_lock);
  132. }
  133. /* If we have a request that is not used for the reduction, and that a
  134. * reduction is pending, we put it at the end of normal list, and we
  135. * use the reduction_req_list instead */
  136. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  137. unsigned frozen = 0;
  138. /* If we are currently performing a reduction, we freeze any request
  139. * that is not explicitely a reduction task. */
  140. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  141. if (pending_reduction && !is_a_reduction_task)
  142. frozen = 1;
  143. /* If there is currently nobody accessing the piece of data, or it's
  144. * not another writter and if this is the same type of access as the
  145. * current one, we can proceed. */
  146. unsigned put_in_list = 1;
  147. enum starpu_data_access_mode previous_mode = handle->current_mode;
  148. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  149. {
  150. /* Detect whether this is the end of a reduction phase */
  151. /* We don't want to start multiple reductions of the
  152. * same handle at the same time ! */
  153. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  154. {
  155. _starpu_data_end_reduction_mode(handle);
  156. /* Since we need to perform a mode change, we freeze
  157. * the request if needed. */
  158. put_in_list = (handle->reduction_refcnt > 0);
  159. }
  160. else
  161. {
  162. put_in_list = 0;
  163. }
  164. }
  165. if (put_in_list)
  166. {
  167. /* there cannot be multiple writers or a new writer
  168. * while the data is in read mode */
  169. handle->busy_count++;
  170. /* enqueue the request */
  171. struct _starpu_data_requester *r = _starpu_data_requester_new();
  172. r->mode = mode;
  173. r->is_requested_by_codelet = request_from_codelet;
  174. r->j = j;
  175. r->buffer_index = buffer_index;
  176. r->ready_data_callback = callback;
  177. r->argcb = argcb;
  178. /* We put the requester in a specific list if this is a reduction task */
  179. struct _starpu_data_requester_list *req_list =
  180. is_a_reduction_task?&handle->reduction_req_list:&handle->req_list;
  181. _starpu_data_requester_list_push_back(req_list, r);
  182. /* failed */
  183. put_in_list = 1;
  184. }
  185. else
  186. {
  187. handle->refcnt++;
  188. handle->busy_count++;
  189. /* Do not write to handle->current_mode if it is already
  190. * R. This avoids a spurious warning from helgrind when
  191. * the following happens:
  192. * acquire(R) in thread A
  193. * acquire(R) in thread B
  194. * release_data_on_node() in thread A
  195. * helgrind would shout that the latter reads current_mode
  196. * unsafely.
  197. *
  198. * This actually basically explains helgrind that it is a
  199. * shared R acquisition.
  200. */
  201. if (mode != STARPU_R || handle->current_mode != mode)
  202. handle->current_mode = mode;
  203. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  204. _starpu_data_start_reduction_mode(handle);
  205. /* success */
  206. put_in_list = 0;
  207. }
  208. _starpu_spin_unlock(&handle->header_lock);
  209. return put_in_list;
  210. }
  211. /* No lock is held */
  212. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  213. void (*callback)(void *), void *argcb)
  214. {
  215. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  216. }
  217. /* No lock is held */
  218. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  219. {
  220. /* Note that we do not access j->task->handles, but j->ordered_buffers
  221. * which is a sorted copy of it. */
  222. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buffer_index);
  223. enum starpu_data_access_mode mode = _STARPU_JOB_GET_ORDERED_BUFFER_MODE(j, buffer_index) & ~STARPU_COMMUTE;
  224. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  225. }
  226. /* Acquire all data of the given job, one by one in handle pointer value order
  227. */
  228. /* No lock is held */
  229. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  230. {
  231. unsigned buf;
  232. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  233. for (buf = start_buffer_index; buf < nbuffers; buf++)
  234. {
  235. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  236. if (buf)
  237. {
  238. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  239. if (handle_m1 == handle)
  240. /* We have already requested this data, skip it. This
  241. * depends on ordering putting writes before reads, see
  242. * _starpu_compar_handles. */
  243. continue;
  244. }
  245. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  246. if(handle->arbiter)
  247. {
  248. /* We arrived on an arbitered data, we stop and proceed
  249. * with the arbiter second step. */
  250. _starpu_submit_job_enforce_arbitered_deps(j, buf, nbuffers);
  251. return 1;
  252. }
  253. if (attempt_to_submit_data_request_from_job(j, buf))
  254. {
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260. /* This is called when the tag+task dependencies are to be finished releasing. */
  261. void _starpu_enforce_data_deps_notify_job_ready_soon(struct _starpu_job *j, _starpu_notify_job_start_data *data)
  262. {
  263. unsigned buf;
  264. if (j->task->cl) {
  265. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  266. for (buf = 0; buf < nbuffers; buf++)
  267. {
  268. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  269. if (handle->arbiter)
  270. /* Oops, it's the arbiter's decision */
  271. return;
  272. }
  273. /* We need to check data availability only if sequential consistency
  274. * dependencies have not been used */
  275. if (!j->sequential_consistency) {
  276. for (buf = 0; buf < nbuffers; buf++)
  277. {
  278. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  279. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, buf) & ~STARPU_COMMUTE;
  280. if (handle->reduction_refcnt)
  281. /* Reduction pending, don't bother trying */
  282. return;
  283. if (handle->refcnt != 0 && (mode == STARPU_W || handle->current_mode != mode))
  284. /* Incompatible modes, not ready immediately */
  285. return;
  286. }
  287. }
  288. }
  289. /* Ok, it really looks like this job will be ready soon */
  290. _starpu_job_notify_ready_soon(j, data);
  291. }
  292. void _starpu_job_set_ordered_buffers(struct _starpu_job *j)
  293. {
  294. /* Compute an ordered list of the different pieces of data so that we
  295. * grab then according to a total order, thus avoiding a deadlock
  296. * condition */
  297. unsigned i;
  298. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  299. struct starpu_task *task = j->task;
  300. for (i=0 ; i<nbuffers; i++)
  301. {
  302. _STARPU_JOB_SET_ORDERED_BUFFER_INDEX(j, i, i);
  303. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  304. _STARPU_JOB_SET_ORDERED_BUFFER_HANDLE(j, handle, i);
  305. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, i);
  306. _STARPU_JOB_SET_ORDERED_BUFFER_MODE(j, mode, i);
  307. int node = -1;
  308. if (task->cl->specific_nodes)
  309. node = STARPU_CODELET_GET_NODE(task->cl, i);
  310. _STARPU_JOB_SET_ORDERED_BUFFER_NODE(j, node, i);
  311. }
  312. _starpu_sort_task_handles(_STARPU_JOB_GET_ORDERED_BUFFERS(j), nbuffers);
  313. }
  314. /* Sort the data used by the given job by handle pointer value order, and
  315. * acquire them in that order */
  316. /* No lock is held */
  317. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  318. {
  319. struct starpu_codelet *cl = j->task->cl;
  320. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  321. return 0;
  322. return _submit_job_enforce_data_deps(j, 0);
  323. }
  324. /* This request got fulfilled, continue with the other requests of the
  325. * corresponding job */
  326. /* No lock is held */
  327. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  328. {
  329. struct _starpu_job *j = r->j;
  330. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  331. unsigned buffer_index = r->buffer_index;
  332. if (buffer_index + 1 < nbuffers)
  333. /* not all buffers are protected yet */
  334. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  335. else
  336. return 0;
  337. }
  338. /* This is called when a task is finished with a piece of data
  339. * (or on starpu_data_release)
  340. *
  341. * The header lock must already be taken by the caller.
  342. * This may free the handle if it was lazily unregistered (1 is returned in
  343. * that case). The handle pointer thus becomes invalid for the caller.
  344. */
  345. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  346. {
  347. _starpu_spin_checklocked(&handle->header_lock);
  348. if (handle->arbiter)
  349. {
  350. /* Keep our reference for now, _starpu_notify_arbitered_dependencies
  351. * will drop it when it needs to */
  352. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->req_list));
  353. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->reduction_req_list));
  354. _starpu_spin_unlock(&handle->header_lock);
  355. /* _starpu_notify_arbitered_dependencies will handle its own locking */
  356. _starpu_notify_arbitered_dependencies(handle);
  357. /* We have already unlocked */
  358. return 1;
  359. }
  360. /* A data access has finished so we remove a reference. */
  361. STARPU_ASSERT(handle->refcnt > 0);
  362. handle->refcnt--;
  363. STARPU_ASSERT(handle->busy_count > 0);
  364. handle->busy_count--;
  365. if (_starpu_data_check_not_busy(handle))
  366. /* Handle was destroyed, nothing left to do. */
  367. return 1;
  368. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->arbitered_req_list));
  369. /* In case there is a pending reduction, and that this is the last
  370. * requester, we may go back to a "normal" coherency model. */
  371. if (handle->reduction_refcnt > 0)
  372. {
  373. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  374. handle->reduction_refcnt--;
  375. if (handle->reduction_refcnt == 0)
  376. _starpu_data_end_reduction_mode_terminate(handle);
  377. }
  378. if (handle->unlocking_reqs)
  379. /*
  380. * Our caller is already running the unlock loop below (we were
  381. * most probably called from the ready_data_callback call
  382. * below). Avoid looping again (which would potentially mean
  383. * unbounded recursion), our caller will continue doing the
  384. * unlock work for us.
  385. */
  386. return 0;
  387. handle->unlocking_reqs = 1;
  388. struct _starpu_data_requester *r;
  389. while ((r = may_unlock_data_req_list_head(handle)))
  390. {
  391. /* STARPU_RW accesses are treated as STARPU_W */
  392. enum starpu_data_access_mode r_mode = r->mode;
  393. if (r_mode == STARPU_RW)
  394. r_mode = STARPU_W;
  395. int put_in_list = 1;
  396. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  397. {
  398. _starpu_data_end_reduction_mode(handle);
  399. /* Since we need to perform a mode change, we freeze
  400. * the request if needed. */
  401. put_in_list = (handle->reduction_refcnt > 0);
  402. }
  403. else
  404. {
  405. put_in_list = 0;
  406. }
  407. if (put_in_list)
  408. {
  409. /* We need to put the request back because we must
  410. * perform a reduction before. */
  411. _starpu_data_requester_list_push_front(&handle->req_list, r);
  412. }
  413. else
  414. {
  415. /* The data is now attributed to that request so we put a
  416. * reference on it. */
  417. handle->refcnt++;
  418. handle->busy_count++;
  419. enum starpu_data_access_mode previous_mode = handle->current_mode;
  420. handle->current_mode = r_mode;
  421. /* In case we enter in a reduction mode, we invalidate all per
  422. * worker replicates. Note that the "per_node" replicates are
  423. * kept intact because we'll reduce a valid copy of the
  424. * "per-node replicate" with the per-worker replicates .*/
  425. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  426. _starpu_data_start_reduction_mode(handle);
  427. _starpu_spin_unlock(&handle->header_lock);
  428. if (r->is_requested_by_codelet)
  429. {
  430. if (!unlock_one_requester(r))
  431. _starpu_push_task(r->j);
  432. }
  433. else
  434. {
  435. STARPU_ASSERT(r->ready_data_callback);
  436. /* execute the callback associated with the data requester */
  437. r->ready_data_callback(r->argcb);
  438. }
  439. _starpu_data_requester_delete(r);
  440. _starpu_spin_lock(&handle->header_lock);
  441. STARPU_ASSERT(handle->busy_count > 0);
  442. handle->busy_count--;
  443. if (_starpu_data_check_not_busy(handle))
  444. return 1;
  445. }
  446. }
  447. handle->unlocking_reqs = 0;
  448. return 0;
  449. }