data_concurrency.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <core/dependencies/data_concurrency.h>
  17. #include <datawizard/coherency.h>
  18. #include <core/sched_policy.h>
  19. #include <common/starpu_spinlock.h>
  20. #include <datawizard/sort_data_handles.h>
  21. #include <datawizard/memory_nodes.h>
  22. /*
  23. * We have a kind of dining philosophers problem: various tasks are accessing
  24. * various data concurrently in different modes: STARPU_R, STARPU_RW, STARPU_W,
  25. * STARPU_SCRATCH and STARPU_REDUX. STARPU_RW is managed as a STARPU_W access.
  26. * We have the following constraints:
  27. *
  28. * - A single STARPU_W access is allowed at a time.
  29. * - Concurrent STARPU_R accesses are allowed.
  30. * - Concurrent STARPU_SCRATCH accesses are allowed.
  31. * - Concurrent STARPU_REDUX accesses are allowed.
  32. *
  33. * What we do here is implementing the Dijkstra solutions: handles are sorted
  34. * by pointer value order, and tasks call
  35. * _starpu_attempt_to_submit_data_request for each requested data in that order
  36. * (see _starpu_sort_task_handles call in _starpu_submit_job_enforce_data_deps).
  37. *
  38. * _starpu_attempt_to_submit_data_request will either:
  39. * - obtain access to the data, and thus the task can proceed with acquiring
  40. * other data (see _submit_job_enforce_data_deps)
  41. * - queue a request on the data handle
  42. *
  43. * When a task finishes, it calls _starpu_notify_data_dependencies for each
  44. * data, to free its acquisitions. This will look whether the first queued
  45. * request can be fulfilled, and in such case make the task try to acquire its
  46. * next data.
  47. *
  48. * The same mechanism is used for application data aquisition
  49. * (starpu_data_acquire).
  50. *
  51. * For data with an arbiter, we have a second step, performed after this first
  52. * step, implemented in data_arbiter_concurrency.c
  53. */
  54. /*
  55. * Check to see whether the first queued request can proceed, and return it in
  56. * such case.
  57. */
  58. /* the handle header lock must be taken by the caller */
  59. static struct _starpu_data_requester *may_unlock_data_req_list_head(starpu_data_handle_t handle)
  60. {
  61. struct _starpu_data_requester_prio_list *req_list;
  62. if (handle->reduction_refcnt > 0)
  63. {
  64. req_list = &handle->reduction_req_list;
  65. }
  66. else
  67. {
  68. if (_starpu_data_requester_prio_list_empty(&handle->reduction_req_list))
  69. req_list = &handle->req_list;
  70. else
  71. req_list = &handle->reduction_req_list;
  72. }
  73. /* if there is no one to unlock ... */
  74. if (_starpu_data_requester_prio_list_empty(req_list))
  75. return NULL;
  76. /* if there is no reference to the data anymore, we can use it */
  77. if (handle->refcnt == 0)
  78. return _starpu_data_requester_prio_list_pop_front_highest(req_list);
  79. /* Already writing to it, do not let another write access through */
  80. if (handle->current_mode == STARPU_W)
  81. return NULL;
  82. /* data->current_mode == STARPU_R, so we can process more readers */
  83. struct _starpu_data_requester *r = _starpu_data_requester_prio_list_front_highest(req_list);
  84. enum starpu_data_access_mode r_mode = r->mode;
  85. if (r_mode == STARPU_RW)
  86. r_mode = STARPU_W;
  87. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  88. * access, we only proceed if the current mode is the same as the
  89. * requested mode. */
  90. if (r_mode == handle->current_mode)
  91. return _starpu_data_requester_prio_list_pop_front_highest(req_list);
  92. else
  93. return NULL;
  94. }
  95. /* Try to submit a data request, in case the request can be processed
  96. * immediatly, return 0, if there is still a dependency that is not compatible
  97. * with the current mode, the request is put in the per-handle list of
  98. * "requesters", and this function returns 1. */
  99. /* No lock is held, this acquires and releases the handle header lock */
  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 (handle->arbiter)
  106. return _starpu_attempt_to_submit_arbitered_data_request(request_from_codelet, handle, mode, callback, argcb, j, buffer_index);
  107. /* Do not care about some flags */
  108. mode &= ~STARPU_COMMUTE;
  109. mode &= ~STARPU_SSEND;
  110. mode &= ~STARPU_LOCALITY;
  111. if (mode == STARPU_RW)
  112. mode = STARPU_W;
  113. /* Take the lock protecting the header. We try to do some progression
  114. * in case this is called from a worker, otherwise we just wait for the
  115. * lock to be available. */
  116. if (request_from_codelet)
  117. {
  118. int cpt = 0;
  119. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  120. {
  121. cpt++;
  122. _starpu_datawizard_progress(0);
  123. }
  124. if (cpt == STARPU_SPIN_MAXTRY)
  125. _starpu_spin_lock(&handle->header_lock);
  126. }
  127. else
  128. {
  129. _starpu_spin_lock(&handle->header_lock);
  130. }
  131. STARPU_ASSERT_MSG(!(handle->readonly && (mode & STARPU_W)), "Read-only handles can not be written to");
  132. /* If we have a request that is not used for the reduction, and that a
  133. * reduction is pending, we put it at the end of normal list, and we
  134. * use the reduction_req_list instead */
  135. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  136. unsigned frozen = 0;
  137. /* If we are currently performing a reduction, we freeze any request
  138. * that is not explicitely a reduction task. */
  139. unsigned is_a_reduction_task = (request_from_codelet && j && j->reduction_task);
  140. if (pending_reduction && !is_a_reduction_task)
  141. frozen = 1;
  142. /* If there is currently nobody accessing the piece of data, or it's
  143. * not another writter and if this is the same type of access as the
  144. * current one, we can proceed. */
  145. unsigned put_in_list = 1;
  146. enum starpu_data_access_mode previous_mode = handle->current_mode;
  147. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  148. {
  149. /* Detect whether this is the end of a reduction phase */
  150. /* We don't want to start multiple reductions of the
  151. * same handle at the same time ! */
  152. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  153. {
  154. _starpu_data_end_reduction_mode(handle);
  155. /* Since we need to perform a mode change, we freeze
  156. * the request if needed. */
  157. put_in_list = (handle->reduction_refcnt > 0);
  158. }
  159. else
  160. {
  161. put_in_list = 0;
  162. }
  163. }
  164. if (put_in_list)
  165. {
  166. /* there cannot be multiple writers or a new writer
  167. * while the data is in read mode */
  168. handle->busy_count++;
  169. /* enqueue the request */
  170. struct _starpu_data_requester *r = _starpu_data_requester_new();
  171. r->mode = mode;
  172. r->is_requested_by_codelet = request_from_codelet;
  173. r->j = j;
  174. r->buffer_index = buffer_index;
  175. r->prio = j ? j->task->priority : 0;
  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_prio_list *req_list =
  180. is_a_reduction_task?&handle->reduction_req_list:&handle->req_list;
  181. _starpu_data_requester_prio_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. /* Take a data, without waiting for it to be available (it is assumed to be).
  212. * This is typicall used for nodeps tasks, for which a previous task has already
  213. * waited for the proper conditions, and we just need to take another reference
  214. * for overall reference coherency.
  215. * No lock is held, this acquires and releases the handle header lock */
  216. static void _starpu_take_data(unsigned request_from_codelet,
  217. starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  218. struct _starpu_job *j)
  219. {
  220. STARPU_ASSERT_MSG(!handle->arbiter, "TODO");
  221. /* Do not care about some flags */
  222. mode &= ~STARPU_COMMUTE;
  223. mode &= ~STARPU_SSEND;
  224. mode &= ~STARPU_LOCALITY;
  225. if (mode == STARPU_RW)
  226. mode = STARPU_W;
  227. /* Take the lock protecting the header. We try to do some progression
  228. * in case this is called from a worker, otherwise we just wait for the
  229. * lock to be available. */
  230. if (request_from_codelet)
  231. {
  232. int cpt = 0;
  233. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  234. {
  235. cpt++;
  236. _starpu_datawizard_progress(0);
  237. }
  238. if (cpt == STARPU_SPIN_MAXTRY)
  239. _starpu_spin_lock(&handle->header_lock);
  240. }
  241. else
  242. {
  243. _starpu_spin_lock(&handle->header_lock);
  244. }
  245. /* If we are currently performing a reduction, we freeze any request
  246. * that is not explicitely a reduction task. */
  247. unsigned is_a_reduction_task = (request_from_codelet && j && j->reduction_task);
  248. STARPU_ASSERT_MSG(!is_a_reduction_task, "TODO");
  249. enum starpu_data_access_mode previous_mode = handle->current_mode;
  250. STARPU_ASSERT_MSG(mode == previous_mode, "mode was %d, but requested %d", previous_mode, mode);
  251. handle->refcnt++;
  252. handle->busy_count++;
  253. _starpu_spin_unlock(&handle->header_lock);
  254. }
  255. /* No lock is held */
  256. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle_t handle, enum starpu_data_access_mode mode,
  257. void (*callback)(void *), void *argcb)
  258. {
  259. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  260. }
  261. /* No lock is held */
  262. static unsigned attempt_to_submit_data_request_from_job(struct _starpu_job *j, unsigned buffer_index)
  263. {
  264. /* Note that we do not access j->task->handles, but j->ordered_buffers
  265. * which is a sorted copy of it. */
  266. struct _starpu_data_descr *buffer = &(_STARPU_JOB_GET_ORDERED_BUFFERS(j)[buffer_index]);
  267. starpu_data_handle_t handle = buffer->handle;
  268. enum starpu_data_access_mode mode = buffer->mode & ~STARPU_COMMUTE;
  269. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  270. }
  271. /* Try to acquire all data of the given job, one by one in handle pointer value order
  272. */
  273. /* No lock is held */
  274. static unsigned _submit_job_enforce_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  275. {
  276. unsigned buf;
  277. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  278. for (buf = start_buffer_index; buf < nbuffers; buf++)
  279. {
  280. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  281. if (buf)
  282. {
  283. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  284. if (handle_m1 == handle)
  285. /* We have already requested this data, skip it. This
  286. * depends on ordering putting writes before reads, see
  287. * _starpu_compar_handles. */
  288. continue;
  289. }
  290. 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);
  291. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  292. if(handle->arbiter)
  293. {
  294. /* We arrived on an arbitered data, we stop and proceed
  295. * with the arbiter second step. */
  296. _starpu_submit_job_enforce_arbitered_deps(j, buf, nbuffers);
  297. return 1;
  298. }
  299. if (attempt_to_submit_data_request_from_job(j, buf))
  300. {
  301. return 1;
  302. }
  303. }
  304. return 0;
  305. }
  306. static void take_data_from_job(struct _starpu_job *j, unsigned buffer_index)
  307. {
  308. /* Note that we do not access j->task->handles, but j->ordered_buffers
  309. * which is a sorted copy of it. */
  310. struct _starpu_data_descr *buffer = &(_STARPU_JOB_GET_ORDERED_BUFFERS(j)[buffer_index]);
  311. starpu_data_handle_t handle = buffer->handle;
  312. enum starpu_data_access_mode mode = buffer->mode & ~STARPU_COMMUTE;
  313. _starpu_take_data(1, handle, mode, j);
  314. }
  315. /* Immediately acquire all data of the given job, one by one in handle pointer value order
  316. */
  317. /* No lock is held */
  318. static void _submit_job_take_data_deps(struct _starpu_job *j, unsigned start_buffer_index)
  319. {
  320. unsigned buf;
  321. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  322. for (buf = start_buffer_index; buf < nbuffers; buf++)
  323. {
  324. starpu_data_handle_t handle = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf);
  325. if (buf)
  326. {
  327. starpu_data_handle_t handle_m1 = _STARPU_JOB_GET_ORDERED_BUFFER_HANDLE(j, buf-1);
  328. if (handle_m1 == handle)
  329. /* We have already requested this data, skip it. This
  330. * depends on ordering putting writes before reads, see
  331. * _starpu_compar_handles. */
  332. continue;
  333. }
  334. if(handle->arbiter)
  335. {
  336. /* We arrived on an arbitered data, we stop and proceed
  337. * with the arbiter second step. */
  338. STARPU_ASSERT_MSG(0, "TODO");
  339. //_starpu_submit_job_take_arbitered_deps(j, buf, nbuffers);
  340. }
  341. take_data_from_job(j, buf);
  342. }
  343. }
  344. /* This is called when the tag+task dependencies are to be finished releasing. */
  345. void _starpu_enforce_data_deps_notify_job_ready_soon(struct _starpu_job *j, _starpu_notify_job_start_data *data)
  346. {
  347. unsigned buf;
  348. if (j->task->cl)
  349. {
  350. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  351. for (buf = 0; buf < nbuffers; buf++)
  352. {
  353. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  354. if (handle->arbiter)
  355. /* Oops, it's the arbiter's decision */
  356. return;
  357. }
  358. /* We need to check data availability only if sequential consistency
  359. * dependencies have not been used */
  360. if (!j->sequential_consistency)
  361. {
  362. for (buf = 0; buf < nbuffers; buf++)
  363. {
  364. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(j->task, buf);
  365. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(j->task, buf) & ~STARPU_COMMUTE;
  366. if (handle->reduction_refcnt)
  367. /* Reduction pending, don't bother trying */
  368. return;
  369. if (handle->refcnt != 0 && (mode == STARPU_W || handle->current_mode != mode))
  370. /* Incompatible modes, not ready immediately */
  371. return;
  372. }
  373. }
  374. }
  375. /* Ok, it really looks like this job will be ready soon */
  376. _starpu_job_notify_ready_soon(j, data);
  377. }
  378. void _starpu_job_set_ordered_buffers(struct _starpu_job *j)
  379. {
  380. /* Compute an ordered list of the different pieces of data so that we
  381. * grab then according to a total order, thus avoiding a deadlock
  382. * condition */
  383. unsigned i;
  384. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  385. struct starpu_task *task = j->task;
  386. struct _starpu_data_descr *buffers = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  387. for (i=0 ; i<nbuffers; i++)
  388. {
  389. buffers[i].index = i;
  390. buffers[i].handle = STARPU_TASK_GET_HANDLE(task, i);
  391. buffers[i].mode = STARPU_TASK_GET_MODE(task, i);
  392. buffers[i].node = -1;
  393. }
  394. _starpu_sort_task_handles(buffers, nbuffers);
  395. for (i=0 ; i<nbuffers; i++)
  396. {
  397. buffers[buffers[i].index].orderedindex = i;
  398. }
  399. }
  400. /* Sort the data used by the given job by handle pointer value order, and
  401. * try to acquire them in that order */
  402. /* No lock is held */
  403. unsigned _starpu_submit_job_enforce_data_deps(struct _starpu_job *j)
  404. {
  405. struct starpu_codelet *cl = j->task->cl;
  406. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  407. return 0;
  408. return _submit_job_enforce_data_deps(j, 0);
  409. }
  410. /* This request got fulfilled, continue with the other requests of the
  411. * corresponding job */
  412. /* No lock is held */
  413. static unsigned unlock_one_requester(struct _starpu_data_requester *r)
  414. {
  415. struct _starpu_job *j = r->j;
  416. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  417. unsigned buffer_index = r->buffer_index;
  418. if (buffer_index + 1 < nbuffers)
  419. /* not all buffers are protected yet */
  420. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  421. else
  422. return 0;
  423. }
  424. /* Sort the data used by the given job by handle pointer value order, and
  425. * immediately acquire them in that order */
  426. /* No lock is held */
  427. void _starpu_submit_job_take_data_deps(struct _starpu_job *j)
  428. {
  429. struct starpu_codelet *cl = j->task->cl;
  430. if ((cl == NULL) || (STARPU_TASK_GET_NBUFFERS(j->task) == 0))
  431. return;
  432. _submit_job_take_data_deps(j, 0);
  433. }
  434. /* This is called when a task is finished with a piece of data
  435. * (or on starpu_data_release)
  436. *
  437. * The header lock must already be taken by the caller.
  438. * This may free the handle if it was lazily unregistered (1 is returned in
  439. * that case). The handle pointer thus becomes invalid for the caller.
  440. */
  441. int _starpu_notify_data_dependencies(starpu_data_handle_t handle)
  442. {
  443. _starpu_spin_checklocked(&handle->header_lock);
  444. if (handle->arbiter)
  445. {
  446. /* Keep our reference for now, _starpu_notify_arbitered_dependencies
  447. * will drop it when it needs to */
  448. STARPU_ASSERT(_starpu_data_requester_prio_list_empty(&handle->req_list));
  449. STARPU_ASSERT(_starpu_data_requester_prio_list_empty(&handle->reduction_req_list));
  450. _starpu_spin_unlock(&handle->header_lock);
  451. /* _starpu_notify_arbitered_dependencies will handle its own locking */
  452. _starpu_notify_arbitered_dependencies(handle);
  453. /* We have already unlocked */
  454. return 1;
  455. }
  456. /* A data access has finished so we remove a reference. */
  457. STARPU_ASSERT(handle->refcnt > 0);
  458. handle->refcnt--;
  459. STARPU_ASSERT(handle->busy_count > 0);
  460. handle->busy_count--;
  461. if (_starpu_data_check_not_busy(handle))
  462. /* Handle was destroyed, nothing left to do. */
  463. return 1;
  464. STARPU_ASSERT(_starpu_data_requester_prio_list_empty(&handle->arbitered_req_list));
  465. /* In case there is a pending reduction, and that this is the last
  466. * requester, we may go back to a "normal" coherency model. */
  467. if (handle->reduction_refcnt > 0)
  468. {
  469. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  470. handle->reduction_refcnt--;
  471. if (handle->reduction_refcnt == 0)
  472. _starpu_data_end_reduction_mode_terminate(handle);
  473. }
  474. if (handle->unlocking_reqs)
  475. /*
  476. * Our caller is already running the unlock loop below (we were
  477. * most probably called from the ready_data_callback call
  478. * below). Avoid looping again (which would potentially mean
  479. * unbounded recursion), our caller will continue doing the
  480. * unlock work for us.
  481. */
  482. return 0;
  483. handle->unlocking_reqs = 1;
  484. struct _starpu_data_requester *r;
  485. while ((r = may_unlock_data_req_list_head(handle)))
  486. {
  487. /* STARPU_RW accesses are treated as STARPU_W */
  488. enum starpu_data_access_mode r_mode = r->mode;
  489. if (r_mode == STARPU_RW)
  490. r_mode = STARPU_W;
  491. int put_in_list = 1;
  492. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  493. {
  494. _starpu_data_end_reduction_mode(handle);
  495. /* Since we need to perform a mode change, we freeze
  496. * the request if needed. */
  497. put_in_list = (handle->reduction_refcnt > 0);
  498. }
  499. else
  500. {
  501. put_in_list = 0;
  502. }
  503. if (put_in_list)
  504. {
  505. /* We need to put the request back because we must
  506. * perform a reduction before. */
  507. _starpu_data_requester_prio_list_push_front(&handle->req_list, r);
  508. }
  509. else
  510. {
  511. /* The data is now attributed to that request so we put a
  512. * reference on it. */
  513. handle->refcnt++;
  514. handle->busy_count++;
  515. enum starpu_data_access_mode previous_mode = handle->current_mode;
  516. handle->current_mode = r_mode;
  517. /* In case we enter in a reduction mode, we invalidate all per
  518. * worker replicates. Note that the "per_node" replicates are
  519. * kept intact because we'll reduce a valid copy of the
  520. * "per-node replicate" with the per-worker replicates .*/
  521. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  522. _starpu_data_start_reduction_mode(handle);
  523. _starpu_spin_unlock(&handle->header_lock);
  524. if (r->is_requested_by_codelet)
  525. {
  526. if (!unlock_one_requester(r))
  527. _starpu_push_task(r->j);
  528. }
  529. else
  530. {
  531. STARPU_ASSERT(r->ready_data_callback);
  532. /* execute the callback associated with the data requester */
  533. r->ready_data_callback(r->argcb);
  534. }
  535. _starpu_data_requester_delete(r);
  536. _starpu_spin_lock(&handle->header_lock);
  537. STARPU_ASSERT(handle->busy_count > 0);
  538. handle->busy_count--;
  539. if (_starpu_data_check_not_busy(handle))
  540. return 1;
  541. }
  542. }
  543. handle->unlocking_reqs = 0;
  544. return 0;
  545. }