-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_benchmark.cpp
More file actions
138 lines (111 loc) · 4.96 KB
/
cpu_benchmark.cpp
File metadata and controls
138 lines (111 loc) · 4.96 KB
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
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
#include <cmath>
#include <iomanip>
// Computationally intensive workload per thread
void cpu_workload(int thread_id, std::atomic<uint64_t>& total_operations, int duration_seconds) {
uint64_t operations = 0;
double result = 1.0;
auto start = std::chrono::steady_clock::now();
auto end_time = start + std::chrono::seconds(duration_seconds);
while (std::chrono::steady_clock::now() < end_time) {
// Mix of floating-point operations
for (int i = 0; i < 10000; ++i) {
result = std::sin(result) * std::cos(result) + std::sqrt(std::abs(result) + 1.0);
result = std::log(std::abs(result) + 1.0) * std::exp(result * 0.0001);
result = std::pow(std::abs(result) + 0.1, 0.5) + std::tan(result * 0.001);
operations += 3;
}
// Prevent optimization
if (result == 0.0) {
std::cout << "Unexpected zero" << std::endl;
}
}
total_operations += operations;
}
// Integer-focused workload
void integer_workload(int thread_id, std::atomic<uint64_t>& total_operations, int duration_seconds) {
uint64_t operations = 0;
uint64_t value = 12345678901234567ULL;
auto start = std::chrono::steady_clock::now();
auto end_time = start + std::chrono::seconds(duration_seconds);
while (std::chrono::steady_clock::now() < end_time) {
for (int i = 0; i < 100000; ++i) {
value = (value * 6364136223846793005ULL + 1442695040888963407ULL);
value ^= (value >> 33);
value *= 0xff51afd7ed558ccdULL;
value ^= (value >> 33);
operations += 4;
}
}
total_operations += operations;
// Prevent optimization
if (value == 0) {
std::cout << "Unexpected zero" << std::endl;
}
}
void run_benchmark(const std::string& name,
void (*workload)(int, std::atomic<uint64_t>&, int),
unsigned int num_threads,
int duration_seconds) {
std::atomic<uint64_t> total_operations(0);
std::vector<std::thread> threads;
std::cout << "\n" << name << " Benchmark" << std::endl;
std::cout << std::string(50, '-') << std::endl;
std::cout << "Threads: " << num_threads << std::endl;
std::cout << "Duration: " << duration_seconds << " seconds" << std::endl;
std::cout << "Running..." << std::flush;
auto start = std::chrono::high_resolution_clock::now();
// Launch threads
for (unsigned int i = 0; i < num_threads; ++i) {
threads.emplace_back(workload, i, std::ref(total_operations), duration_seconds);
}
// Wait for all threads to complete
for (auto& t : threads) {
t.join();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration<double>(end - start).count();
double ops_per_second = total_operations / elapsed;
double mops = ops_per_second / 1000000.0;
std::cout << " Done!" << std::endl;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Total operations: " << total_operations << std::endl;
std::cout << "Time elapsed: " << elapsed << " seconds" << std::endl;
std::cout << "Performance: " << mops << " MOPS (Million Operations/Second)" << std::endl;
std::cout << "Per-thread: " << (mops / num_threads) << " MOPS" << std::endl;
}
int main(int argc, char* argv[]) {
unsigned int num_cores = std::thread::hardware_concurrency();
int duration = 5; // Default duration in seconds
if (num_cores == 0) {
num_cores = 4; // Fallback
std::cout << "Warning: Could not detect CPU cores, using 4" << std::endl;
}
// Parse command line arguments
if (argc > 1) {
duration = std::atoi(argv[1]);
if (duration <= 0) duration = 5;
}
std::cout << "========================================" << std::endl;
std::cout << " CPU Benchmark Tool" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "Detected CPU cores: " << num_cores << std::endl;
std::cout << "Benchmark duration: " << duration << " seconds per test" << std::endl;
// Run floating-point benchmark with all cores
run_benchmark("Floating-Point (All Cores)", cpu_workload, num_cores, duration);
// Run integer benchmark with all cores
run_benchmark("Integer (All Cores)", integer_workload, num_cores, duration);
// Single-threaded baseline for comparison
run_benchmark("Floating-Point (Single Thread)", cpu_workload, 1, duration);
run_benchmark("Integer (Single Thread)", integer_workload, 1, duration);
// Calculate and display scaling efficiency
std::cout << "\n========================================" << std::endl;
std::cout << " Benchmark Complete" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "\nUsage: " << argv[0] << " [duration_seconds]" << std::endl;
return 0;
}