Mercurial > hg > CbC > CbC_gcc
comparison libgomp/config/linux/proc.c @ 0:a06113de4d67
first commit
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2009 14:47:48 +0900 |
parents | |
children | b7f97abdc517 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a06113de4d67 |
---|---|
1 /* Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | |
2 Contributed by Jakub Jelinek <jakub@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 General Public License as published by | |
8 the Free Software Foundation; either version 3, or (at your option) | |
9 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 General Public License for | |
14 more details. | |
15 | |
16 Under Section 7 of GPL version 3, you are granted additional | |
17 permissions described in the GCC Runtime Library Exception, version | |
18 3.1, as published by the Free Software Foundation. | |
19 | |
20 You should have received a copy of the GNU General Public License and | |
21 a copy of the GCC Runtime Library Exception along with this program; | |
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 <http://www.gnu.org/licenses/>. */ | |
24 | |
25 /* This file contains system specific routines related to counting | |
26 online processors and dynamic load balancing. */ | |
27 | |
28 #ifndef _GNU_SOURCE | |
29 #define _GNU_SOURCE 1 | |
30 #endif | |
31 #include "libgomp.h" | |
32 #include <sched.h> | |
33 #include <stdlib.h> | |
34 #include <unistd.h> | |
35 #ifdef HAVE_GETLOADAVG | |
36 # ifdef HAVE_SYS_LOADAVG_H | |
37 # include <sys/loadavg.h> | |
38 # endif | |
39 #endif | |
40 | |
41 #ifdef HAVE_PTHREAD_AFFINITY_NP | |
42 static unsigned long | |
43 cpuset_popcount (cpu_set_t *cpusetp) | |
44 { | |
45 #ifdef CPU_COUNT | |
46 /* glibc 2.6 and above provide a macro for this. */ | |
47 return CPU_COUNT (cpusetp); | |
48 #else | |
49 size_t i; | |
50 unsigned long ret = 0; | |
51 extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)]; | |
52 | |
53 (void) check; | |
54 for (i = 0; i < sizeof (*cpusetp) / sizeof (cpusetp->__bits[0]); i++) | |
55 { | |
56 unsigned long int mask = cpusetp->__bits[i]; | |
57 if (mask == 0) | |
58 continue; | |
59 ret += __builtin_popcountl (mask); | |
60 } | |
61 return ret; | |
62 #endif | |
63 } | |
64 #endif | |
65 | |
66 /* At startup, determine the default number of threads. It would seem | |
67 this should be related to the number of cpus online. */ | |
68 | |
69 void | |
70 gomp_init_num_threads (void) | |
71 { | |
72 #ifdef HAVE_PTHREAD_AFFINITY_NP | |
73 cpu_set_t cpuset; | |
74 | |
75 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset) == 0) | |
76 { | |
77 /* Count only the CPUs this process can use. */ | |
78 gomp_global_icv.nthreads_var = cpuset_popcount (&cpuset); | |
79 if (gomp_global_icv.nthreads_var == 0) | |
80 gomp_global_icv.nthreads_var = 1; | |
81 return; | |
82 } | |
83 #endif | |
84 #ifdef _SC_NPROCESSORS_ONLN | |
85 gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); | |
86 #endif | |
87 } | |
88 | |
89 static int | |
90 get_num_procs (void) | |
91 { | |
92 #ifdef HAVE_PTHREAD_AFFINITY_NP | |
93 cpu_set_t cpuset; | |
94 | |
95 if (gomp_cpu_affinity == NULL) | |
96 { | |
97 /* Count only the CPUs this process can use. */ | |
98 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), | |
99 &cpuset) == 0) | |
100 { | |
101 int ret = cpuset_popcount (&cpuset); | |
102 return ret != 0 ? ret : 1; | |
103 } | |
104 } | |
105 else | |
106 { | |
107 size_t idx; | |
108 static int affinity_cpus; | |
109 | |
110 /* We can't use pthread_getaffinity_np in this case | |
111 (we have changed it ourselves, it binds to just one CPU). | |
112 Count instead the number of different CPUs we are | |
113 using. */ | |
114 CPU_ZERO (&cpuset); | |
115 if (affinity_cpus == 0) | |
116 { | |
117 int cpus = 0; | |
118 for (idx = 0; idx < gomp_cpu_affinity_len; idx++) | |
119 if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpuset)) | |
120 { | |
121 cpus++; | |
122 CPU_SET (gomp_cpu_affinity[idx], &cpuset); | |
123 } | |
124 affinity_cpus = cpus; | |
125 } | |
126 return affinity_cpus; | |
127 } | |
128 #endif | |
129 #ifdef _SC_NPROCESSORS_ONLN | |
130 return sysconf (_SC_NPROCESSORS_ONLN); | |
131 #else | |
132 return gomp_icv (false)->nthreads_var; | |
133 #endif | |
134 } | |
135 | |
136 /* When OMP_DYNAMIC is set, at thread launch determine the number of | |
137 threads we should spawn for this team. */ | |
138 /* ??? I have no idea what best practice for this is. Surely some | |
139 function of the number of processors that are *still* online and | |
140 the load average. Here I use the number of processors online | |
141 minus the 15 minute load average. */ | |
142 | |
143 unsigned | |
144 gomp_dynamic_max_threads (void) | |
145 { | |
146 unsigned n_onln, loadavg, nthreads_var = gomp_icv (false)->nthreads_var; | |
147 | |
148 n_onln = get_num_procs (); | |
149 if (n_onln > nthreads_var) | |
150 n_onln = nthreads_var; | |
151 | |
152 loadavg = 0; | |
153 #ifdef HAVE_GETLOADAVG | |
154 { | |
155 double dloadavg[3]; | |
156 if (getloadavg (dloadavg, 3) == 3) | |
157 { | |
158 /* Add 0.1 to get a kind of biased rounding. */ | |
159 loadavg = dloadavg[2] + 0.1; | |
160 } | |
161 } | |
162 #endif | |
163 | |
164 if (loadavg >= n_onln) | |
165 return 1; | |
166 else | |
167 return n_onln - loadavg; | |
168 } | |
169 | |
170 int | |
171 omp_get_num_procs (void) | |
172 { | |
173 return get_num_procs (); | |
174 } | |
175 | |
176 ialias (omp_get_num_procs) |