1169695Skan/* Copyright (C) 2005, 2006 Free Software Foundation, Inc. 2169695Skan Contributed by Richard Henderson <rth@redhat.com>. 3169695Skan 4169695Skan This file is part of the GNU OpenMP Library (libgomp). 5169695Skan 6169695Skan Libgomp is free software; you can redistribute it and/or modify it 7169695Skan under the terms of the GNU Lesser General Public License as published by 8169695Skan the Free Software Foundation; either version 2.1 of the License, or 9169695Skan (at your option) any later version. 10169695Skan 11169695Skan Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 12169695Skan WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13169695Skan FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 14169695Skan more details. 15169695Skan 16169695Skan You should have received a copy of the GNU Lesser General Public License 17169695Skan along with libgomp; see the file COPYING.LIB. If not, write to the 18169695Skan Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19169695Skan MA 02110-1301, USA. */ 20169695Skan 21169695Skan/* As a special exception, if you link this library with other files, some 22169695Skan of which are compiled with GCC, to produce an executable, this library 23169695Skan does not by itself cause the resulting executable to be covered by the 24169695Skan GNU General Public License. This exception does not however invalidate 25169695Skan any other reasons why the executable file might be covered by the GNU 26169695Skan General Public License. */ 27169695Skan 28169695Skan/* This file contains system specific routines related to counting 29169695Skan online processors and dynamic load balancing. It is expected that 30169695Skan a system may well want to write special versions of each of these. 31169695Skan 32169695Skan The following implementation uses a mix of POSIX and BSD routines. */ 33169695Skan 34169695Skan#include "libgomp.h" 35169695Skan#include <unistd.h> 36169695Skan#include <stdlib.h> 37169695Skan#ifdef HAVE_GETLOADAVG 38169695Skan# ifdef HAVE_SYS_LOADAVG_H 39169695Skan# include <sys/loadavg.h> 40169695Skan# endif 41169695Skan#endif 42169695Skan 43169695Skan 44169695Skan/* At startup, determine the default number of threads. It would seem 45169695Skan this should be related to the number of cpus online. */ 46169695Skan 47169695Skanvoid 48169695Skangomp_init_num_threads (void) 49169695Skan{ 50169695Skan#ifdef _SC_NPROCESSORS_ONLN 51169695Skan gomp_nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); 52169695Skan#endif 53169695Skan} 54169695Skan 55169695Skan/* When OMP_DYNAMIC is set, at thread launch determine the number of 56169695Skan threads we should spawn for this team. */ 57169695Skan/* ??? I have no idea what best practice for this is. Surely some 58169695Skan function of the number of processors that are *still* online and 59169695Skan the load average. Here I use the number of processors online 60169695Skan minus the 15 minute load average. */ 61169695Skan 62169695Skanunsigned 63169695Skangomp_dynamic_max_threads (void) 64169695Skan{ 65169695Skan unsigned n_onln, loadavg; 66169695Skan 67169695Skan#ifdef _SC_NPROCESSORS_ONLN 68169695Skan n_onln = sysconf (_SC_NPROCESSORS_ONLN); 69169695Skan if (n_onln > gomp_nthreads_var) 70169695Skan n_onln = gomp_nthreads_var; 71169695Skan#else 72169695Skan n_onln = gomp_nthreads_var; 73169695Skan#endif 74169695Skan 75169695Skan loadavg = 0; 76169695Skan#ifdef HAVE_GETLOADAVG 77169695Skan { 78169695Skan double dloadavg[3]; 79169695Skan if (getloadavg (dloadavg, 3) == 3) 80169695Skan { 81169695Skan /* Add 0.1 to get a kind of biased rounding. */ 82169695Skan loadavg = dloadavg[2] + 0.1; 83169695Skan } 84169695Skan } 85169695Skan#endif 86169695Skan 87169695Skan if (loadavg >= n_onln) 88169695Skan return 1; 89169695Skan else 90169695Skan return n_onln - loadavg; 91169695Skan} 92169695Skan 93169695Skanint 94169695Skanomp_get_num_procs (void) 95169695Skan{ 96169695Skan#ifdef _SC_NPROCESSORS_ONLN 97169695Skan return sysconf (_SC_NPROCESSORS_ONLN); 98169695Skan#else 99169695Skan return gomp_nthreads_var; 100169695Skan#endif 101169695Skan} 102169695Skan 103169695Skanialias (omp_get_num_procs) 104