data_concurrency.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. struct _starpu_data_descr *buffer = &(_STARPU_JOB_GET_ORDERED_BUFFERS(j)[buffer_index]);
  223. starpu_data_handle_t handle = buffer->handle;
  224. enum starpu_data_access_mode mode = buffer->mode & ~STARPU_COMMUTE;
  225. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  226. }
  227. /* Acquire all data of the given job, one by one in handle pointer value order
  228. */
  229. /* No lock is held */
  230. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  231. {
  232. unsigned buf;
  233. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  234. for (buf = start_buffer_index; buf < nbuffers; buf++)
  235. {
  236. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  237. if (buf)
  238. {
  239. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  240. if (handle_m1 == handle)
  241. /* We have already requested this data, skip it. This
  242. * depends on ordering putting writes before reads, see
  243. * _starpu_compar_handles. */
  244. continue;
  245. }
  246. STARPU_ASSERT(j->task->status == STARPU_TASK_BLOCKED || j->task->status == STARPU_TASK_BLOCKED_ON_TAG || j->task->status == STARPU_TASK_BLOCKED_ON_TASK || j->task->status == STARPU_TASK_BLOCKED_ON_DATA);
  247. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  248. if(handle->arbiter)
  249. {
  250. /* We arrived on an arbitered data, we stop and proceed
  251. * with the arbiter second step. */
  252. _starpu_submit_job_enforce_arbitered_deps(j, buf, nbuffers);
  253. return 1;
  254. }
  255. if (attempt_to_submit_data_request_from_job(j, buf))
  256. {
  257. return 1;
  258. }
  259. }
  260. return 0;
  261. }
  262. /* This is called when the tag+task dependencies are to be finished releasing. */
  263. void _starpu_enforce_data_deps_notify_job_ready_soon(struct _starpu_job *j, _starpu_notify_job_start_data *data)
  264. {
  265. unsigned buf;
  266. if (j->task->cl) {
  267. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  268. for (buf = 0; buf < nbuffers; buf++)
  269. {
  270. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  271. if (handle->arbiter)
  272. /* Oops, it's the arbiter's decision */
  273. return;
  274. }
  275. /* We need to check data availability only if sequential consistency
  276. * dependencies have not been used */
  277. if (!j->sequential_consistency) {
  278. for (buf = 0; buf < nbuffers; buf++)
  279. {
  280. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  281. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, buf) & ~STARPU_COMMUTE;
  282. if (handle->reduction_refcnt)
  283. /* Reduction pending, don't bother trying */
  284. return;
  285. if (handle->refcnt != 0 && (mode == STARPU_W || handle->current_mode != mode))
  286. /* Incompatible modes, not ready immediately */
  287. return;
  288. }
  289. }
  290. }
  291. /* Ok, it really looks like this job will be ready soon */
  292. _starpu_job_notify_ready_soon(j, data);
  293. }
  294. void _starpu_job_set_ordered_buffers(struct _starpu_job *j)
  295. {
  296. /* Compute an ordered list of the different pieces of data so that we
  297. * grab then according to a total order, thus avoiding a deadlock
  298. * condition */
  299. unsigned i;
  300. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  301. struct starpu_task *task = j->task;
  302. struct _starpu_data_descr *buffers = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  303. for (i=0 ; i<nbuffers; i++)
  304. {
  305. buffers[i].index = i;
  306. buffers[i].handle = STARPU_TASK_GET_HANDLE(task, i);
  307. buffers[i].mode = STARPU_TASK_GET_MODE(task, i);
  308. buffers[i].node = -1;
  309. }
  310. _starpu_sort_task_handles(buffers, nbuffers);
  311. for (i=0 ; i<nbuffers; i++)
  312. {
  313. buffers[buffers[i].index].orderedindex = i;
  314. }
  315. }
  316. /* Sort the data used by the given job by handle pointer value order, and
  317. * acquire them in that order */
  318. /* No lock is held */
  319. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  320. {
  321. struct starpu_codelet *cl = j->task->cl;
  322. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  323. return 0;
  324. return _submit_job_enforce_data_deps(j, 0);
  325. }
  326. /* This request got fulfilled, continue with the other requests of the
  327. * corresponding job */
  328. /* No lock is held */
  329. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  330. {
  331. struct _starpu_job *j = r->j;
  332. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  333. unsigned buffer_index = r->buffer_index;
  334. if (buffer_index + 1 < nbuffers)
  335. /* not all buffers are protected yet */
  336. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  337. else
  338. return 0;
  339. }
  340. /* This is called when a task is finished with a piece of data
  341. * (or on starpu_data_release)
  342. *
  343. * The header lock must already be taken by the caller.
  344. * This may free the handle if it was lazily unregistered (1 is returned in
  345. * that case). The handle pointer thus becomes invalid for the caller.
  346. */
  347. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  348. {
  349. _starpu_spin_checklocked(&handle->header_lock);
  350. if (handle->arbiter)
  351. {
  352. /* Keep our reference for now, _starpu_notify_arbitered_dependencies
  353. * will drop it when it needs to */
  354. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->req_list));
  355. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->reduction_req_list));
  356. _starpu_spin_unlock(&handle->header_lock);
  357. /* _starpu_notify_arbitered_dependencies will handle its own locking */
  358. _starpu_notify_arbitered_dependencies(handle);
  359. /* We have already unlocked */
  360. return 1;
  361. }
  362. /* A data access has finished so we remove a reference. */
  363. STARPU_ASSERT(handle->refcnt > 0);
  364. handle->refcnt--;
  365. STARPU_ASSERT(handle->busy_count > 0);
  366. handle->busy_count--;
  367. if (_starpu_data_check_not_busy(handle))
  368. /* Handle was destroyed, nothing left to do. */
  369. return 1;
  370. STARPU_ASSERT(_starpu_data_requester_list_empty(&handle->arbitered_req_list));
  371. /* In case there is a pending reduction, and that this is the last
  372. * requester, we may go back to a "normal" coherency model. */
  373. if (handle->reduction_refcnt > 0)
  374. {
  375. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  376. handle->reduction_refcnt--;
  377. if (handle->reduction_refcnt == 0)
  378. _starpu_data_end_reduction_mode_terminate(handle);
  379. }
  380. if (handle->unlocking_reqs)
  381. /*
  382. * Our caller is already running the unlock loop below (we were
  383. * most probably called from the ready_data_callback call
  384. * below). Avoid looping again (which would potentially mean
  385. * unbounded recursion), our caller will continue doing the
  386. * unlock work for us.
  387. */
  388. return 0;
  389. handle->unlocking_reqs = 1;
  390. struct _starpu_data_requester *r;
  391. while ((r = may_unlock_data_req_list_head(handle)))
  392. {
  393. /* STARPU_RW accesses are treated as STARPU_W */
  394. enum starpu_data_access_mode r_mode = r->mode;
  395. if (r_mode == STARPU_RW)
  396. r_mode = STARPU_W;
  397. int put_in_list = 1;
  398. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  399. {
  400. _starpu_data_end_reduction_mode(handle);
  401. /* Since we need to perform a mode change, we freeze
  402. * the request if needed. */
  403. put_in_list = (handle->reduction_refcnt > 0);
  404. }
  405. else
  406. {
  407. put_in_list = 0;
  408. }
  409. if (put_in_list)
  410. {
  411. /* We need to put the request back because we must
  412. * perform a reduction before. */
  413. _starpu_data_requester_list_push_front(&handle->req_list, r);
  414. }
  415. else
  416. {
  417. /* The data is now attributed to that request so we put a
  418. * reference on it. */
  419. handle->refcnt++;
  420. handle->busy_count++;
  421. enum starpu_data_access_mode previous_mode = handle->current_mode;
  422. handle->current_mode = r_mode;
  423. /* In case we enter in a reduction mode, we invalidate all per
  424. * worker replicates. Note that the "per_node" replicates are
  425. * kept intact because we'll reduce a valid copy of the
  426. * "per-node replicate" with the per-worker replicates .*/
  427. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  428. _starpu_data_start_reduction_mode(handle);
  429. _starpu_spin_unlock(&handle->header_lock);
  430. if (r->is_requested_by_codelet)
  431. {
  432. if (!unlock_one_requester(r))
  433. _starpu_push_task(r->j);
  434. }
  435. else
  436. {
  437. STARPU_ASSERT(r->ready_data_callback);
  438. /* execute the callback associated with the data requester */
  439. r->ready_data_callback(r->argcb);
  440. }
  441. _starpu_data_requester_delete(r);
  442. _starpu_spin_lock(&handle->header_lock);
  443. STARPU_ASSERT(handle->busy_count > 0);
  444. handle->busy_count--;
  445. if (_starpu_data_check_not_busy(handle))
  446. return 1;
  447. }
  448. }
  449. handle->unlocking_reqs = 0;
  450. return 0;
  451. }