1*404b540aSrobert /* Copyright (C) 2005, 2007 Free Software Foundation, Inc.
2*404b540aSrobert Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert
4*404b540aSrobert This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert
6*404b540aSrobert Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert (at your option) any later version.
10*404b540aSrobert
11*404b540aSrobert Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14*404b540aSrobert more details.
15*404b540aSrobert
16*404b540aSrobert You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert along with libgomp; see the file COPYING.LIB. If not, write to the
18*404b540aSrobert Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert MA 02110-1301, USA. */
20*404b540aSrobert
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert GNU General Public License. This exception does not however invalidate
25*404b540aSrobert any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert General Public License. */
27*404b540aSrobert
28*404b540aSrobert /* This file handles the SECTIONS construct. */
29*404b540aSrobert
30*404b540aSrobert #include "libgomp.h"
31*404b540aSrobert
32*404b540aSrobert
33*404b540aSrobert /* Initialize the given work share construct from the given arguments. */
34*404b540aSrobert
35*404b540aSrobert static inline void
gomp_sections_init(struct gomp_work_share * ws,unsigned count)36*404b540aSrobert gomp_sections_init (struct gomp_work_share *ws, unsigned count)
37*404b540aSrobert {
38*404b540aSrobert ws->sched = GFS_DYNAMIC;
39*404b540aSrobert ws->chunk_size = 1;
40*404b540aSrobert ws->end = count + 1;
41*404b540aSrobert ws->incr = 1;
42*404b540aSrobert ws->next = 1;
43*404b540aSrobert }
44*404b540aSrobert
45*404b540aSrobert /* This routine is called when first encountering a sections construct
46*404b540aSrobert that is not bound directly to a parallel construct. The first thread
47*404b540aSrobert that arrives will create the work-share construct; subsequent threads
48*404b540aSrobert will see the construct exists and allocate work from it.
49*404b540aSrobert
50*404b540aSrobert COUNT is the number of sections in this construct.
51*404b540aSrobert
52*404b540aSrobert Returns the 1-based section number for this thread to perform, or 0 if
53*404b540aSrobert all work was assigned to other threads prior to this thread's arrival. */
54*404b540aSrobert
55*404b540aSrobert unsigned
GOMP_sections_start(unsigned count)56*404b540aSrobert GOMP_sections_start (unsigned count)
57*404b540aSrobert {
58*404b540aSrobert struct gomp_thread *thr = gomp_thread ();
59*404b540aSrobert long s, e, ret;
60*404b540aSrobert
61*404b540aSrobert if (gomp_work_share_start (false))
62*404b540aSrobert gomp_sections_init (thr->ts.work_share, count);
63*404b540aSrobert
64*404b540aSrobert if (gomp_iter_dynamic_next_locked (&s, &e))
65*404b540aSrobert ret = s;
66*404b540aSrobert else
67*404b540aSrobert ret = 0;
68*404b540aSrobert
69*404b540aSrobert gomp_mutex_unlock (&thr->ts.work_share->lock);
70*404b540aSrobert
71*404b540aSrobert return ret;
72*404b540aSrobert }
73*404b540aSrobert
74*404b540aSrobert /* This routine is called when the thread completes processing of the
75*404b540aSrobert section currently assigned to it. If the work-share construct is
76*404b540aSrobert bound directly to a parallel construct, then the construct may have
77*404b540aSrobert been set up before the parallel. In which case, this may be the
78*404b540aSrobert first iteration for the thread.
79*404b540aSrobert
80*404b540aSrobert Returns the 1-based section number for this thread to perform, or 0 if
81*404b540aSrobert all work was assigned to other threads prior to this thread's arrival. */
82*404b540aSrobert
83*404b540aSrobert unsigned
GOMP_sections_next(void)84*404b540aSrobert GOMP_sections_next (void)
85*404b540aSrobert {
86*404b540aSrobert struct gomp_thread *thr = gomp_thread ();
87*404b540aSrobert long s, e, ret;
88*404b540aSrobert
89*404b540aSrobert gomp_mutex_lock (&thr->ts.work_share->lock);
90*404b540aSrobert if (gomp_iter_dynamic_next_locked (&s, &e))
91*404b540aSrobert ret = s;
92*404b540aSrobert else
93*404b540aSrobert ret = 0;
94*404b540aSrobert gomp_mutex_unlock (&thr->ts.work_share->lock);
95*404b540aSrobert
96*404b540aSrobert return ret;
97*404b540aSrobert }
98*404b540aSrobert
99*404b540aSrobert /* This routine pre-initializes a work-share construct to avoid one
100*404b540aSrobert synchronization once we get into the loop. */
101*404b540aSrobert
102*404b540aSrobert void
GOMP_parallel_sections_start(void (* fn)(void *),void * data,unsigned num_threads,unsigned count)103*404b540aSrobert GOMP_parallel_sections_start (void (*fn) (void *), void *data,
104*404b540aSrobert unsigned num_threads, unsigned count)
105*404b540aSrobert {
106*404b540aSrobert struct gomp_work_share *ws;
107*404b540aSrobert
108*404b540aSrobert num_threads = gomp_resolve_num_threads (num_threads);
109*404b540aSrobert if (gomp_dyn_var && num_threads > count)
110*404b540aSrobert num_threads = count;
111*404b540aSrobert
112*404b540aSrobert ws = gomp_new_work_share (false, num_threads);
113*404b540aSrobert gomp_sections_init (ws, count);
114*404b540aSrobert gomp_team_start (fn, data, num_threads, ws);
115*404b540aSrobert }
116*404b540aSrobert
117*404b540aSrobert /* The GOMP_section_end* routines are called after the thread is told
118*404b540aSrobert that all sections are complete. This first version synchronizes
119*404b540aSrobert all threads; the nowait version does not. */
120*404b540aSrobert
121*404b540aSrobert void
GOMP_sections_end(void)122*404b540aSrobert GOMP_sections_end (void)
123*404b540aSrobert {
124*404b540aSrobert gomp_work_share_end ();
125*404b540aSrobert }
126*404b540aSrobert
127*404b540aSrobert void
GOMP_sections_end_nowait(void)128*404b540aSrobert GOMP_sections_end_nowait (void)
129*404b540aSrobert {
130*404b540aSrobert gomp_work_share_end_nowait ();
131*404b540aSrobert }
132