Hardware reporting now covers all nodes
[alexxy/gromacs.git] / src / gromacs / legacyheaders / gmx_cpuid.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #ifndef GMX_CPUID_H_
36 #define GMX_CPUID_H_
37
38 #include <stdio.h>
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 #if 0
45 } /* fixes auto-indentation problems */
46 #endif
47
48
49 /* Currently identifiable CPU Vendors */
50 enum gmx_cpuid_vendor
51 {
52     GMX_CPUID_VENDOR_CANNOTDETECT,   /* Should only be used if something fails */
53     GMX_CPUID_VENDOR_UNKNOWN,
54     GMX_CPUID_VENDOR_INTEL,
55     GMX_CPUID_VENDOR_AMD,
56     GMX_CPUID_VENDOR_FUJITSU,
57     GMX_CPUID_VENDOR_IBM,
58     GMX_CPUID_VENDOR_ARM,
59     GMX_CPUID_NVENDORS
60 };
61
62
63 /* CPU feature/property list, to be used as indices into the feature array of the
64  * gmxcpuid_t data structure.
65  *
66  * To facilitate looking things up, we keep this list alphabetical.
67  * The list is NOT exhaustive - we have basically added stuff that might be
68  * useful in an application like Gromacs.
69  *
70  * AMD and Intel tend to share most architectural elements, and even if the
71  * flags might have to be detected in different ways (different cpuid registers),
72  * once the flag is present the functions should be identical. Unfortunately the
73  * trend right now (2012) seems to be that they are diverging. This means that
74  * we need to use specific flags to the compiler to maximize performance, and
75  * then the binaries might not be portable between Intel and AMD as they were
76  * before when we only needed to check for SSE and/or SSE2 support in Gromacs.
77  */
78 enum gmx_cpuid_feature
79 {
80     GMX_CPUID_FEATURE_CANNOTDETECT,      /* Flag set if we could not detect on this CPU  */
81     GMX_CPUID_FEATURE_X86_AES,           /* x86 advanced encryption standard accel.      */
82     GMX_CPUID_FEATURE_X86_APIC,          /* APIC support                                 */
83     GMX_CPUID_FEATURE_X86_AVX,           /* Advanced vector extensions                   */
84     GMX_CPUID_FEATURE_X86_AVX2,          /* AVX2 including gather support (not used yet) */
85     GMX_CPUID_FEATURE_X86_AVX_512F,      /* Foundation AVX-512 instructions              */
86     GMX_CPUID_FEATURE_X86_AVX_512PF,     /* Extended gather/scatter for AVX-512          */
87     GMX_CPUID_FEATURE_X86_AVX_512ER,     /* Extended-range 1/x and /1sqrt(x) for AVX-512 */
88     GMX_CPUID_FEATURE_X86_AVX_512CD,     /* Memory conflict-detection for AVX-512        */
89     GMX_CPUID_FEATURE_X86_CLFSH,         /* Supports CLFLUSH instruction                 */
90     GMX_CPUID_FEATURE_X86_CMOV,          /* Conditional move insn support                */
91     GMX_CPUID_FEATURE_X86_CX8,           /* Supports CMPXCHG8B (8-byte compare-exchange) */
92     GMX_CPUID_FEATURE_X86_CX16,          /* Supports CMPXCHG16B (16-byte compare-exchg)  */
93     GMX_CPUID_FEATURE_X86_F16C,          /* Supports 16-bit FP conversion instructions   */
94     GMX_CPUID_FEATURE_X86_FMA,           /* Fused-multiply add support (mainly for AVX)  */
95     GMX_CPUID_FEATURE_X86_FMA4,          /* 4-operand FMA, only on AMD for now           */
96     GMX_CPUID_FEATURE_X86_HTT,           /* Hyper-Threading supported                    */
97     GMX_CPUID_FEATURE_X86_LAHF_LM,       /* LAHF/SAHF support in 64 bits                 */
98     GMX_CPUID_FEATURE_X86_MISALIGNSSE,   /* Support for misaligned SSE data instructions */
99     GMX_CPUID_FEATURE_X86_MMX,           /* MMX registers and instructions               */
100     GMX_CPUID_FEATURE_X86_MSR,           /* Supports Intel model-specific-registers      */
101     GMX_CPUID_FEATURE_X86_NONSTOP_TSC,   /* Invariant TSC (constant rate in ACPI states) */
102     GMX_CPUID_FEATURE_X86_PCID,          /* Process context identifier support           */
103     GMX_CPUID_FEATURE_X86_PCLMULDQ,      /* Carry-less 64-bit multiplication supported   */
104     GMX_CPUID_FEATURE_X86_PDCM,          /* Perfmon and Debug Capability                 */
105     GMX_CPUID_FEATURE_X86_PDPE1GB,       /* Support for 1GB pages                        */
106     GMX_CPUID_FEATURE_X86_POPCNT,        /* Supports the POPCNT (population count) insn  */
107     GMX_CPUID_FEATURE_X86_PSE,           /* Supports 4MB-pages (page size extension)     */
108     GMX_CPUID_FEATURE_X86_RDRND,         /* RDRAND high-quality hardware random numbers  */
109     GMX_CPUID_FEATURE_X86_RDTSCP,        /* Serializing rdtscp instruction available     */
110     GMX_CPUID_FEATURE_X86_SSE2,          /* SSE 2                                        */
111     GMX_CPUID_FEATURE_X86_SSE3,          /* SSE 3                                        */
112     GMX_CPUID_FEATURE_X86_SSE4A,         /* SSE 4A                                       */
113     GMX_CPUID_FEATURE_X86_SSE4_1,        /* SSE 4.1                                      */
114     GMX_CPUID_FEATURE_X86_SSE4_2,        /* SSE 4.2                                      */
115     GMX_CPUID_FEATURE_X86_SSSE3,         /* Supplemental SSE3                            */
116     GMX_CPUID_FEATURE_X86_TDT,           /* TSC deadline timer                           */
117     GMX_CPUID_FEATURE_X86_X2APIC,        /* Extended xAPIC Support                       */
118     GMX_CPUID_FEATURE_X86_XOP,           /* AMD extended instructions, only AMD for now  */
119     GMX_CPUID_FEATURE_ARM_NEON,          /* 32-bit ARM NEON                              */
120     GMX_CPUID_FEATURE_ARM_NEON_ASIMD,    /* 64-bit ARM AArch64 Advanced SIMD             */
121     GMX_CPUID_FEATURE_IBM_QPX,           /* IBM QPX SIMD (BlueGene/Q and later)          */
122     GMX_CPUID_FEATURE_IBM_VMX,           /* IBM VMX SIMD (Altivec on Power6 and later)   */
123     GMX_CPUID_FEATURE_IBM_VSX,           /* IBM VSX SIMD (Power7 and later)              */
124     GMX_CPUID_NFEATURES
125 };
126
127
128 /* Currently supported SIMD instruction sets, intrinsics or other similar combinations
129  * in Gromacs. There is not always a 1-to-1 correspondence with feature flags; on some AMD
130  * hardware we prefer to use 128bit AVX instructions (although 256-bit ones could be executed).
131  * These are listed in increasing order for sets supported by one CPU.
132  * The order is only used for printing "minimum" and "maximum" suggested
133  * SIMD instruction sets for nodes in a cluster, so pairs like
134  * GMX_CPUID_SIMD_X86_AVX_128_FMA vs GMX_CPUID_SIMD_X86_AVX_256 which strictly
135  * speaking can't be ordered are not really an issue.
136  */
137 enum gmx_cpuid_simd
138 {
139     GMX_CPUID_SIMD_CANNOTDETECT,    /* Should only be used if something fails */
140     GMX_CPUID_SIMD_NONE,
141     GMX_CPUID_SIMD_REFERENCE,
142     GMX_CPUID_SIMD_X86_SSE2,
143     GMX_CPUID_SIMD_X86_SSE4_1,
144     GMX_CPUID_SIMD_X86_AVX_128_FMA,
145     GMX_CPUID_SIMD_X86_AVX_256,
146     GMX_CPUID_SIMD_X86_AVX2_256,
147     GMX_CPUID_SIMD_X86_AVX_512F,
148     GMX_CPUID_SIMD_X86_AVX_512ER,
149     GMX_CPUID_SIMD_SPARC64_HPC_ACE,
150     GMX_CPUID_SIMD_IBM_QPX,
151     GMX_CPUID_SIMD_IBM_VMX,
152     GMX_CPUID_SIMD_IBM_VSX,
153     GMX_CPUID_SIMD_ARM_NEON,
154     GMX_CPUID_SIMD_ARM_NEON_ASIMD,
155     GMX_CPUID_NSIMD
156 };
157
158 /* Text strings corresponding to CPU vendors */
159 extern const char *
160 gmx_cpuid_vendor_string[GMX_CPUID_NVENDORS];
161
162 /* Text strings for CPU feature indices */
163 extern const char *
164 gmx_cpuid_feature_string[GMX_CPUID_NFEATURES];
165
166 /* Text strings for Gromacs SIMD instruction sets */
167 extern const char *
168 gmx_cpuid_simd_string[GMX_CPUID_NSIMD];
169
170
171 /* Abstract data type with CPU detection information. Set by gmx_cpuid_init(). */
172 typedef struct gmx_cpuid *
173     gmx_cpuid_t;
174
175
176 /* Return the SIMD instruction set GROMACS was compiled with. */
177 enum gmx_cpuid_simd
178 gmx_compiled_simd           ();
179
180
181 /* Fill the data structure by using CPU detection instructions.
182  * Return 0 on success, 1 if something bad happened.
183  */
184 int
185 gmx_cpuid_init              (gmx_cpuid_t *              cpuid);
186
187
188 /* Return the vendor id as enumerated type. Use gmx_cpuid_vendor_string[]
189  * to get the corresponding text string.
190  */
191 enum gmx_cpuid_vendor
192 gmx_cpuid_vendor            (gmx_cpuid_t                cpuid);
193
194
195 /* Return a constant pointer to the processor brand string. */
196 const char *
197 gmx_cpuid_brand             (gmx_cpuid_t                cpuid);
198
199
200 /* Return processor family version. For a chip of version 1.2.3, this is 1 */
201 int
202 gmx_cpuid_family            (gmx_cpuid_t                cpuid);
203
204 /* Return processor model version, For a chip of version 1.2.3, this is 2. */
205 int
206 gmx_cpuid_model             (gmx_cpuid_t                cpuid);
207
208 /* Return processor stepping version, For a chip of version 1.2.3, this is 3. */
209 int
210 gmx_cpuid_stepping          (gmx_cpuid_t                cpuid);
211
212
213 /* Check whether a particular CPUID feature is set.
214  * Returns 0 if flag "feature" is not set, 1 if the flag is set. We cannot use
215  * gmx_bool here since this file must be possible to compile without simple.h.
216  */
217 int
218 gmx_cpuid_feature           (gmx_cpuid_t                cpuid,
219                              enum gmx_cpuid_feature     feature);
220
221
222 /* Check whether the CPU is an Intel with Nehalem microarchitecture.
223  * Return 0 if not Intel Nehalem, 1 if Intel Nehalem.
224  */
225 int
226 gmx_cpuid_is_intel_nehalem  (const gmx_cpuid_t          cpuid);
227
228
229 /* Return pointers to cpu topology information.
230  *
231  * Important: CPU topology requires more OS support than most other
232  * functions in this file, including support for thread pinning to hardware.
233  * This means it will not work on some platforms, including e.g. Mac OS X.
234  * Thus, it is IMPERATIVE that you check the return value from this routine
235  * before doing anything with the information. It is only if the return
236  * value is zero that the data is valid.
237  *
238  * For the returned values we have:
239  * - nprocessors         Total number of logical processors reported by OS
240  * - npackages           Usually number of CPU sockets
241  * - ncores_per_package  Number of cores in each package
242  * - nhwthreads_per_core Number of hardware threads per core; 2 for hyperthreading.
243  * - package_id          Array with the package index for each logical cpu
244  * - core_id             Array with local core index for each logical cpu
245  * - hwthread_id         Array with local hwthread index for each logical cpu
246  * - locality_order      Array with logical cpu numbers, sorted in order
247  *                       of physical and logical locality in the system.
248  *
249  * All arrays are of length nprocessors.
250  */
251 int
252 gmx_cpuid_topology(gmx_cpuid_t        cpuid,
253                    int *              nprocessors,
254                    int *              npackages,
255                    int *              ncores_per_package,
256                    int *              nhwthreads_per_core,
257                    const int **       package_id,
258                    const int **       core_id,
259                    const int **       hwthread_id,
260                    const int **       locality_order);
261
262 /* Enumerated values for x86 SMT enabled-status. Note that this does not refer
263  * to Hyper-Threading support (that is the flag GMX_CPUID_FEATURE_X86_HTT), but
264  * whether Hyper-Threading is _enabled_ and _used_ in bios right now.
265  */
266 enum gmx_cpuid_x86_smt
267 {
268     GMX_CPUID_X86_SMT_CANNOTDETECT,
269     GMX_CPUID_X86_SMT_DISABLED,
270     GMX_CPUID_X86_SMT_ENABLED
271 };
272
273 /* Returns the status of x86 SMT support. IMPORTANT: There are non-zero
274  * return values for this routine that still do not indicate supported and
275  * enabled smt/Hyper-Threading. You need to carefully check the return value
276  * against the enumerated type values to see what you are getting.
277  *
278  * Long-term, this functionality will move to a new hardware topology detection
279  * layer, but that will require a lot of new code and a working interface to the
280  * hwloc library. Surprisingly, there is no simple way to find out that
281  * Hyper-Threading is actually turned on without fully enumerating and checking
282  * all the cores, which we presently can only do on Linux. This means a couple
283  * of things:
284  *
285  * 1) If you want to know whether your CPU _supports_ Hyper-Threading in the
286  *    first place, check the GMX_CPUID_FEATURE_X86_HTT flag instead!
287  * 2) There are several scenarios where this routine will say that it cannot
288  *    detect whether SMT is enabled and used right now.
289  * 3) If you need support on non-Linux x86, you have to write it :-)
290  * 4) Don't invest too much efforts, since this will be replaced with
291  *    full hardware topology detection in the future.
292  * 5) Don't worry if the detection does not work. It is not a catastrophe, but
293  *    but we get slightly better performance on x86 if we use Hyper-Threading
294  *    cores in direct space, but not reciprocal space.
295  *
296  * Since this routine presently only supports Hyper-Threading we say X86_SMT
297  * in order not to give the impression we can detect any SMT. We haven't
298  * even tested the performance on other SMT implementations, so it is not
299  * obvious we shouldn't use SMT there.
300  *
301  * Note that you can get more complete topology information from
302  * gmx_cpuid_topology(), although that requires slightly more OS support.
303  */
304 enum gmx_cpuid_x86_smt
305 gmx_cpuid_x86_smt(gmx_cpuid_t cpuid);
306
307
308 /* Formats a text string (up to n characters) from the data structure.
309  * The output will have max 80 chars between newline characters.
310  */
311 int
312 gmx_cpuid_formatstring      (gmx_cpuid_t                cpuid,
313                              char *                     s,
314                              int                        n);
315
316
317 /* Suggests a suitable gromacs SIMD based on the support in the
318  * hardware.
319  */
320 enum gmx_cpuid_simd
321 gmx_cpuid_simd_suggest  (gmx_cpuid_t                    cpuid);
322
323
324 /* Check if this binary was compiled with the same SIMD instructions as we
325  * would suggest for the current hardware. Always print stats to the log file
326  * if it is non-NULL, and if we don't have a match, print a warning in log
327  * (if non-NULL) and if print_to_stderr!=0 also to stderr.
328  * The suggested SIMD instruction set simd_suggest is obtained with
329  * gmx_cpuid_simd_suggest(), but with MPI this might be different for
330  * different nodes, so it shoul be passed here after parallel reduction.
331  */
332 int
333 gmx_cpuid_simd_check    (enum gmx_cpuid_simd        simd_suggest,
334                          FILE *                     log,
335                          int                        print_to_stderr);
336
337
338 /* Release resources used by data structure. Note that the pointer to the
339  * CPU brand string will no longer be valid once this routine has been called.
340  */
341 void
342 gmx_cpuid_done              (gmx_cpuid_t                cpuid);
343
344
345
346
347 #ifdef __cplusplus
348 }
349 #endif
350
351
352 #endif /* GMX_CPUID_H_ */