data_concurrency.c 11 KB

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