Newer
Older
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <thread>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <mutex>
#include <set>
#include <fstream>
extern "C"
{
#include <sched.h>
#include <unistd.h>
}
const long n = 42l * 1024 * 1024 * 100;
const double h = 1.0 / (double)n;
std::mutex used_cpu_ids_mtx;
std::set<int> used_cpu_ids;
// A mutex and macro to sync the output to stdout
std::mutex iomtx;
#define IO_SYNC(X) \
{ \
std::lock_guard<std::mutex> iolock(iomtx); \
X \
}
// Get the current timestamp in micro seconds
uint64_t now_micro()
{
return std::chrono::time_point_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now()
).time_since_epoch().count();
}
// Print stats about the calling thread, including the currently executing CPU Core ID
void print_thread_stats()
{
// Get the CPU Core ID on which this thread is currently being executed
auto cpu = sched_getcpu();
{
std::lock_guard<std::mutex> lock(used_cpu_ids_mtx);
used_cpu_ids.insert(cpu);
}
std::cout
// Process ID of this process
<< "pid = "
<< getpid()
// Thread ID of this thread
<< ", thread_id = "
<< std::this_thread::get_id()
// CPU Core ID on which this is currently being executed
<< ", cpu_id = "
<< cpu;
}
// In Linux CPUs with SMT/Hyperthreading are counted twice in the cpu list. So for example on a
// machine with SMT and 4 cores, the cpu list might look like this:
// 0, 1, 2, 3, 4, 5, 6, 7
// While there are 8 cpus in the list, only 4 cores are actually physically available. So there are
// pairs of cpus (so called siblings) that are actually the same physical core, but split up into
// 2 virtual cores. Those pairs could be for example 0,4 1,5 2,6 3,7 .
// This function returns the sibling pair for a given cpu core as a string
std::string cpu_siblings(int cpu_id)
{
std::stringstream filename;
filename
<< "/sys/devices/system/cpu/cpu"
<< cpu_id
<< "/topology/thread_siblings_list";
std::ifstream file(filename.str());
std::string siblings;
file >> siblings;
return siblings;
}
void pi_thread(int thread_num, int numThreads, double *partial_pi)
{
IO_SYNC(
print_thread_stats();
std::cout << '\n';
);
auto tstart = now_micro();
double sum = 0.0;
for (long i = thread_num + 1; i <= n; i += numThreads)
{
double x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x * x);
}
*partial_pi = h * sum;
auto elapsed = now_micro() - tstart;
IO_SYNC(
print_thread_stats();
std::cout
// The time spent calculating on this specific thread
<< ", thread_calc_time = "
<< (elapsed / 1000)
<< " ms"
<< '\n';
);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <number-of-threads>" << std::endl;
return -1;
}
int numThreads = std::stoi(argv[1]);
char hostname[256];
gethostname(hostname, 256);
auto hwc = std::thread::hardware_concurrency();
std::cout << "Running on node: " << hostname << '\n';
std::cout << "CPP detected hardware concurrency: " << hwc << '\n';
std::cout << "Main thread: ";
print_thread_stats();
std::cout << "\n--------------------\n";
auto tstart = now_micro();
std::thread threads[numThreads];
double partials[numThreads];
for (int thread_num = 0; thread_num < numThreads; thread_num++)
{
threads[thread_num] = std::thread(
pi_thread, thread_num, numThreads, &(partials[thread_num])
);
}
double pi = 0;
for (int i = 0; i < numThreads; ++i)
{
threads[i].join();
pi += partials[i];
}
auto elapsed_ms = (now_micro() - tstart) / 1000;
std::cout << "--------------------\n";
std::cout << std::setprecision(16) << "Error is " << std::fabs(pi - M_PI) << '\n';
std::cout << "Calculation took " << elapsed_ms << " ms\n";
std::cout << "Num Threads = " << numThreads << '\n';
// Print what cores have been utilized over the runtime of the program. Since those are only
// sampled at specific point, it is theoretically possible that cores are missing
std::cout << "Utilized CPU ids: \n";
for (auto cpu : used_cpu_ids)
{
// For each core, also print the siblings
std::cout << " " << cpu << ", siblings: " << cpu_siblings(cpu) << '\n';
}
return 0;
}