OpenMM
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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  #else
50  #include <dlfcn.h>
51  #include <unistd.h>
52  #endif
53 #endif
54 
55 static int getNumProcessors() {
56 #ifdef __APPLE__
57  int ncpu;
58  size_t len = 4;
59  if (sysctlbyname("hw.logicalcpu", &ncpu, &len, NULL, 0) == 0)
60  return ncpu;
61  else
62  return 1;
63 #else
64 #ifdef WIN32
65  SYSTEM_INFO siSysInfo;
66  int ncpu;
67  GetSystemInfo(&siSysInfo);
68  ncpu = siSysInfo.dwNumberOfProcessors;
69  if (ncpu < 1)
70  ncpu = 1;
71  return ncpu;
72 #else
73  long nProcessorsOnline = sysconf(_SC_NPROCESSORS_ONLN);
74  if (nProcessorsOnline == -1)
75  return 1;
76  else
77  return (int) nProcessorsOnline;
78 #endif
79 #endif
80 }
81 
85 #ifdef _WIN32
86 #define cpuid __cpuid
87 #else
88 static void cpuid(int cpuInfo[4], int infoType){
89 #ifdef __LP64__
90  __asm__ __volatile__ (
91  "cpuid":
92  "=a" (cpuInfo[0]),
93  "=b" (cpuInfo[1]),
94  "=c" (cpuInfo[2]),
95  "=d" (cpuInfo[3]) :
96  "a" (infoType)
97  );
98 #else
99  __asm__ __volatile__ (
100  "pushl %%ebx\n"
101  "cpuid\n"
102  "movl %%ebx, %1\n"
103  "popl %%ebx\n" :
104  "=a" (cpuInfo[0]),
105  "=r" (cpuInfo[1]),
106  "=c" (cpuInfo[2]),
107  "=d" (cpuInfo[3]) :
108  "a" (infoType)
109  );
110 #endif
111 }
112 #endif
113 
114 #endif // OPENMM_HARDWARE_H_