1/* Copyright (C) 2005, 2006, 2008 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#ifdef HAVE_SYS_SYSCTL_H 43# include <sys/sysctl.h> 44#endif 45 46static int 47get_num_procs (void) 48{ 49#ifdef _SC_NPROCESSORS_ONLN 50 return sysconf (_SC_NPROCESSORS_ONLN); 51#elif defined HW_NCPU 52 int ncpus = 1; 53 size_t len = sizeof(ncpus); 54 sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0); 55 return ncpus; 56#else 57 return 0; 58#endif 59} 60 61/* At startup, determine the default number of threads. It would seem 62 this should be related to the number of cpus online. */ 63 64void 65gomp_init_num_threads (void) 66{ 67 int ncpus = get_num_procs (); 68 69 if (ncpus > 0) 70 gomp_global_icv.nthreads_var = ncpus; 71} 72 73/* When OMP_DYNAMIC is set, at thread launch determine the number of 74 threads we should spawn for this team. */ 75/* ??? I have no idea what best practice for this is. Surely some 76 function of the number of processors that are *still* online and 77 the load average. Here I use the number of processors online 78 minus the 15 minute load average. */ 79 80unsigned 81gomp_dynamic_max_threads (void) 82{ 83 unsigned n_onln, loadavg; 84 unsigned nthreads_var = gomp_icv (false)->nthreads_var; 85 86 n_onln = get_num_procs (); 87 if (!n_onln || n_onln > nthreads_var) 88 n_onln = nthreads_var; 89 90 loadavg = 0; 91#ifdef HAVE_GETLOADAVG 92 { 93 double dloadavg[3]; 94 if (getloadavg (dloadavg, 3) == 3) 95 { 96 /* Add 0.1 to get a kind of biased rounding. */ 97 loadavg = dloadavg[2] + 0.1; 98 } 99 } 100#endif 101 102 if (loadavg >= n_onln) 103 return 1; 104 else 105 return n_onln - loadavg; 106} 107 108int 109omp_get_num_procs (void) 110{ 111 int ncpus = get_num_procs (); 112 if (ncpus <= 0) 113 ncpus = gomp_icv (false)->nthreads_var; 114 return ncpus; 115} 116 117ialias (omp_get_num_procs) 118