data_concurrency.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. /*
  22. * The different types of data accesses are STARPU_R, STARPU_RW, STARPU_W,
  23. * STARPU_SCRATCH and STARPU_REDUX. STARPU_RW is managed as a STARPU_W access.
  24. * - A single STARPU_W access is allowed at a time.
  25. * - Concurrent STARPU_R accesses are allowed.
  26. * - Concurrent STARPU_SCRATCH accesses are allowed.
  27. * - Concurrent STARPU_REDUX accesses are allowed.
  28. */
  29. /* the header lock must be taken by the caller */
  30. static unsigned may_unlock_data_req_list_head(starpu_data_handle handle,
  31. starpu_data_requester_list_t req_list)
  32. {
  33. /* if there is no one to unlock ... */
  34. if (starpu_data_requester_list_empty(req_list))
  35. return 0;
  36. /* if there is no reference to the data anymore, we can use it */
  37. if (handle->refcnt == 0)
  38. return 1;
  39. if (handle->current_mode == STARPU_W)
  40. return 0;
  41. /* data->current_mode == STARPU_R, so we can process more readers */
  42. starpu_data_requester_t r = starpu_data_requester_list_front(req_list);
  43. starpu_access_mode r_mode = r->mode;
  44. if (r_mode == STARPU_RW)
  45. r_mode = STARPU_W;
  46. /* If this is a STARPU_R, STARPU_SCRATCH or STARPU_REDUX type of
  47. * access, we only proceed if the cuurrent mode is the same as the
  48. * requested mode. */
  49. return (r_mode == handle->current_mode);
  50. }
  51. /* Try to submit a data request, in case the request can be processed
  52. * immediatly, return 0, if there is still a dependency that is not compatible
  53. * with the current mode, the request is put in the per-handle list of
  54. * "requesters", and this function returns 1. */
  55. static unsigned _starpu_attempt_to_submit_data_request(unsigned request_from_codelet,
  56. starpu_data_handle handle, starpu_access_mode mode,
  57. void (*callback)(void *), void *argcb,
  58. starpu_job_t j, unsigned buffer_index)
  59. {
  60. if (mode == STARPU_RW)
  61. mode = STARPU_W;
  62. /* Take the lock protecting the header. We try to do some progression
  63. * in case this is called from a worker, otherwise we just wait for the
  64. * lock to be available. */
  65. if (request_from_codelet)
  66. {
  67. while (_starpu_spin_trylock(&handle->header_lock))
  68. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  69. }
  70. else {
  71. _starpu_spin_lock(&handle->header_lock);
  72. }
  73. /* If we have a request that is not used for the reduction, and that a
  74. * reduction is pending, we put it at the end of normal list, and we
  75. * use the reduction_req_list instead */
  76. unsigned pending_reduction = (handle->reduction_refcnt > 0);
  77. unsigned frozen = 0;
  78. /* If we are currently performing a reduction, we freeze any request
  79. * that is not explicitely a reduction task. */
  80. unsigned is_a_reduction_task = (request_from_codelet && j->reduction_task);
  81. if (pending_reduction && !is_a_reduction_task)
  82. frozen = 1;
  83. /* If there is currently nobody accessing the piece of data, or it's
  84. * not another writter and if this is the same type of access as the
  85. * current one, we can proceed. */
  86. unsigned put_in_list = 1;
  87. starpu_access_mode previous_mode = handle->current_mode;
  88. if (!frozen && ((handle->refcnt == 0) || (!(mode == STARPU_W) && (handle->current_mode == mode))))
  89. {
  90. /* Detect whether this is the end of a reduction phase */
  91. /* We don't want to start multiple reductions of the
  92. * same handle at the same time ! */
  93. if ((handle->reduction_refcnt == 0) && (previous_mode == STARPU_REDUX) && (mode != STARPU_REDUX))
  94. {
  95. starpu_data_end_reduction_mode(handle);
  96. /* Since we need to perform a mode change, we freeze
  97. * the request if needed. */
  98. put_in_list = (handle->reduction_refcnt > 0);
  99. }
  100. else {
  101. put_in_list = 0;
  102. }
  103. }
  104. if (put_in_list)
  105. {
  106. /* there cannot be multiple writers or a new writer
  107. * while the data is in read mode */
  108. /* enqueue the request */
  109. starpu_data_requester_t r = starpu_data_requester_new();
  110. r->mode = mode;
  111. r->is_requested_by_codelet = request_from_codelet;
  112. r->j = j;
  113. r->buffer_index = buffer_index;
  114. r->ready_data_callback = callback;
  115. r->argcb = argcb;
  116. /* We put the requester in a specific list if this is a reduction task */
  117. starpu_data_requester_list_t req_list =
  118. is_a_reduction_task?handle->reduction_req_list:handle->req_list;
  119. starpu_data_requester_list_push_back(req_list, r);
  120. /* failed */
  121. put_in_list = 1;
  122. }
  123. else {
  124. handle->refcnt++;
  125. handle->current_mode = mode;
  126. if ((mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  127. starpu_data_start_reduction_mode(handle);
  128. /* success */
  129. put_in_list = 0;
  130. }
  131. _starpu_spin_unlock(&handle->header_lock);
  132. return put_in_list;
  133. }
  134. unsigned _starpu_attempt_to_submit_data_request_from_apps(starpu_data_handle handle, starpu_access_mode mode,
  135. void (*callback)(void *), void *argcb)
  136. {
  137. return _starpu_attempt_to_submit_data_request(0, handle, mode, callback, argcb, NULL, 0);
  138. }
  139. static unsigned attempt_to_submit_data_request_from_job(starpu_job_t j, unsigned buffer_index)
  140. {
  141. /* Note that we do not access j->task->buffers, but j->ordered_buffers
  142. * which is a sorted copy of it. */
  143. starpu_data_handle handle = j->ordered_buffers[buffer_index].handle;
  144. starpu_access_mode mode = j->ordered_buffers[buffer_index].mode;
  145. return _starpu_attempt_to_submit_data_request(1, handle, mode, NULL, NULL, j, buffer_index);
  146. }
  147. static unsigned _submit_job_enforce_data_deps(starpu_job_t j, unsigned start_buffer_index)
  148. {
  149. unsigned buf;
  150. unsigned nbuffers = j->task->cl->nbuffers;
  151. for (buf = start_buffer_index; buf < nbuffers; buf++)
  152. {
  153. if (attempt_to_submit_data_request_from_job(j, buf)) {
  154. j->task->status = STARPU_TASK_BLOCKED_ON_DATA;
  155. return 1;
  156. }
  157. }
  158. return 0;
  159. }
  160. /* When a new task is submitted, we make sure that there cannot be codelets
  161. with concurrent data-access at the same time in the scheduling engine (eg.
  162. there can be 2 tasks reading a piece of data, but there cannot be one
  163. reading and another writing) */
  164. unsigned _starpu_submit_job_enforce_data_deps(starpu_job_t j)
  165. {
  166. struct starpu_codelet_t *cl = j->task->cl;
  167. if ((cl == NULL) || (cl->nbuffers == 0))
  168. return 0;
  169. /* Compute an ordered list of the different pieces of data so that we
  170. * grab then according to a total order, thus avoiding a deadlock
  171. * condition */
  172. memcpy(j->ordered_buffers, j->task->buffers, cl->nbuffers*sizeof(starpu_buffer_descr));
  173. _starpu_sort_task_handles(j->ordered_buffers, cl->nbuffers);
  174. return _submit_job_enforce_data_deps(j, 0);
  175. }
  176. static unsigned unlock_one_requester(starpu_data_requester_t r)
  177. {
  178. starpu_job_t j = r->j;
  179. unsigned nbuffers = j->task->cl->nbuffers;
  180. unsigned buffer_index = r->buffer_index;
  181. if (buffer_index + 1 < nbuffers)
  182. {
  183. /* not all buffers are protected yet */
  184. return _submit_job_enforce_data_deps(j, buffer_index + 1);
  185. }
  186. else
  187. return 0;
  188. }
  189. /* The header lock must already be taken by the caller */
  190. void _starpu_notify_data_dependencies(starpu_data_handle handle)
  191. {
  192. /* A data access has finished so we remove a reference. */
  193. STARPU_ASSERT(handle->refcnt > 0);
  194. handle->refcnt--;
  195. /* The handle has been destroyed in between (eg. this was a temporary
  196. * handle created for a reduction.) */
  197. if (handle->lazy_unregister && handle->refcnt == 0)
  198. {
  199. starpu_data_unregister_no_coherency(handle);
  200. /* Warning: in case we unregister the handle, we must be sure
  201. * that the application will not try to unlock the header after
  202. * !*/
  203. return;
  204. }
  205. /* In case there is a pending reduction, and that this is the last
  206. * requester, we may go back to a "normal" coherency model. */
  207. if (handle->reduction_refcnt > 0)
  208. {
  209. //fprintf(stderr, "NOTIFY REDUCTION TASK RED REFCNT %d\n", handle->reduction_refcnt);
  210. handle->reduction_refcnt--;
  211. if (handle->reduction_refcnt == 0)
  212. starpu_data_end_reduction_mode_terminate(handle);
  213. }
  214. starpu_data_requester_list_t req_list =
  215. (handle->reduction_refcnt > 0)?handle->reduction_req_list:handle->req_list;
  216. while (may_unlock_data_req_list_head(handle, req_list))
  217. {
  218. /* Grab the head of the requester list and unlock it. */
  219. starpu_data_requester_t r = starpu_data_requester_list_pop_front(req_list);
  220. /* STARPU_RW accesses are treated as STARPU_W */
  221. starpu_access_mode r_mode = r->mode;
  222. if (r_mode == STARPU_RW)
  223. r_mode = STARPU_W;
  224. int put_in_list = 1;
  225. if ((handle->reduction_refcnt == 0) && (handle->current_mode == STARPU_REDUX) && (r_mode != STARPU_REDUX))
  226. {
  227. starpu_data_end_reduction_mode(handle);
  228. /* Since we need to perform a mode change, we freeze
  229. * the request if needed. */
  230. put_in_list = (handle->reduction_refcnt > 0);
  231. }
  232. else {
  233. put_in_list = 0;
  234. }
  235. if (put_in_list)
  236. {
  237. /* We need to put the request back because we must
  238. * perform a reduction before. */
  239. starpu_data_requester_list_push_front(req_list, r);
  240. req_list = handle->reduction_req_list;
  241. }
  242. else {
  243. /* The data is now attributed to that request so we put a
  244. * reference on it. */
  245. handle->refcnt++;
  246. starpu_access_mode previous_mode = handle->current_mode;
  247. handle->current_mode = r_mode;
  248. /* In case we enter in a reduction mode, we invalidate all per
  249. * worker replicates. Note that the "per_node" replicates are
  250. * kept intact because we'll reduce a valid copy of the
  251. * "per-node replicate" with the per-worker replicates .*/
  252. if ((r_mode == STARPU_REDUX) && (previous_mode != STARPU_REDUX))
  253. starpu_data_start_reduction_mode(handle);
  254. _starpu_spin_unlock(&handle->header_lock);
  255. if (r->is_requested_by_codelet)
  256. {
  257. if (!unlock_one_requester(r))
  258. _starpu_push_task(r->j, 0);
  259. }
  260. else
  261. {
  262. STARPU_ASSERT(r->ready_data_callback);
  263. /* execute the callback associated with the data requester */
  264. r->ready_data_callback(r->argcb);
  265. }
  266. starpu_data_requester_delete(r);
  267. _starpu_spin_lock(&handle->header_lock);
  268. }
  269. }
  270. }