14# if (!defined(FREE_WINDOWS))
18#elif defined(__APPLE__)
20# include <sys/ioctl.h>
21# include <sys/resource.h>
22# include <sys/sysctl.h>
23# include <sys/types.h>
27# include <sys/ioctl.h>
28# include <sys/resource.h>
39 CONSOLE_SCREEN_BUFFER_INFO csbi;
40 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
41 columns = csbi.dwSize.X;
45 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &
w) == 0) {
50 return (columns > 0) ? columns : 80;
54#if (!defined(_WIN32) || defined(FREE_WINDOWS)) && (defined(__x86_64__) || defined(__i386__))
55static void __cpuid(
int data[4],
int selector)
57# if defined(__x86_64__)
58 asm(
"cpuid" :
"=a"(
data[0]),
"=b"(
data[1]),
"=c"(
data[2]),
"=d"(
data[3]) :
"a"(selector));
59# elif defined(__i386__)
60 asm(
"pushl %%ebx \n\t"
77 char modelname[512] =
"";
78 size_t bufferlen = 512;
79 if (sysctlbyname(
"machdep.cpu.brand_string", &modelname, &bufferlen,
nullptr, 0) == 0) {
82#elif (defined(WIN32) || defined(__x86_64__) || defined(__i386__)) && !defined(_M_ARM64)
87 __cpuid(result, 0x80000000);
89 if (result[0] != 0 && result[0] >= (
int)0x80000004) {
90 __cpuid((
int *)(buf + 0), 0x80000002);
91 __cpuid((
int *)(buf + 16), 0x80000003);
92 __cpuid((
int *)(buf + 32), 0x80000004);
102#elif defined(_M_ARM64)
103 DWORD processorNameStringLength = 255;
104 char processorNameString[255];
105 if (RegGetValueA(HKEY_LOCAL_MACHINE,
106 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
107 "ProcessorNameString",
110 &processorNameString,
111 &processorNameStringLength) == ERROR_SUCCESS)
113 return processorNameString;
117 FILE *cpuinfo = fopen(
"/proc/cpuinfo",
"r");
118 if (cpuinfo !=
nullptr) {
119 char cpuinfo_buf[513] =
"";
120 fread(cpuinfo_buf,
sizeof(cpuinfo_buf) - 1, 1, cpuinfo);
123 char *modelname = strstr(cpuinfo_buf,
"model name");
124 if (modelname !=
nullptr) {
125 modelname = strchr(modelname,
':');
126 if (modelname !=
nullptr) {
128 char *modelname_end = strchr(modelname,
'\n');
129 if (modelname_end !=
nullptr) {
130 *modelname_end =
'\0';
137 return "Unknown CPU";
142 return (
sizeof(
void *) * 8);
145#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
147struct CPUCapabilities {
153static CPUCapabilities system_cpu_capabilities_init()
155 CPUCapabilities caps = {};
163 __cpuid(result, 0x00000001);
164 const bool sse = (
result[3] & ((int)1 << 25)) != 0;
165 const bool sse2 = (
result[3] & ((int)1 << 26)) != 0;
166 const bool sse3 = (
result[2] & ((int)1 << 0)) != 0;
168 const bool ssse3 = (
result[2] & ((int)1 << 9)) != 0;
169 const bool sse41 = (
result[2] & ((int)1 << 19)) != 0;
170 const bool sse42 = (
result[2] & ((int)1 << 20)) != 0;
172 const bool fma3 = (
result[2] & ((int)1 << 12)) != 0;
173 const bool os_uses_xsave_xrestore = (
result[2] & ((int)1 << 27)) != 0;
174 const bool cpu_avx_support = (
result[2] & ((int)1 << 28)) != 0;
177 caps.sse42 = sse && sse2 && sse3 && ssse3 && sse41 && sse42;
179 if (os_uses_xsave_xrestore && cpu_avx_support) {
182# if defined(__GNUC__)
185 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a"(xcr_feature_mask),
"=d"(edx) :
"c"(0));
186# elif defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
188 xcr_feature_mask = (
uint32_t)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
190 xcr_feature_mask = 0;
192 const bool avx = (xcr_feature_mask & 0x6) == 0x6;
193 const bool f16c = (
result[2] & ((int)1 << 29)) != 0;
195 caps.f16c = avx && f16c;
197 __cpuid(result, 0x00000007);
198 bool bmi1 = (
result[1] & ((int)1 << 3)) != 0;
199 bool bmi2 = (
result[1] & ((int)1 << 8)) != 0;
200 bool avx2 = (
result[1] & ((int)1 << 5)) != 0;
202 caps.avx2 = sse && sse2 && sse3 && ssse3 && sse41 && sse42 && avx && f16c && avx2 && fma3 &&
210static const CPUCapabilities &system_cpu_capabilities()
212 static const CPUCapabilities caps = system_cpu_capabilities_init();
218 const CPUCapabilities &caps = system_cpu_capabilities();
229 const CPUCapabilities &caps = system_cpu_capabilities();
230 return caps.avx2 && caps.f16c;
251 ram.dwLength =
sizeof(ram);
252 GlobalMemoryStatusEx(&ram);
253 return ram.ullTotalPhys;
254#elif defined(__APPLE__)
256 size_t len =
sizeof(ram);
257 if (sysctlbyname(
"hw.memsize", &ram, &
len,
nullptr, 0) == 0) {
262 size_t ps = sysconf(_SC_PAGESIZE);
263 size_t pn = sysconf(_SC_PHYS_PAGES);
271 return GetCurrentProcessId();
280 return _getmaxstdio();
282 struct rlimit limit = {};
283 return (getrlimit(RLIMIT_NOFILE, &limit) == 0) ? limit.rlim_cur :
SIZE_MAX;
290 constexpr int max_open_files = 8192;
294 if (_getmaxstdio() < max_open_files) {
295 ok = _setmaxstdio(max_open_files) == max_open_files;
298 struct rlimit limit = {};
299 ok = getrlimit(RLIMIT_NOFILE, &limit) == 0;
300 if (ok && limit.rlim_cur < rlim_t(max_open_files)) {
301 limit.rlim_cur = std::min(rlim_t(max_open_files), limit.rlim_max);
302 ok = setrlimit(RLIMIT_NOFILE, &limit) == 0;
307 LOG_DEBUG <<
"Failed to ensure max open files is at least " << max_open_files <<
": "
unsigned long long int uint64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
#define CCL_NAMESPACE_END
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const size_t num
string string_remove_trademark(const string &s)
string string_remove_gpu_from_cpu_name(const string &s)
size_t system_physical_ram()
bool system_cpu_support_avx2()
void system_max_open_files_ensure()
size_t system_max_open_files()
CCL_NAMESPACE_BEGIN int system_console_width()
bool system_cpu_support_sse42()
string system_cpu_brand_string()
uint64_t system_self_process_id()