-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.hpp
86 lines (75 loc) · 2.45 KB
/
cpu.hpp
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
#pragma once
#ifndef __HPC_CPU_HPP__
#define __HPC_CPU_HPP__
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <bitset>
#include <sstream>
#include <iostream>
#include <thread>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
namespace hpc {
const int MAX_THREADS = 1000;
#ifdef SYS_gettid
#define gettid() ((pid_t)syscall(SYS_gettid))
// pid_t gettid() {
// return syscall(SYS_gettid);
// }
#else
#error "SYS_gettid unavailable on this system"
#endif
// http://www.cplusplus.com/forum/unices/6544/
int get_hardware_sockets() {
std::string cmd = "lscpu | grep -e 'Socket(s)' | awk -F ':' '{print $2}' | awk '$1=$1'";
FILE * fp = popen(cmd.c_str(),"r");
char sockets[8];
while (fgets(sockets, sizeof(sockets), fp) != NULL) {};
fclose(fp);
return std::stoi(sockets);
}
std::size_t get_hardware_concurrency() {
return std::thread::hardware_concurrency();
}
// https://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
// https://stackoverflow.com/questions/18916254/get-the-current-cpuset
// https://stackoverflow.com/questions/280909/how-to-set-cpu-affinity-for-a-process-from-c-or-c-in-linux
std::vector<int> get_thread_affinity(bool verboe=false) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
std::vector<int> cores;
if (0 == sched_getaffinity(getpid(), sizeof(cpu_set_t), &cpuset)) {
const int nCores = sysconf( _SC_NPROCESSORS_ONLN );
if (verboe) {
std::cout << "affinity cores: ";
}
for (int i = 0; i < nCores; i++) {
if (CPU_ISSET(i, &cpuset)) {
cores.emplace_back(i);
if (verboe) {
std::cout << i << ",";
}
}
}
if (verboe) {
std::cout << std::endl;
}
} else {
std::cerr << "sched_getaffinity() failed: " << strerror(errno) << std::endl;
}
return cores;
}
int set_thread_affinity(std::vector<int> &cores) {
cpu_set_t mask;
CPU_ZERO(&mask);
for (auto core : cores) {
CPU_SET(core, &mask);
}
return sched_setaffinity(0, sizeof(mask), &mask);
}
}
#endif // __HPC_CPU_HPP__