ordered.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file handles the ORDERED construct. */
  21. #include "libgomp.h"
  22. /* This function is called when first allocating an iteration block. That
  23. is, the thread is not currently on the queue. The work-share lock must
  24. be held on entry. */
  25. void
  26. gomp_ordered_first (void)
  27. {
  28. struct gomp_thread *thr = gomp_thread ();
  29. struct gomp_team *team = thr->ts.team;
  30. struct gomp_work_share *ws = thr->ts.work_share;
  31. unsigned index;
  32. /* Work share constructs can be orphaned. */
  33. if (team == NULL || team->nthreads == 1)
  34. return;
  35. index = ws->ordered_cur + ws->ordered_num_used;
  36. if (index >= team->nthreads)
  37. index -= team->nthreads;
  38. ws->ordered_team_ids[index] = thr->ts.team_id;
  39. /* If this is the first and only thread in the queue, then there is
  40. no one to release us when we get to our ordered section. Post to
  41. our own release queue now so that we won't block later. */
  42. if (ws->ordered_num_used++ == 0)
  43. gomp_sem_post (team->ordered_release[thr->ts.team_id]);
  44. }
  45. /* This function is called when completing the last iteration block. That
  46. is, there are no more iterations to perform and so the thread should be
  47. removed from the queue entirely. Because of the way ORDERED blocks are
  48. managed, it follows that we currently own access to the ORDERED block,
  49. and should now pass it on to the next thread. The work-share lock must
  50. be held on entry. */
  51. void
  52. gomp_ordered_last (void)
  53. {
  54. struct gomp_thread *thr = gomp_thread ();
  55. struct gomp_team *team = thr->ts.team;
  56. struct gomp_work_share *ws = thr->ts.work_share;
  57. unsigned next_id;
  58. /* Work share constructs can be orphaned. */
  59. if (team == NULL || team->nthreads == 1)
  60. return;
  61. /* We're no longer the owner. */
  62. ws->ordered_owner = -1;
  63. /* If we're not the last thread in the queue, then wake the next. */
  64. if (--ws->ordered_num_used > 0)
  65. {
  66. unsigned next = ws->ordered_cur + 1;
  67. if (next == team->nthreads)
  68. next = 0;
  69. ws->ordered_cur = next;
  70. next_id = ws->ordered_team_ids[next];
  71. gomp_sem_post (team->ordered_release[next_id]);
  72. }
  73. }
  74. /* This function is called when allocating a subsequent allocation block.
  75. That is, we're done with the current iteration block and we're allocating
  76. another. This is the logical combination of a call to gomp_ordered_last
  77. followed by a call to gomp_ordered_first. The work-share lock must be
  78. held on entry. */
  79. void
  80. gomp_ordered_next (void)
  81. {
  82. struct gomp_thread *thr = gomp_thread ();
  83. struct gomp_team *team = thr->ts.team;
  84. struct gomp_work_share *ws = thr->ts.work_share;
  85. unsigned index, next_id;
  86. /* Work share constructs can be orphaned. */
  87. if (team == NULL || team->nthreads == 1)
  88. return;
  89. /* We're no longer the owner. */
  90. ws->ordered_owner = -1;
  91. /* If there's only one thread in the queue, that must be us. */
  92. if (ws->ordered_num_used == 1)
  93. {
  94. /* We have a similar situation as in gomp_ordered_first
  95. where we need to post to our own release semaphore. */
  96. gomp_sem_post (team->ordered_release[thr->ts.team_id]);
  97. return;
  98. }
  99. /* If the queue is entirely full, then we move ourself to the end of
  100. the queue merely by incrementing ordered_cur. Only if it's not
  101. full do we have to write our id. */
  102. if (ws->ordered_num_used < team->nthreads)
  103. {
  104. index = ws->ordered_cur + ws->ordered_num_used;
  105. if (index >= team->nthreads)
  106. index -= team->nthreads;
  107. ws->ordered_team_ids[index] = thr->ts.team_id;
  108. }
  109. index = ws->ordered_cur + 1;
  110. if (index == team->nthreads)
  111. index = 0;
  112. ws->ordered_cur = index;
  113. next_id = ws->ordered_team_ids[index];
  114. gomp_sem_post (team->ordered_release[next_id]);
  115. }
  116. /* This function is called when a statically scheduled loop is first
  117. being created. */
  118. void
  119. gomp_ordered_static_init (void)
  120. {
  121. struct gomp_thread *thr = gomp_thread ();
  122. struct gomp_team *team = thr->ts.team;
  123. if (team == NULL || team->nthreads == 1)
  124. return;
  125. gomp_sem_post (team->ordered_release[0]);
  126. }
  127. /* This function is called when a statically scheduled loop is moving to
  128. the next allocation block. Static schedules are not first come first
  129. served like the others, so we're to move to the numerically next thread,
  130. not the next thread on a list. The work-share lock should *not* be held
  131. on entry. */
  132. void
  133. gomp_ordered_static_next (void)
  134. {
  135. struct gomp_thread *thr = gomp_thread ();
  136. struct gomp_team *team = thr->ts.team;
  137. struct gomp_work_share *ws = thr->ts.work_share;
  138. unsigned id = thr->ts.team_id;
  139. if (team == NULL || team->nthreads == 1)
  140. return;
  141. ws->ordered_owner = -1;
  142. /* This thread currently owns the lock. Increment the owner. */
  143. if (++id == team->nthreads)
  144. id = 0;
  145. ws->ordered_team_ids[0] = id;
  146. gomp_sem_post (team->ordered_release[id]);
  147. }
  148. /* This function is called when we need to assert that the thread owns the
  149. ordered section. Due to the problem of posted-but-not-waited semaphores,
  150. this needs to happen before completing a loop iteration. */
  151. void
  152. gomp_ordered_sync (void)
  153. {
  154. struct gomp_thread *thr = gomp_thread ();
  155. struct gomp_team *team = thr->ts.team;
  156. struct gomp_work_share *ws = thr->ts.work_share;
  157. /* Work share constructs can be orphaned. But this clearly means that
  158. we are the only thread, and so we automatically own the section. */
  159. if (team == NULL || team->nthreads == 1)
  160. return;
  161. /* ??? I believe it to be safe to access this data without taking the
  162. ws->lock. The only presumed race condition is with the previous
  163. thread on the queue incrementing ordered_cur such that it points
  164. to us, concurrently with our check below. But our team_id is
  165. already present in the queue, and the other thread will always
  166. post to our release semaphore. So the two cases are that we will
  167. either win the race an momentarily block on the semaphore, or lose
  168. the race and find the semaphore already unlocked and so not block.
  169. Either way we get correct results.
  170. However, there is an implicit flush on entry to an ordered region,
  171. so we do need to have a barrier here. If we were taking a lock
  172. this could be MEMMODEL_RELEASE since the acquire would be coverd
  173. by the lock. */
  174. __atomic_thread_fence (MEMMODEL_ACQ_REL);
  175. if (ws->ordered_owner != thr->ts.team_id)
  176. {
  177. gomp_sem_wait (team->ordered_release[thr->ts.team_id]);
  178. ws->ordered_owner = thr->ts.team_id;
  179. }
  180. }
  181. /* This function is called by user code when encountering the start of an
  182. ORDERED block. We must check to see if the current thread is at the
  183. head of the queue, and if not, block. */
  184. #ifdef HAVE_ATTRIBUTE_ALIAS
  185. extern void GOMP_ordered_start (void)
  186. __attribute__((alias ("gomp_ordered_sync")));
  187. #else
  188. void
  189. GOMP_ordered_start (void)
  190. {
  191. gomp_ordered_sync ();
  192. }
  193. #endif
  194. /* This function is called by user code when encountering the end of an
  195. ORDERED block. With the current ORDERED implementation there's nothing
  196. for us to do.
  197. However, the current implementation has a flaw in that it does not allow
  198. the next thread into the ORDERED section immediately after the current
  199. thread exits the ORDERED section in its last iteration. The existance
  200. of this function allows the implementation to change. */
  201. void
  202. GOMP_ordered_end (void)
  203. {
  204. }