1 /* Copyright (C) 2005, 2006, 2008 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@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. It is expected that
30 a system may well want to write special versions of each of these.
32 The following implementation uses a mix of POSIX and BSD routines. */
37 #ifdef HAVE_GETLOADAVG
38 # ifdef HAVE_SYS_LOADAVG_H
39 # include <sys/loadavg.h>
42 #ifdef HAVE_SYS_SYSCTL_H
43 # include <sys/sysctl.h>
49 #ifdef _SC_NPROCESSORS_ONLN
50 return sysconf (_SC_NPROCESSORS_ONLN);
53 size_t len = sizeof(ncpus);
54 sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0);
61 /* At startup, determine the default number of threads. It would seem
62 this should be related to the number of cpus online. */
65 gomp_init_num_threads (void)
67 int ncpus = get_num_procs ();
70 gomp_global_icv.nthreads_var = ncpus;
73 /* When OMP_DYNAMIC is set, at thread launch determine the number of
74 threads we should spawn for this team. */
75 /* ??? I have no idea what best practice for this is. Surely some
76 function of the number of processors that are *still* online and
77 the load average. Here I use the number of processors online
78 minus the 15 minute load average. */
81 gomp_dynamic_max_threads (void)
83 unsigned n_onln, loadavg;
84 unsigned nthreads_var = gomp_icv (false)->nthreads_var;
86 n_onln = get_num_procs ();
87 if (!n_onln || n_onln > nthreads_var)
88 n_onln = nthreads_var;
91 #ifdef HAVE_GETLOADAVG
94 if (getloadavg (dloadavg, 3) == 3)
96 /* Add 0.1 to get a kind of biased rounding. */
97 loadavg = dloadavg[2] + 0.1;
102 if (loadavg >= n_onln)
105 return n_onln - loadavg;
109 omp_get_num_procs (void)
111 int ncpus = get_num_procs ();
113 ncpus = gomp_icv (false)->nthreads_var;
117 ialias (omp_get_num_procs)