00001 /* Copyright (C) 2005, 2006, 2008, 2009 Free Software Foundation, Inc. 00002 Contributed by Richard Henderson <rth@redhat.com>. 00003 00004 This file is part of the GNU OpenMP Library (libgomp). 00005 00006 Libgomp is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3, or (at your option) 00009 any later version. 00010 00011 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 00012 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00013 FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00014 more details. 00015 00016 Under Section 7 of GPL version 3, you are granted additional 00017 permissions described in the GCC Runtime Library Exception, version 00018 3.1, as published by the Free Software Foundation. 00019 00020 You should have received a copy of the GNU General Public License and 00021 a copy of the GCC Runtime Library Exception along with this program; 00022 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 <http://www.gnu.org/licenses/>. */ 00024 00025 /* This file contains system specific routines related to counting 00026 online processors and dynamic load balancing. It is expected that 00027 a system may well want to write special versions of each of these. 00028 00029 The following implementation uses a mix of POSIX and BSD routines. */ 00030 00031 #include "libgomp.h" 00032 #include <unistd.h> 00033 #include <stdlib.h> 00034 #ifdef HAVE_GETLOADAVG 00035 # ifdef HAVE_SYS_LOADAVG_H 00036 # include <sys/loadavg.h> 00037 # endif 00038 #endif 00039 00040 00041 /* At startup, determine the default number of threads. It would seem 00042 this should be related to the number of cpus online. */ 00043 00044 void 00045 gomp_init_num_threads (void) 00046 { 00047 #ifdef _SC_NPROCESSORS_ONLN 00048 gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); 00049 #endif 00050 } 00051 00052 /* When OMP_DYNAMIC is set, at thread launch determine the number of 00053 threads we should spawn for this team. */ 00054 /* ??? I have no idea what best practice for this is. Surely some 00055 function of the number of processors that are *still* online and 00056 the load average. Here I use the number of processors online 00057 minus the 15 minute load average. */ 00058 00059 unsigned 00060 gomp_dynamic_max_threads (void) 00061 { 00062 unsigned n_onln, loadavg; 00063 unsigned nthreads_var = gomp_icv (false)->nthreads_var; 00064 00065 #ifdef _SC_NPROCESSORS_ONLN 00066 n_onln = sysconf (_SC_NPROCESSORS_ONLN); 00067 if (n_onln > nthreads_var) 00068 n_onln = nthreads_var; 00069 #else 00070 n_onln = nthreads_var; 00071 #endif 00072 00073 loadavg = 0; 00074 #ifdef HAVE_GETLOADAVG 00075 { 00076 double dloadavg[3]; 00077 if (getloadavg (dloadavg, 3) == 3) 00078 { 00079 /* Add 0.1 to get a kind of biased rounding. */ 00080 loadavg = dloadavg[2] + 0.1; 00081 } 00082 } 00083 #endif 00084 00085 if (loadavg >= n_onln) 00086 return 1; 00087 else 00088 return n_onln - loadavg; 00089 } 00090 00091 int 00092 omp_get_num_procs (void) 00093 { 00094 #ifdef _SC_NPROCESSORS_ONLN 00095 return sysconf (_SC_NPROCESSORS_ONLN); 00096 #else 00097 return gomp_icv (false)->nthreads_var; 00098 #endif 00099 } 00100 00101 ialias (omp_get_num_procs)