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