1*629ff9f7SJohn Marino /* Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2*629ff9f7SJohn Marino Contributed by Richard Henderson <rth@redhat.com>.
3*629ff9f7SJohn Marino
4*629ff9f7SJohn Marino This file is part of the GNU OpenMP Library (libgomp).
5*629ff9f7SJohn Marino
6*629ff9f7SJohn Marino Libgomp is free software; you can redistribute it and/or modify it
7*629ff9f7SJohn Marino under the terms of the GNU General Public License as published by
8*629ff9f7SJohn Marino the Free Software Foundation; either version 3, or (at your option)
9*629ff9f7SJohn Marino any later version.
10*629ff9f7SJohn Marino
11*629ff9f7SJohn Marino Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*629ff9f7SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*629ff9f7SJohn Marino FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14*629ff9f7SJohn Marino more details.
15*629ff9f7SJohn Marino
16*629ff9f7SJohn Marino Under Section 7 of GPL version 3, you are granted additional
17*629ff9f7SJohn Marino permissions described in the GCC Runtime Library Exception, version
18*629ff9f7SJohn Marino 3.1, as published by the Free Software Foundation.
19*629ff9f7SJohn Marino
20*629ff9f7SJohn Marino You should have received a copy of the GNU General Public License and
21*629ff9f7SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
22*629ff9f7SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*629ff9f7SJohn Marino <http://www.gnu.org/licenses/>. */
24*629ff9f7SJohn Marino
25*629ff9f7SJohn Marino /* This file handles the SECTIONS construct. */
26*629ff9f7SJohn Marino
27*629ff9f7SJohn Marino #include "libgomp.h"
28*629ff9f7SJohn Marino
29*629ff9f7SJohn Marino
30*629ff9f7SJohn Marino /* Initialize the given work share construct from the given arguments. */
31*629ff9f7SJohn Marino
32*629ff9f7SJohn Marino static inline void
gomp_sections_init(struct gomp_work_share * ws,unsigned count)33*629ff9f7SJohn Marino gomp_sections_init (struct gomp_work_share *ws, unsigned count)
34*629ff9f7SJohn Marino {
35*629ff9f7SJohn Marino ws->sched = GFS_DYNAMIC;
36*629ff9f7SJohn Marino ws->chunk_size = 1;
37*629ff9f7SJohn Marino ws->end = count + 1L;
38*629ff9f7SJohn Marino ws->incr = 1;
39*629ff9f7SJohn Marino ws->next = 1;
40*629ff9f7SJohn Marino #ifdef HAVE_SYNC_BUILTINS
41*629ff9f7SJohn Marino /* Prepare things to make each iteration faster. */
42*629ff9f7SJohn Marino if (sizeof (long) > sizeof (unsigned))
43*629ff9f7SJohn Marino ws->mode = 1;
44*629ff9f7SJohn Marino else
45*629ff9f7SJohn Marino {
46*629ff9f7SJohn Marino struct gomp_thread *thr = gomp_thread ();
47*629ff9f7SJohn Marino struct gomp_team *team = thr->ts.team;
48*629ff9f7SJohn Marino long nthreads = team ? team->nthreads : 1;
49*629ff9f7SJohn Marino
50*629ff9f7SJohn Marino ws->mode = ((nthreads | ws->end)
51*629ff9f7SJohn Marino < 1UL << (sizeof (long) * __CHAR_BIT__ / 2 - 1));
52*629ff9f7SJohn Marino }
53*629ff9f7SJohn Marino #else
54*629ff9f7SJohn Marino ws->mode = 0;
55*629ff9f7SJohn Marino #endif
56*629ff9f7SJohn Marino }
57*629ff9f7SJohn Marino
58*629ff9f7SJohn Marino /* This routine is called when first encountering a sections construct
59*629ff9f7SJohn Marino that is not bound directly to a parallel construct. The first thread
60*629ff9f7SJohn Marino that arrives will create the work-share construct; subsequent threads
61*629ff9f7SJohn Marino will see the construct exists and allocate work from it.
62*629ff9f7SJohn Marino
63*629ff9f7SJohn Marino COUNT is the number of sections in this construct.
64*629ff9f7SJohn Marino
65*629ff9f7SJohn Marino Returns the 1-based section number for this thread to perform, or 0 if
66*629ff9f7SJohn Marino all work was assigned to other threads prior to this thread's arrival. */
67*629ff9f7SJohn Marino
68*629ff9f7SJohn Marino unsigned
GOMP_sections_start(unsigned count)69*629ff9f7SJohn Marino GOMP_sections_start (unsigned count)
70*629ff9f7SJohn Marino {
71*629ff9f7SJohn Marino struct gomp_thread *thr = gomp_thread ();
72*629ff9f7SJohn Marino long s, e, ret;
73*629ff9f7SJohn Marino
74*629ff9f7SJohn Marino if (gomp_work_share_start (false))
75*629ff9f7SJohn Marino {
76*629ff9f7SJohn Marino gomp_sections_init (thr->ts.work_share, count);
77*629ff9f7SJohn Marino gomp_work_share_init_done ();
78*629ff9f7SJohn Marino }
79*629ff9f7SJohn Marino
80*629ff9f7SJohn Marino #ifdef HAVE_SYNC_BUILTINS
81*629ff9f7SJohn Marino if (gomp_iter_dynamic_next (&s, &e))
82*629ff9f7SJohn Marino ret = s;
83*629ff9f7SJohn Marino else
84*629ff9f7SJohn Marino ret = 0;
85*629ff9f7SJohn Marino #else
86*629ff9f7SJohn Marino gomp_mutex_lock (&thr->ts.work_share->lock);
87*629ff9f7SJohn Marino if (gomp_iter_dynamic_next_locked (&s, &e))
88*629ff9f7SJohn Marino ret = s;
89*629ff9f7SJohn Marino else
90*629ff9f7SJohn Marino ret = 0;
91*629ff9f7SJohn Marino gomp_mutex_unlock (&thr->ts.work_share->lock);
92*629ff9f7SJohn Marino #endif
93*629ff9f7SJohn Marino
94*629ff9f7SJohn Marino return ret;
95*629ff9f7SJohn Marino }
96*629ff9f7SJohn Marino
97*629ff9f7SJohn Marino /* This routine is called when the thread completes processing of the
98*629ff9f7SJohn Marino section currently assigned to it. If the work-share construct is
99*629ff9f7SJohn Marino bound directly to a parallel construct, then the construct may have
100*629ff9f7SJohn Marino been set up before the parallel. In which case, this may be the
101*629ff9f7SJohn Marino first iteration for the thread.
102*629ff9f7SJohn Marino
103*629ff9f7SJohn Marino Returns the 1-based section number for this thread to perform, or 0 if
104*629ff9f7SJohn Marino all work was assigned to other threads prior to this thread's arrival. */
105*629ff9f7SJohn Marino
106*629ff9f7SJohn Marino unsigned
GOMP_sections_next(void)107*629ff9f7SJohn Marino GOMP_sections_next (void)
108*629ff9f7SJohn Marino {
109*629ff9f7SJohn Marino long s, e, ret;
110*629ff9f7SJohn Marino
111*629ff9f7SJohn Marino #ifdef HAVE_SYNC_BUILTINS
112*629ff9f7SJohn Marino if (gomp_iter_dynamic_next (&s, &e))
113*629ff9f7SJohn Marino ret = s;
114*629ff9f7SJohn Marino else
115*629ff9f7SJohn Marino ret = 0;
116*629ff9f7SJohn Marino #else
117*629ff9f7SJohn Marino struct gomp_thread *thr = gomp_thread ();
118*629ff9f7SJohn Marino
119*629ff9f7SJohn Marino gomp_mutex_lock (&thr->ts.work_share->lock);
120*629ff9f7SJohn Marino if (gomp_iter_dynamic_next_locked (&s, &e))
121*629ff9f7SJohn Marino ret = s;
122*629ff9f7SJohn Marino else
123*629ff9f7SJohn Marino ret = 0;
124*629ff9f7SJohn Marino gomp_mutex_unlock (&thr->ts.work_share->lock);
125*629ff9f7SJohn Marino #endif
126*629ff9f7SJohn Marino
127*629ff9f7SJohn Marino return ret;
128*629ff9f7SJohn Marino }
129*629ff9f7SJohn Marino
130*629ff9f7SJohn Marino /* This routine pre-initializes a work-share construct to avoid one
131*629ff9f7SJohn Marino synchronization once we get into the loop. */
132*629ff9f7SJohn Marino
133*629ff9f7SJohn Marino void
GOMP_parallel_sections_start(void (* fn)(void *),void * data,unsigned num_threads,unsigned count)134*629ff9f7SJohn Marino GOMP_parallel_sections_start (void (*fn) (void *), void *data,
135*629ff9f7SJohn Marino unsigned num_threads, unsigned count)
136*629ff9f7SJohn Marino {
137*629ff9f7SJohn Marino struct gomp_team *team;
138*629ff9f7SJohn Marino
139*629ff9f7SJohn Marino num_threads = gomp_resolve_num_threads (num_threads, count);
140*629ff9f7SJohn Marino team = gomp_new_team (num_threads);
141*629ff9f7SJohn Marino gomp_sections_init (&team->work_shares[0], count);
142*629ff9f7SJohn Marino gomp_team_start (fn, data, num_threads, team);
143*629ff9f7SJohn Marino }
144*629ff9f7SJohn Marino
145*629ff9f7SJohn Marino /* The GOMP_section_end* routines are called after the thread is told
146*629ff9f7SJohn Marino that all sections are complete. This first version synchronizes
147*629ff9f7SJohn Marino all threads; the nowait version does not. */
148*629ff9f7SJohn Marino
149*629ff9f7SJohn Marino void
GOMP_sections_end(void)150*629ff9f7SJohn Marino GOMP_sections_end (void)
151*629ff9f7SJohn Marino {
152*629ff9f7SJohn Marino gomp_work_share_end ();
153*629ff9f7SJohn Marino }
154*629ff9f7SJohn Marino
155*629ff9f7SJohn Marino void
GOMP_sections_end_nowait(void)156*629ff9f7SJohn Marino GOMP_sections_end_nowait (void)
157*629ff9f7SJohn Marino {
158*629ff9f7SJohn Marino gomp_work_share_end_nowait ();
159*629ff9f7SJohn Marino }
160