-
Notifications
You must be signed in to change notification settings - Fork 0
/
simd.cpp
153 lines (118 loc) · 4.2 KB
/
simd.cpp
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
#include <bench.hpp>
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
template<typename Q>
auto regular(const std::vector<Q> &A, const std::vector<Q> &B) -> std::vector<Q> {
std::vector<Q> C( std::min(A.size(), B.size()), {} );
for(auto i = 0u; i < C.size(); ++i)
C[i] = A[i] * B[i];
return C;
}
template<typename Q>
auto simd(const std::vector<Q> &A, const std::vector<Q> &B) -> std::vector<Q> {
std::vector<Q> C( std::min(A.size(), B.size()), {} );
#pragma omp simd
for(auto i = 0u; i < C.size(); ++i)
C[i] = A[i] * B[i];
return C;
}
template<typename Q>
auto parallel(const std::vector<Q> &A, const std::vector<Q> &B) -> std::vector<Q> {
std::vector<Q> C( std::min(A.size(), B.size()), {} );
#pragma omp parallel
for(auto i = 0u; i < C.size(); ++i)
C[i] = A[i] * B[i];
return C;
}
template<typename Q>
auto parallel_simd(const std::vector<Q> &A, const std::vector<Q> &B) -> std::vector<Q> {
std::vector<Q> C( std::min(A.size(), B.size()), {} );
#pragma omp parallel
#pragma omp simd
for(auto i = 0u; i < C.size(); ++i)
C[i] = A[i] * B[i];
return C;
}
template<typename Q>
auto simd_parallel(const std::vector<Q> &A, const std::vector<Q> &B) -> std::vector<Q> {
std::vector<Q> C( std::min(A.size(), B.size()), {} );
#pragma omp parallel for simd
for(auto i = 0u; i < C.size(); ++i)
C[i] = A[i] * B[i];
return C;
}
struct {
// Normally I would read random data
// from /dev/random (or maybe /urandom).
// This also makes implementation simpler.
//
// However, in order to do this in compile-
// time, we have to do a more primitive
// approach.
/*__TIME__ = hh:mm:ss*/
#define time_as_number() (((((__TIME__[0] * 10 + __TIME__[1]) * 10 + __TIME__[3]) * 10 + __TIME__[4]) * 10 + __TIME__[6]) * 10 + __TIME__[7])
unsigned int seed { time_as_number() };
constexpr unsigned next(void) {
// Simple x^2 + 1 PRG
unsigned x = seed * seed + 1;
seed = x;
return x;
}
/* Reads 'size' random bytes into x. */
constexpr void read_into(void *x, std::size_t size) {
unsigned *px = static_cast<unsigned *>(x);
constexpr auto block = sizeof(unsigned);
for(auto i = 0u; i < size / block; ++i)
px[i] = next();
if (auto missing = size % block; missing != 0) {
char *ppx = static_cast<char *>(x);
for(auto i = 0u; i < missing; ++i)
ppx[size / block + i] = next(); // here we slice some data
}
}
} random_channel;
/*
* Generates 'size' random values of Q.
*
* Note: will probably assign garbage values
* for nontrivial Q. The random generation
* is done using 'random_channel', which assigns
* a random bitstring of size 'sizeof(Q)' to each
* item in the returned vector.
* */
template<typename Q>
constexpr auto gen_random_vec(const std::size_t size) -> std::vector<Q> {
constexpr const auto bytes = sizeof(Q);
std::vector<Q> result( size, {} );
std::for_each(result.begin(), result.end(), [](auto &x) {
static_assert(bytes == sizeof(Q));
random_channel.read_into(&x, bytes);
});
return result;
}
int main(void) {
constexpr auto trials = 10u;
constexpr auto vecsize = 125'000'000; // 1.25*10^8, which is 500MB of floats
using Q = float;
/* constexpr */ std::vector<Q>
A{gen_random_vec<Q>(vecsize)},
B{gen_random_vec<Q>(vecsize)};
// Unfortunately, I learnt (the hard way)
// that compilers place a limit on constexpr
// loop size, I do not wish to change it,
// for the sake of my computer's integrity :)
std::cout << "Created random data.\n"
<< "--------------------\n\n";
std::cout << "regular:\n"
<< bench(trials, regular<Q>, A, B) << '\n';
std::cout << "parallel:\n"
<< bench(trials, parallel<Q>, A, B) << '\n';
std::cout << "simd:\n"
<< bench(trials, simd<Q>, A, B) << '\n';
std::cout << "parallel_simd:\n"
<< bench(trials, parallel_simd<Q>, A, B) << '\n';
std::cout << "simd_parallel:\n"
<< bench(trials, simd_parallel<Q>, A, B) << '\n';
}