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