1 /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This file handles the SECTIONS construct. */
30 /* Initialize the given work share construct from the given arguments. */
33 gomp_sections_init (struct gomp_work_share
*ws
, unsigned count
)
35 ws
->sched
= GFS_DYNAMIC
;
40 #ifdef HAVE_SYNC_BUILTINS
41 /* Prepare things to make each iteration faster. */
42 if (sizeof (long) > sizeof (unsigned))
46 struct gomp_thread
*thr
= gomp_thread ();
47 struct gomp_team
*team
= thr
->ts
.team
;
48 long nthreads
= team
? team
->nthreads
: 1;
50 ws
->mode
= ((nthreads
| ws
->end
)
51 < 1UL << (sizeof (long) * __CHAR_BIT__
/ 2 - 1));
58 /* This routine is called when first encountering a sections construct
59 that is not bound directly to a parallel construct. The first thread
60 that arrives will create the work-share construct; subsequent threads
61 will see the construct exists and allocate work from it.
63 COUNT is the number of sections in this construct.
65 Returns the 1-based section number for this thread to perform, or 0 if
66 all work was assigned to other threads prior to this thread's arrival. */
69 GOMP_sections_start (unsigned count
)
71 struct gomp_thread
*thr
= gomp_thread ();
74 if (gomp_work_share_start (false))
76 gomp_sections_init (thr
->ts
.work_share
, count
);
77 gomp_work_share_init_done ();
80 #ifdef HAVE_SYNC_BUILTINS
81 if (gomp_iter_dynamic_next (&s
, &e
))
86 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
87 if (gomp_iter_dynamic_next_locked (&s
, &e
))
91 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
97 /* This routine is called when the thread completes processing of the
98 section currently assigned to it. If the work-share construct is
99 bound directly to a parallel construct, then the construct may have
100 been set up before the parallel. In which case, this may be the
101 first iteration for the thread.
103 Returns the 1-based section number for this thread to perform, or 0 if
104 all work was assigned to other threads prior to this thread's arrival. */
107 GOMP_sections_next (void)
111 #ifdef HAVE_SYNC_BUILTINS
112 if (gomp_iter_dynamic_next (&s
, &e
))
117 struct gomp_thread
*thr
= gomp_thread ();
119 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
120 if (gomp_iter_dynamic_next_locked (&s
, &e
))
124 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
130 /* This routine pre-initializes a work-share construct to avoid one
131 synchronization once we get into the loop. */
134 GOMP_parallel_sections_start (void (*fn
) (void *), void *data
,
135 unsigned num_threads
, unsigned count
)
137 struct gomp_team
*team
;
139 num_threads
= gomp_resolve_num_threads (num_threads
, count
);
140 team
= gomp_new_team (num_threads
);
141 gomp_sections_init (&team
->work_shares
[0], count
);
142 gomp_team_start (fn
, data
, num_threads
, 0, team
);
145 ialias_redirect (GOMP_parallel_end
)
148 GOMP_parallel_sections (void (*fn
) (void *), void *data
,
149 unsigned num_threads
, unsigned count
, unsigned flags
)
151 struct gomp_team
*team
;
153 num_threads
= gomp_resolve_num_threads (num_threads
, count
);
154 team
= gomp_new_team (num_threads
);
155 gomp_sections_init (&team
->work_shares
[0], count
);
156 gomp_team_start (fn
, data
, num_threads
, flags
, team
);
158 GOMP_parallel_end ();
161 /* The GOMP_section_end* routines are called after the thread is told
162 that all sections are complete. The first two versions synchronize
163 all threads; the nowait version does not. */
166 GOMP_sections_end (void)
168 gomp_work_share_end ();
172 GOMP_sections_end_cancel (void)
174 return gomp_work_share_end_cancel ();
178 GOMP_sections_end_nowait (void)
180 gomp_work_share_end_nowait ();