-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.cpp
More file actions
91 lines (79 loc) · 2.74 KB
/
bench.cpp
File metadata and controls
91 lines (79 loc) · 2.74 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
#include <benchmark/benchmark.h>
#include <vector>
#include <experimental/simd>
#include <algorithm>
#include <numeric>
#include <memory_resource>
static void pmr_simd(benchmark::State& state) {
using Type = std::experimental::parallelism_v2::fixed_size_simd<double, 2>;
int n = state.range(0);
std::pmr::monotonic_buffer_resource pool1(sizeof(double)* state.range(0));
std::pmr::monotonic_buffer_resource pool2(sizeof(double)* state.range(0));
std::pmr::vector<double> a(&pool1);
std::pmr::vector<double> b(&pool2);
a.assign(n, 1);
b.assign(n, 1);
for (auto _ : state) {
Type sum = 0.0;
for (int i = 0; i < state.range(0);i += 2) {
Type va(&a[i], std::experimental::parallelism_v2::element_aligned);
Type vb(&b[i], std::experimental::parallelism_v2::element_aligned);
sum += va * vb;
}
double res = std::experimental::parallelism_v2::reduce(sum);
}
}
// g++-15 -std=c++20 -O3 -march=native -Ibenchmark/include -Lbenchmark/build/src -lbenchmark -lpthread bench.cpp -o my_bench
static void BM_simd(benchmark::State& state) {
for (auto _ : state) {
std::experimental::fixed_size_simd<double, 2> a;
a = 1;
for (int i = 0; i < state.range(0); ++i) {
double f = std::experimental::parallelism_v2::reduce(a, std::plus<>());
}
}
}
static void innerproduct(benchmark::State& state) {
using Type = std::experimental::parallelism_v2::fixed_size_simd<double, 2>;
std::size_t n = state.range(0);
std::vector<double> a;
std::vector<double> b;
a.assign(n, 1);
b.assign(n, 1);
for (auto _ : state) {
Type sum = 0.0;
for (int i = 0; i < n; i+= 2) {
Type va(&a[i], std::experimental::parallelism_v2::element_aligned);
Type vb(&b[i], std::experimental::parallelism_v2::element_aligned);
sum += va * vb;
}
double res = std::experimental::parallelism_v2::reduce(sum);
}
}
static void innerproduct_reg(benchmark::State& state) {
std::size_t n = state.range(0);
std::vector<double> a;
std::vector<double> b;
a.assign(n, 1);
b.assign(n, 1);
for (auto _ : state) {
double sum = 0.0;
for (int i = 0; i < n; i++) {
sum += a[i] * b[i];
}
}
}
static void BM_Vec(benchmark::State& state) {
std::vector<double> a;
a.assign(10, 1);
std::experimental::parallelism_v2::__m128d x = {1.0, 2.0};
for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) {
double _ = std::reduce(a.begin(), a.end(), 0.f);
}
}
}
BENCHMARK(innerproduct)->Arg(1000000);
BENCHMARK(innerproduct_reg)->Arg(1000000);
BENCHMARK(pmr_simd)->Arg(1000000);
BENCHMARK_MAIN();