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