Added AVX2 detection to cmake and created CPU acceleration macro
[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, 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_NVENDORS
59 };
60
61
62 /* CPU feature/property list, to be used as indices into the feature array of the
63  * gmxcpuid_t data structure.
64  *
65  * To facilitate looking things up, we keep this list alphabetical.
66  * The list is NOT exhaustive - we have basically added stuff that might be
67  * useful in an application like Gromacs.
68  *
69  * AMD and Intel tend to share most architectural elements, and even if the
70  * flags might have to be detected in different ways (different cpuid registers),
71  * once the flag is present the functions should be identical. Unfortunately the
72  * trend right now (2012) seems to be that they are diverging. This means that
73  * we need to use specific flags to the compiler to maximize performance, and
74  * then the binaries might not be portable between Intel and AMD as they were
75  * before when we only needed to check for SSE and/or SSE2 support in Gromacs.
76  */
77 enum gmx_cpuid_feature
78 {
79     GMX_CPUID_FEATURE_CANNOTDETECT,      /* Flag set if we could not detect on this CPU  */
80     GMX_CPUID_FEATURE_X86_AES,           /* x86 advanced encryption standard accel.      */
81     GMX_CPUID_FEATURE_X86_APIC,          /* APIC support                                 */
82     GMX_CPUID_FEATURE_X86_AVX,           /* Advanced vector extensions                   */
83     GMX_CPUID_FEATURE_X86_AVX2,          /* AVX2 including gather support (not used yet) */
84     GMX_CPUID_FEATURE_X86_CLFSH,         /* Supports CLFLUSH instruction                 */
85     GMX_CPUID_FEATURE_X86_CMOV,          /* Conditional move insn support                */
86     GMX_CPUID_FEATURE_X86_CX8,           /* Supports CMPXCHG8B (8-byte compare-exchange) */
87     GMX_CPUID_FEATURE_X86_CX16,          /* Supports CMPXCHG16B (16-byte compare-exchg)  */
88     GMX_CPUID_FEATURE_X86_F16C,          /* Supports 16-bit FP conversion instructions   */
89     GMX_CPUID_FEATURE_X86_FMA,           /* Fused-multiply add support (mainly for AVX)  */
90     GMX_CPUID_FEATURE_X86_FMA4,          /* 4-operand FMA, only on AMD for now           */
91     GMX_CPUID_FEATURE_X86_HTT,           /* Hyper-Threading supported                    */
92     GMX_CPUID_FEATURE_X86_LAHF_LM,       /* LAHF/SAHF support in 64 bits                 */
93     GMX_CPUID_FEATURE_X86_MISALIGNSSE,   /* Support for misaligned SSE data instructions */
94     GMX_CPUID_FEATURE_X86_MMX,           /* MMX registers and instructions               */
95     GMX_CPUID_FEATURE_X86_MSR,           /* Supports Intel model-specific-registers      */
96     GMX_CPUID_FEATURE_X86_NONSTOP_TSC,   /* Invariant TSC (constant rate in ACPI states) */
97     GMX_CPUID_FEATURE_X86_PCID,          /* Process context identifier support           */
98     GMX_CPUID_FEATURE_X86_PCLMULDQ,      /* Carry-less 64-bit multiplication supported   */
99     GMX_CPUID_FEATURE_X86_PDCM,          /* Perfmon and Debug Capability                 */
100     GMX_CPUID_FEATURE_X86_PDPE1GB,       /* Support for 1GB pages                        */
101     GMX_CPUID_FEATURE_X86_POPCNT,        /* Supports the POPCNT (population count) insn  */
102     GMX_CPUID_FEATURE_X86_PSE,           /* Supports 4MB-pages (page size extension)     */
103     GMX_CPUID_FEATURE_X86_RDRND,         /* RDRAND high-quality hardware random numbers  */
104     GMX_CPUID_FEATURE_X86_RDTSCP,        /* Serializing rdtscp instruction available     */
105     GMX_CPUID_FEATURE_X86_SSE2,          /* SSE 2                                        */
106     GMX_CPUID_FEATURE_X86_SSE3,          /* SSE 3                                        */
107     GMX_CPUID_FEATURE_X86_SSE4A,         /* SSE 4A                                       */
108     GMX_CPUID_FEATURE_X86_SSE4_1,        /* SSE 4.1                                      */
109     GMX_CPUID_FEATURE_X86_SSE4_2,        /* SSE 4.2                                      */
110     GMX_CPUID_FEATURE_X86_SSSE3,         /* Supplemental SSE3                            */
111     GMX_CPUID_FEATURE_X86_TDT,           /* TSC deadline timer                           */
112     GMX_CPUID_FEATURE_X86_X2APIC,        /* Extended xAPIC Support                       */
113     GMX_CPUID_FEATURE_X86_XOP,           /* AMD extended instructions, only AMD for now  */
114     GMX_CPUID_NFEATURES
115 };
116
117
118 /* Currently supported acceleration instruction sets, intrinsics or other similar combinations
119  * in Gromacs. There is not always a 1-to-1 correspondence with feature flags; on some AMD
120  * hardware we prefer to use 128bit AVX instructions (although 256-bit ones could be executed),
121  * and we still haven't written the AVX2 kernels.
122  */
123 enum gmx_cpuid_acceleration
124 {
125     GMX_CPUID_ACCELERATION_CANNOTDETECT,    /* Should only be used if something fails */
126     GMX_CPUID_ACCELERATION_NONE,
127     GMX_CPUID_ACCELERATION_X86_SSE2,
128     GMX_CPUID_ACCELERATION_X86_SSE4_1,
129     GMX_CPUID_ACCELERATION_X86_AVX_128_FMA,
130     GMX_CPUID_ACCELERATION_X86_AVX_256,
131     GMX_CPUID_ACCELERATION_X86_AVX2_256,
132     GMX_CPUID_ACCELERATION_SPARC64_HPC_ACE,
133     GMX_CPUID_ACCELERATION_IBM_QPX,
134     GMX_CPUID_NACCELERATIONS
135 };
136
137 /* Text strings corresponding to CPU vendors */
138 extern const char *
139 gmx_cpuid_vendor_string[GMX_CPUID_NVENDORS];
140
141 /* Text strings for CPU feature indices */
142 extern const char *
143 gmx_cpuid_feature_string[GMX_CPUID_NFEATURES];
144
145 /* Text strings for Gromacs acceleration/instruction sets */
146 extern const char *
147 gmx_cpuid_acceleration_string[GMX_CPUID_NACCELERATIONS];
148
149
150 /* Abstract data type with CPU detection information. Set by gmx_cpuid_init(). */
151 typedef struct gmx_cpuid *
152     gmx_cpuid_t;
153
154
155 /* Fill the data structure by using CPU detection instructions.
156  * Return 0 on success, 1 if something bad happened.
157  */
158 int
159 gmx_cpuid_init              (gmx_cpuid_t *              cpuid);
160
161
162 /* Return the vendor id as enumerated type. Use gmx_cpuid_vendor_string[]
163  * to get the corresponding text string.
164  */
165 enum gmx_cpuid_vendor
166 gmx_cpuid_vendor            (gmx_cpuid_t                cpuid);
167
168
169 /* Return a constant pointer to the processor brand string. */
170 const char *
171 gmx_cpuid_brand             (gmx_cpuid_t                cpuid);
172
173
174 /* Return processor family version. For a chip of version 1.2.3, this is 1 */
175 int
176 gmx_cpuid_family            (gmx_cpuid_t                cpuid);
177
178 /* Return processor model version, For a chip of version 1.2.3, this is 2. */
179 int
180 gmx_cpuid_model             (gmx_cpuid_t                cpuid);
181
182 /* Return processor stepping version, For a chip of version 1.2.3, this is 3. */
183 int
184 gmx_cpuid_stepping          (gmx_cpuid_t                cpuid);
185
186
187 /* Check whether a particular CPUID feature is set.
188  * Returns 0 if flag "feature" is not set, 1 if the flag is set. We cannot use
189  * gmx_bool here since this file must be possible to compile without simple.h.
190  */
191 int
192 gmx_cpuid_feature           (gmx_cpuid_t                cpuid,
193                              enum gmx_cpuid_feature     feature);
194
195
196 /* Return pointers to cpu topology information.
197  *
198  * Important: CPU topology requires more OS support than most other
199  * functions in this file, including support for thread pinning to hardware.
200  * This means it will not work on some platforms, including e.g. Mac OS X.
201  * Thus, it is IMPERATIVE that you check the return value from this routine
202  * before doing anything with the information. It is only if the return
203  * value is zero that the data is valid.
204  *
205  * For the returned values we have:
206  * - nprocessors         Total number of logical processors reported by OS
207  * - npackages           Usually number of CPU sockets
208  * - ncores_per_package  Number of cores in each package
209  * - nhwthreads_per_core Number of hardware threads per core; 2 for hyperthreading.
210  * - package_id          Array with the package index for each logical cpu
211  * - core_id             Array with local core index for each logical cpu
212  * - hwthread_id         Array with local hwthread index for each logical cpu
213  * - locality_order      Array with logical cpu numbers, sorted in order
214  *                       of physical and logical locality in the system.
215  *
216  * All arrays are of length nprocessors.
217  */
218 int
219 gmx_cpuid_topology(gmx_cpuid_t        cpuid,
220                    int *              nprocessors,
221                    int *              npackages,
222                    int *              ncores_per_package,
223                    int *              nhwthreads_per_core,
224                    const int **       package_id,
225                    const int **       core_id,
226                    const int **       hwthread_id,
227                    const int **       locality_order);
228
229 /* Enumerated values for x86 SMT enabled-status. Note that this does not refer
230  * to Hyper-Threading support (that is the flag GMX_CPUID_FEATURE_X86_HTT), but
231  * whether Hyper-Threading is _enabled_ and _used_ in bios right now.
232  */
233 enum gmx_cpuid_x86_smt
234 {
235     GMX_CPUID_X86_SMT_CANNOTDETECT,
236     GMX_CPUID_X86_SMT_DISABLED,
237     GMX_CPUID_X86_SMT_ENABLED
238 };
239
240 /* Returns the status of x86 SMT support. IMPORTANT: There are non-zero
241  * return values for this routine that still do not indicate supported and
242  * enabled smt/Hyper-Threading. You need to carefully check the return value
243  * against the enumerated type values to see what you are getting.
244  *
245  * Long-term, this functionality will move to a new hardware topology detection
246  * layer, but that will require a lot of new code and a working interface to the
247  * hwloc library. Surprisingly, there is no simple way to find out that
248  * Hyper-Threading is actually turned on without fully enumerating and checking
249  * all the cores, which we presently can only do on Linux. This means a couple
250  * of things:
251  *
252  * 1) If you want to know whether your CPU _supports_ Hyper-Threading in the
253  *    first place, check the GMX_CPUID_FEATURE_X86_HTT flag instead!
254  * 2) There are several scenarios where this routine will say that it cannot
255  *    detect whether SMT is enabled and used right now.
256  * 3) If you need support on non-Linux x86, you have to write it :-)
257  * 4) Don't invest too much efforts, since this will be replaced with
258  *    full hardware topology detection in the future.
259  * 5) Don't worry if the detection does not work. It is not a catastrophe, but
260  *    but we get slightly better performance on x86 if we use Hyper-Threading
261  *    cores in direct space, but not reciprocal space.
262  *
263  * Since this routine presently only supports Hyper-Threading we say X86_SMT
264  * in order not to give the impression we can detect any SMT. We haven't
265  * even tested the performance on other SMT implementations, so it is not
266  * obvious we shouldn't use SMT there.
267  *
268  * Note that you can get more complete topology information from
269  * gmx_cpuid_topology(), although that requires slightly more OS support.
270  */
271 enum gmx_cpuid_x86_smt
272 gmx_cpuid_x86_smt(gmx_cpuid_t cpuid);
273
274
275 /* Formats a text string (up to n characters) from the data structure.
276  * The output will have max 80 chars between newline characters.
277  */
278 int
279 gmx_cpuid_formatstring      (gmx_cpuid_t                cpuid,
280                              char *                     s,
281                              int                        n);
282
283
284 /* Suggests a suitable gromacs acceleration based on the support in the
285  * hardware.
286  */
287 enum gmx_cpuid_acceleration
288 gmx_cpuid_acceleration_suggest  (gmx_cpuid_t                    cpuid);
289
290
291 /* Check if this binary was compiled with the same acceleration as we
292  * would suggest for the current hardware. Always print stats to the log file
293  * if it is non-NULL, and if we don't have a match, print a warning in log
294  * (if non-NULL) and if print_to_stderr!=0 also to stderr.
295  */
296 int
297 gmx_cpuid_acceleration_check    (gmx_cpuid_t                cpuid,
298                                  FILE *                     log,
299                                  int                        print_to_stderr);
300
301
302 /* Release resources used by data structure. Note that the pointer to the
303  * CPU brand string will no longer be valid once this routine has been called.
304  */
305 void
306 gmx_cpuid_done              (gmx_cpuid_t                cpuid);
307
308
309
310
311 #ifdef __cplusplus
312 }
313 #endif
314
315
316 #endif /* GMX_CPUID_H_ */