1 /* Copyright (C) 2005, 2006 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 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. 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 Lesser General Public License for 14 more details. 15 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. */ 20 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. */ 27 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. 31 32 The following implementation uses a mix of POSIX and BSD routines. */ 33 34 #include "libgomp.h" 35 #include <unistd.h> 36 #include <stdlib.h> 37 #ifdef HAVE_GETLOADAVG 38 # ifdef HAVE_SYS_LOADAVG_H 39 # include <sys/loadavg.h> 40 # endif 41 #endif 42 43 44 /* At startup, determine the default number of threads. It would seem 45 this should be related to the number of cpus online. */ 46 47 void 48 gomp_init_num_threads (void) 49 { 50 #ifdef _SC_NPROCESSORS_ONLN 51 gomp_nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); 52 #endif 53 } 54 55 /* When OMP_DYNAMIC is set, at thread launch determine the number of 56 threads we should spawn for this team. */ 57 /* ??? I have no idea what best practice for this is. Surely some 58 function of the number of processors that are *still* online and 59 the load average. Here I use the number of processors online 60 minus the 15 minute load average. */ 61 62 unsigned 63 gomp_dynamic_max_threads (void) 64 { 65 unsigned n_onln, loadavg; 66 67 #ifdef _SC_NPROCESSORS_ONLN 68 n_onln = sysconf (_SC_NPROCESSORS_ONLN); 69 if (n_onln > gomp_nthreads_var) 70 n_onln = gomp_nthreads_var; 71 #else 72 n_onln = gomp_nthreads_var; 73 #endif 74 75 loadavg = 0; 76 #ifdef HAVE_GETLOADAVG 77 { 78 double dloadavg[3]; 79 if (getloadavg (dloadavg, 3) == 3) 80 { 81 /* Add 0.1 to get a kind of biased rounding. */ 82 loadavg = dloadavg[2] + 0.1; 83 } 84 } 85 #endif 86 87 if (loadavg >= n_onln) 88 return 1; 89 else 90 return n_onln - loadavg; 91 } 92 93 int 94 omp_get_num_procs (void) 95 { 96 #ifdef _SC_NPROCESSORS_ONLN 97 return sysconf (_SC_NPROCESSORS_ONLN); 98 #else 99 return gomp_nthreads_var; 100 #endif 101 } 102 103 ialias (omp_get_num_procs) 104