config/bsd/proc.c

Go to the documentation of this file.
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 #ifdef HAVE_SYS_SYSCTL_H
00040 # include <sys/sysctl.h>
00041 #endif
00042 
00043 static int
00044 get_num_procs (void)
00045 {
00046 #ifdef _SC_NPROCESSORS_ONLN
00047   return sysconf (_SC_NPROCESSORS_ONLN);
00048 #elif defined HW_NCPU
00049   int ncpus = 1;
00050   size_t len = sizeof(ncpus);
00051   sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0);
00052   return ncpus;
00053 #else
00054   return 0;
00055 #endif
00056 }
00057 
00058 /* At startup, determine the default number of threads.  It would seem
00059    this should be related to the number of cpus online.  */
00060 
00061 void
00062 gomp_init_num_threads (void)
00063 {
00064   int ncpus = get_num_procs ();
00065 
00066   if (ncpus > 0)
00067     gomp_global_icv.nthreads_var = ncpus;
00068 }
00069 
00070 /* When OMP_DYNAMIC is set, at thread launch determine the number of
00071    threads we should spawn for this team.  */
00072 /* ??? I have no idea what best practice for this is.  Surely some
00073    function of the number of processors that are *still* online and
00074    the load average.  Here I use the number of processors online
00075    minus the 15 minute load average.  */
00076 
00077 unsigned
00078 gomp_dynamic_max_threads (void)
00079 {
00080   unsigned n_onln, loadavg;
00081   unsigned nthreads_var = gomp_icv (false)->nthreads_var;
00082 
00083   n_onln = get_num_procs ();
00084   if (!n_onln || n_onln > nthreads_var)
00085     n_onln = nthreads_var;
00086 
00087   loadavg = 0;
00088 #ifdef HAVE_GETLOADAVG
00089   {
00090     double dloadavg[3];
00091     if (getloadavg (dloadavg, 3) == 3)
00092       {
00093     /* Add 0.1 to get a kind of biased rounding.  */
00094     loadavg = dloadavg[2] + 0.1;
00095       }
00096   }
00097 #endif
00098 
00099   if (loadavg >= n_onln)
00100     return 1;
00101   else
00102     return n_onln - loadavg;
00103 }
00104 
00105 int
00106 omp_get_num_procs (void)
00107 {
00108   int ncpus = get_num_procs ();
00109   if (ncpus <= 0)
00110     ncpus = gomp_icv (false)->nthreads_var;
00111   return ncpus;
00112 }
00113 
00114 ialias (omp_get_num_procs)

Generated on Fri Apr 5 05:38:09 2013 for Libgomp by  doxygen 1.4.7