-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_detect.cpp
154 lines (142 loc) · 3.01 KB
/
sys_detect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*********************************************
* Detects Hardware for Windows, Mac OS, UNIX, and NVidia accelerated platforms.
**********************************************/
#include "sys_detect.hpp"
namespace System
{
namespace Cpu
{
int64_t Memory = -1;
int64_t Cores = -1;
};
namespace Gpu
{
int HasCudaSupport = -1;
int64_t Memory = -1;
int64_t Cores = -1;
};
};
extern uint64_t GetCoreCount()
{
if(System::Cpu::Cores == -1)
{
int64_t Cores;
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
Cores = sysinfo.dwNumberOfProcessors;
#elif MACOS
int nm[2];
size_t len = 4;
uint32_t count;
nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1)
{
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1) { count = 1; }
}
Cores = count;
#else
Cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
System::Cpu::Cores = Cores;
return Cores;
}
else
return System::Cpu::Cores;
}
extern uint64_t GetAvailableMemory()
{
if(System::Cpu::Memory == -1)
{
int64_t Memory;
#ifdef WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
Memory = status.ullTotalPhys;
#elif MACOS
int mib [] = { CTL_HW, HW_MEMSIZE };
int64_t value = 0;
size_t length = sizeof(value);
if(-1 == sysctl(mib, 2, &value, &length, NULL, 0))
Memory = value;
#else
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
Memory = pages * page_size;
#endif
System::Cpu::Memory = Memory;
return Memory;
}
else
return System::Cpu::Memory;
}
#ifdef __CUDACC__
extern bool CheckCudaSupport()
{
if(System::Gpu::HasCudaSupport == -1)
{
int device_count;
int error = cudaGetDeviceCount(&device_count);
if(error == 0)
{
System::Gpu::HasCudaSupport = true;
return true;
}
else
{
System::Gpu::HasCudaSupport = false;
return false;
}
}
else
return System::Gpu::HasCudaSupport;
}
extern uint64_t GetCudaCoreCount()
{
if(System::Gpu::Cores == -1)
{
cudaDeviceProp devProp;
cudaGetDeviceProperties(&devProp, 0);
int cores = 0;
int mp = devProp.multiProcessorCount;
switch (devProp.major)
{
case 2: // Fermi
if (devProp.minor == 1) cores = mp * 48;
else cores = mp * 32;
break;
case 3: // Kepler
cores = mp * 192;
break;
case 5: // Maxwell
cores = mp * 128;
break;
case 6: // Pascal
if (devProp.minor == 1) cores = mp * 128;
else if (devProp.minor == 0) cores = mp * 64;
break;
default: break;
}
System::Gpu::Cores = cores;
return cores;
}
else
return System::Gpu::Cores;
}
extern uint64_t GetCudaMemory()
{
if(System::Gpu::Memory == -1)
{
cudaDeviceProp devProp;
cudaGetDeviceProperties(&devProp, 0);
System::Gpu::Memory = devProp.totalGlobalMem;
return System::Gpu::Memory;
}
else
return System::Gpu::Memory;
}
#endif