OpenMM
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
hardware.h
1 #ifndef OPENMM_HARDWARE_H_
2 #define OPENMM_HARDWARE_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * OpenMM *
6  * -------------------------------------------------------------------------- *
7  * This is part of the OpenMM molecular simulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org. *
11  * *
12  * Portions copyright (c) 2013 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: *
15  * *
16  * Permission is hereby granted, free of charge, to any person obtaining a *
17  * copy of this software and associated documentation files (the "Software"), *
18  * to deal in the Software without restriction, including without limitation *
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
20  * and/or sell copies of the Software, and to permit persons to whom the *
21  * Software is furnished to do so, subject to the following conditions: *
22  * *
23  * The above copyright notice and this permission notice shall be included in *
24  * all copies or substantial portions of the Software. *
25  * *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
29  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
32  * USE OR OTHER DEALINGS IN THE SOFTWARE. *
33  * -------------------------------------------------------------------------- */
34 
42 #ifdef __APPLE__
43  #include <sys/sysctl.h>
44  #include <dlfcn.h>
45 #else
46  #ifdef WIN32
47  #define NOMINMAX
48  #include <windows.h>
49  #include <intrin.h>
50  #else
51  #ifdef __ANDROID__
52  #include <cpu-features.h>
53  #else
54  #include <unistd.h>
55  #endif
56  #endif
57 #endif
58 
59 static int getNumProcessors() {
60 #ifdef __APPLE__
61  int ncpu;
62  size_t len = 4;
63  if (sysctlbyname("hw.logicalcpu", &ncpu, &len, NULL, 0) == 0)
64  return ncpu;
65  else
66  return 1;
67 #else
68 #ifdef WIN32
69  SYSTEM_INFO siSysInfo;
70  int ncpu;
71  GetSystemInfo(&siSysInfo);
72  ncpu = siSysInfo.dwNumberOfProcessors;
73  if (ncpu < 1)
74  ncpu = 1;
75  return ncpu;
76 #else
77  #ifdef __ANDROID__
78  return android_getCpuCount();
79  #else
80  long nProcessorsOnline = sysconf(_SC_NPROCESSORS_ONLN);
81  if (nProcessorsOnline == -1)
82  return 1;
83  else
84  return (int) nProcessorsOnline;
85  #endif
86 #endif
87 #endif
88 }
89 
93 #ifdef WIN32
94 #define cpuid __cpuid
95 #else
96 #if !defined(__ANDROID__) && !defined(__PNACL__)
97  static void cpuid(int cpuInfo[4], int infoType){
98  #ifdef __LP64__
99  __asm__ __volatile__ (
100  "cpuid":
101  "=a" (cpuInfo[0]),
102  "=b" (cpuInfo[1]),
103  "=c" (cpuInfo[2]),
104  "=d" (cpuInfo[3]) :
105  "a" (infoType)
106  );
107  #else
108  __asm__ __volatile__ (
109  "pushl %%ebx\n"
110  "cpuid\n"
111  "movl %%ebx, %1\n"
112  "popl %%ebx\n" :
113  "=a" (cpuInfo[0]),
114  "=r" (cpuInfo[1]),
115  "=c" (cpuInfo[2]),
116  "=d" (cpuInfo[3]) :
117  "a" (infoType)
118  );
119  #endif
120  }
121  #endif
122 #endif
123 
124 #endif // OPENMM_HARDWARE_H_