-
Notifications
You must be signed in to change notification settings - Fork 3
/
hardware.c
91 lines (77 loc) · 1.96 KB
/
hardware.c
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
#define _GNU_SOURCE // for sched_getaffinity
#include "assert.h"
#include "cpuid.h"
#include "glob.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/stat.h"
#include "unistd.h"
#include "omp.h"
#include "sched.h"
#include "hardware.h"
const uint64_t HARDWARE_HAS_SSE = 0x00;
const uint64_t HARDWARE_HAS_AVX2 = 0x01;
const uint64_t HARDWARE_HAS_AVX512 = 0x02;
uint64_t hardware_is_needlessly_disabled() {
int a = 0, b = 0, c = 0, d = 0;
while(a == 0) {
__cpuid(0x01, a, b, c, d);
}
const int family = a >> 8 & (int)0x0F;
const int model = (a >> 4 & (int)0x0F) | (a >> 12) & (int)0xF0;
const int stepping = a & 0x0F;
return (family == 6 && model == 151);
}
uint64_t hardware_instruction_set() {
int a = 0, b = 0, c = 0, d = 0;
while(b == 0) {
__cpuid(0x07, a, b, c, d);
}
if(b & bit_AVX512F && b & bit_AVX512BW) {
return HARDWARE_HAS_AVX512;
} else if((b & bit_AVX2)) {
return HARDWARE_HAS_AVX2;
} else {
return HARDWARE_HAS_SSE;
exit(-1);
}
}
uint64_t hardware_ram_speed() {
glob_t dmiglob;
uint16_t ram_speed;
assert(!glob("/sys/firmware/dmi/entries/17-*/raw", 0, NULL, &dmiglob));
for(size_t i = 0; i < dmiglob.gl_pathc; i++) {
FILE* file = fopen(dmiglob.gl_pathv[i], "r");
if(file == NULL) {
return 0;
}
struct stat size;
fseek(file, 0x15, SEEK_SET);
while(!fread((void*) &ram_speed, sizeof(uint16_t), 1, file));
fclose(file);
if(ram_speed) {
break;
}
}
return (uint64_t) ram_speed;
}
uint64_t hardware_cpu_count() {
cpu_set_t cpuset;
size_t omp_thread_count;
sched_getaffinity((pid_t)getpid(), sizeof(cpu_set_t), &cpuset);
#ifndef _OPENMP
return CPU_COUNT(&cpuset);
#endif
#pragma omp parallel
{
#pragma omp single
omp_thread_count = omp_get_num_threads();
}
if(CPU_COUNT(&cpuset) < omp_thread_count){
omp_set_num_threads(CPU_COUNT(&cpuset));
return CPU_COUNT(&cpuset);
} else {
return omp_thread_count;
}
}