00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00059
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
00071
00072
00073
00074
00075
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
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)