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 #ifndef _GNU_SOURCE
00030 #define _GNU_SOURCE 1
00031 #endif
00032 #include "libgomp.h"
00033 #include <sched.h>
00034 #include <stdlib.h>
00035 #include <unistd.h>
00036 #ifdef HAVE_GETLOADAVG
00037 # ifdef HAVE_SYS_LOADAVG_H
00038 # include <sys/loadavg.h>
00039 # endif
00040 #endif
00041
00042 #ifdef HAVE_PTHREAD_AFFINITY_NP
00043 static unsigned long
00044 cpuset_popcount (cpu_set_t *cpusetp)
00045 {
00046 #ifdef CPU_COUNT
00047
00048 return CPU_COUNT (cpusetp);
00049 #else
00050 size_t i;
00051 unsigned long ret = 0;
00052 extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)];
00053
00054 (void) check;
00055 for (i = 0; i < sizeof (*cpusetp) / sizeof (cpusetp->__bits[0]); i++)
00056 {
00057 unsigned long int mask = cpusetp->__bits[i];
00058 if (mask == 0)
00059 continue;
00060 ret += __builtin_popcountl (mask);
00061 }
00062 return ret;
00063 #endif
00064 }
00065 #endif
00066
00067
00068
00069
00070 void
00071 gomp_init_num_threads (void)
00072 {
00073 #ifdef HAVE_PTHREAD_AFFINITY_NP
00074 cpu_set_t cpuset;
00075
00076 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset) == 0)
00077 {
00078
00079 gomp_global_icv.nthreads_var = cpuset_popcount (&cpuset);
00080 if (gomp_global_icv.nthreads_var == 0)
00081 gomp_global_icv.nthreads_var = 1;
00082 return;
00083 }
00084 #endif
00085 #ifdef _SC_NPROCESSORS_ONLN
00086 gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
00087 #endif
00088 }
00089
00090 static int
00091 get_num_procs (void)
00092 {
00093 #ifdef HAVE_PTHREAD_AFFINITY_NP
00094 cpu_set_t cpuset;
00095
00096 if (gomp_cpu_affinity == NULL)
00097 {
00098
00099 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset),
00100 &cpuset) == 0)
00101 {
00102 int ret = cpuset_popcount (&cpuset);
00103 return ret != 0 ? ret : 1;
00104 }
00105 }
00106 else
00107 {
00108
00109
00110
00111
00112
00113
00114 return gomp_available_cpus;
00115 }
00116 #endif
00117 #ifdef _SC_NPROCESSORS_ONLN
00118 return sysconf (_SC_NPROCESSORS_ONLN);
00119 #else
00120 return gomp_icv (false)->nthreads_var;
00121 #endif
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 unsigned
00132 gomp_dynamic_max_threads (void)
00133 {
00134 unsigned n_onln, loadavg, nthreads_var = gomp_icv (false)->nthreads_var;
00135
00136 n_onln = get_num_procs ();
00137 if (n_onln > nthreads_var)
00138 n_onln = nthreads_var;
00139
00140 loadavg = 0;
00141 #ifdef HAVE_GETLOADAVG
00142 {
00143 double dloadavg[3];
00144 if (getloadavg (dloadavg, 3) == 3)
00145 {
00146
00147 loadavg = dloadavg[2] + 0.1;
00148 }
00149 }
00150 #endif
00151
00152 if (loadavg >= n_onln)
00153 return 1;
00154 else
00155 return n_onln - loadavg;
00156 }
00157
00158 int
00159 omp_get_num_procs (void)
00160 {
00161 return get_num_procs ();
00162 }
00163
00164 ialias (omp_get_num_procs)