OSDN Git Service

* c-cppbuiltin.c (c_cpp_builtins): Change _OPENMP value to
[pf3gnuchains/gcc-fork.git] / libgomp / config / posix / proc.c
1 /* Copyright (C) 2005, 2006, 2008 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 contains system specific routines related to counting
29    online processors and dynamic load balancing.  It is expected that
30    a system may well want to write special versions of each of these.
31
32    The following implementation uses a mix of POSIX and BSD routines.  */
33
34 #include "libgomp.h"
35 #include <unistd.h>
36 #include <stdlib.h>
37 #ifdef HAVE_GETLOADAVG
38 # ifdef HAVE_SYS_LOADAVG_H
39 #  include <sys/loadavg.h>
40 # endif
41 #endif
42
43
44 /* At startup, determine the default number of threads.  It would seem
45    this should be related to the number of cpus online.  */
46
47 void
48 gomp_init_num_threads (void)
49 {
50 #ifdef _SC_NPROCESSORS_ONLN
51   gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
52 #endif
53 }
54
55 /* When OMP_DYNAMIC is set, at thread launch determine the number of
56    threads we should spawn for this team.  */
57 /* ??? I have no idea what best practice for this is.  Surely some
58    function of the number of processors that are *still* online and
59    the load average.  Here I use the number of processors online
60    minus the 15 minute load average.  */
61
62 unsigned
63 gomp_dynamic_max_threads (void)
64 {
65   unsigned n_onln, loadavg;
66   unsigned nthreads_var = gomp_icv (false)->nthreads_var;
67
68 #ifdef _SC_NPROCESSORS_ONLN
69   n_onln = sysconf (_SC_NPROCESSORS_ONLN);
70   if (n_onln > nthreads_var)
71     n_onln = nthreads_var;
72 #else
73   n_onln = nthreads_var;
74 #endif
75
76   loadavg = 0;
77 #ifdef HAVE_GETLOADAVG
78   {
79     double dloadavg[3];
80     if (getloadavg (dloadavg, 3) == 3)
81       {
82         /* Add 0.1 to get a kind of biased rounding.  */
83         loadavg = dloadavg[2] + 0.1;
84       }
85   }
86 #endif
87
88   if (loadavg >= n_onln)
89     return 1;
90   else
91     return n_onln - loadavg;
92 }
93
94 int
95 omp_get_num_procs (void)
96 {
97 #ifdef _SC_NPROCESSORS_ONLN
98   return sysconf (_SC_NPROCESSORS_ONLN);
99 #else
100   return gomp_icv (false)->nthreads_var;
101 #endif
102 }
103
104 ialias (omp_get_num_procs)