1 /* Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@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 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.
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
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. */
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. */
28 /* This file contains system specific routines related to counting
29 online processors and dynamic load balancing. */
38 #ifdef HAVE_GETLOADAVG
39 # ifdef HAVE_SYS_LOADAVG_H
40 # include <sys/loadavg.h>
44 #ifdef HAVE_PTHREAD_AFFINITY_NP
46 cpuset_popcount (cpu_set_t *cpusetp)
49 /* glibc 2.6 and above provide a macro for this. */
50 return CPU_COUNT (cpusetp);
53 unsigned long ret = 0;
54 extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)];
57 for (i = 0; i < sizeof (*cpusetp) / sizeof (cpusetp->__bits[0]); i++)
59 unsigned long int mask = cpusetp->__bits[i];
62 ret += __builtin_popcountl (mask);
69 /* At startup, determine the default number of threads. It would seem
70 this should be related to the number of cpus online. */
73 gomp_init_num_threads (void)
75 #ifdef HAVE_PTHREAD_AFFINITY_NP
78 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset) == 0)
80 /* Count only the CPUs this process can use. */
81 gomp_nthreads_var = cpuset_popcount (&cpuset);
82 if (gomp_nthreads_var == 0)
83 gomp_nthreads_var = 1;
87 #ifdef _SC_NPROCESSORS_ONLN
88 gomp_nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
95 #ifdef HAVE_PTHREAD_AFFINITY_NP
98 if (gomp_cpu_affinity == NULL)
100 /* Count only the CPUs this process can use. */
101 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset),
104 int ret = cpuset_popcount (&cpuset);
105 return ret != 0 ? ret : 1;
111 static int affinity_cpus;
113 /* We can't use pthread_getaffinity_np in this case
114 (we have changed it ourselves, it binds to just one CPU).
115 Count instead the number of different CPUs we are
118 if (affinity_cpus == 0)
121 for (idx = 0; idx < gomp_cpu_affinity_len; idx++)
122 if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
125 CPU_SET (gomp_cpu_affinity[idx], &cpuset);
127 affinity_cpus = cpus;
129 return affinity_cpus;
132 #ifdef _SC_NPROCESSORS_ONLN
133 return sysconf (_SC_NPROCESSORS_ONLN);
135 return gomp_nthreads_var;
139 /* When OMP_DYNAMIC is set, at thread launch determine the number of
140 threads we should spawn for this team. */
141 /* ??? I have no idea what best practice for this is. Surely some
142 function of the number of processors that are *still* online and
143 the load average. Here I use the number of processors online
144 minus the 15 minute load average. */
147 gomp_dynamic_max_threads (void)
149 unsigned n_onln, loadavg;
151 n_onln = get_num_procs ();
152 if (n_onln > gomp_nthreads_var)
153 n_onln = gomp_nthreads_var;
156 #ifdef HAVE_GETLOADAVG
159 if (getloadavg (dloadavg, 3) == 3)
161 /* Add 0.1 to get a kind of biased rounding. */
162 loadavg = dloadavg[2] + 0.1;
167 if (loadavg >= n_onln)
170 return n_onln - loadavg;
174 omp_get_num_procs (void)
176 return get_num_procs ();
179 ialias (omp_get_num_procs)