1*38fd1498Szrj /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2*38fd1498Szrj Contributed by Richard Henderson <rth@redhat.com>.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of the GNU Offloading and Multi Processing Library
5*38fd1498Szrj (libgomp).
6*38fd1498Szrj
7*38fd1498Szrj Libgomp is free software; you can redistribute it and/or modify it
8*38fd1498Szrj under the terms of the GNU General Public License as published by
9*38fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
10*38fd1498Szrj any later version.
11*38fd1498Szrj
12*38fd1498Szrj Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14*38fd1498Szrj FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15*38fd1498Szrj more details.
16*38fd1498Szrj
17*38fd1498Szrj Under Section 7 of GPL version 3, you are granted additional
18*38fd1498Szrj permissions described in the GCC Runtime Library Exception, version
19*38fd1498Szrj 3.1, as published by the Free Software Foundation.
20*38fd1498Szrj
21*38fd1498Szrj You should have received a copy of the GNU General Public License and
22*38fd1498Szrj a copy of the GCC Runtime Library Exception along with this program;
23*38fd1498Szrj see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24*38fd1498Szrj <http://www.gnu.org/licenses/>. */
25*38fd1498Szrj
26*38fd1498Szrj /* This file contains system specific routines related to counting
27*38fd1498Szrj online processors and dynamic load balancing. It is expected that
28*38fd1498Szrj a system may well want to write special versions of each of these.
29*38fd1498Szrj
30*38fd1498Szrj The following implementation uses a mix of POSIX and BSD routines. */
31*38fd1498Szrj
32*38fd1498Szrj #include "libgomp.h"
33*38fd1498Szrj #include <unistd.h>
34*38fd1498Szrj #include <stdlib.h>
35*38fd1498Szrj #ifdef HAVE_GETLOADAVG
36*38fd1498Szrj # ifdef HAVE_SYS_LOADAVG_H
37*38fd1498Szrj # include <sys/loadavg.h>
38*38fd1498Szrj # endif
39*38fd1498Szrj #endif
40*38fd1498Szrj
41*38fd1498Szrj
42*38fd1498Szrj /* At startup, determine the default number of threads. It would seem
43*38fd1498Szrj this should be related to the number of cpus online. */
44*38fd1498Szrj
45*38fd1498Szrj void
gomp_init_num_threads(void)46*38fd1498Szrj gomp_init_num_threads (void)
47*38fd1498Szrj {
48*38fd1498Szrj #ifdef _SC_NPROCESSORS_ONLN
49*38fd1498Szrj gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
50*38fd1498Szrj #endif
51*38fd1498Szrj }
52*38fd1498Szrj
53*38fd1498Szrj /* When OMP_DYNAMIC is set, at thread launch determine the number of
54*38fd1498Szrj threads we should spawn for this team. */
55*38fd1498Szrj /* ??? I have no idea what best practice for this is. Surely some
56*38fd1498Szrj function of the number of processors that are *still* online and
57*38fd1498Szrj the load average. Here I use the number of processors online
58*38fd1498Szrj minus the 15 minute load average. */
59*38fd1498Szrj
60*38fd1498Szrj unsigned
gomp_dynamic_max_threads(void)61*38fd1498Szrj gomp_dynamic_max_threads (void)
62*38fd1498Szrj {
63*38fd1498Szrj unsigned n_onln, loadavg;
64*38fd1498Szrj unsigned nthreads_var = gomp_icv (false)->nthreads_var;
65*38fd1498Szrj
66*38fd1498Szrj #ifdef _SC_NPROCESSORS_ONLN
67*38fd1498Szrj n_onln = sysconf (_SC_NPROCESSORS_ONLN);
68*38fd1498Szrj if (n_onln > nthreads_var)
69*38fd1498Szrj n_onln = nthreads_var;
70*38fd1498Szrj #else
71*38fd1498Szrj n_onln = nthreads_var;
72*38fd1498Szrj #endif
73*38fd1498Szrj
74*38fd1498Szrj loadavg = 0;
75*38fd1498Szrj #ifdef HAVE_GETLOADAVG
76*38fd1498Szrj {
77*38fd1498Szrj double dloadavg[3];
78*38fd1498Szrj if (getloadavg (dloadavg, 3) == 3)
79*38fd1498Szrj {
80*38fd1498Szrj /* Add 0.1 to get a kind of biased rounding. */
81*38fd1498Szrj loadavg = dloadavg[2] + 0.1;
82*38fd1498Szrj }
83*38fd1498Szrj }
84*38fd1498Szrj #endif
85*38fd1498Szrj
86*38fd1498Szrj if (loadavg >= n_onln)
87*38fd1498Szrj return 1;
88*38fd1498Szrj else
89*38fd1498Szrj return n_onln - loadavg;
90*38fd1498Szrj }
91*38fd1498Szrj
92*38fd1498Szrj int
omp_get_num_procs(void)93*38fd1498Szrj omp_get_num_procs (void)
94*38fd1498Szrj {
95*38fd1498Szrj #ifdef _SC_NPROCESSORS_ONLN
96*38fd1498Szrj return sysconf (_SC_NPROCESSORS_ONLN);
97*38fd1498Szrj #else
98*38fd1498Szrj return gomp_icv (false)->nthreads_var;
99*38fd1498Szrj #endif
100*38fd1498Szrj }
101*38fd1498Szrj
102*38fd1498Szrj ialias (omp_get_num_procs)
103