-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.hpp
495 lines (424 loc) · 17.4 KB
/
Matrix.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <algorithm>
#include <iostream>
#include <cstddef>
#include "test_macro.hpp"
#include "utils.hpp"
#if HAS_SSE
#include <immintrin.h>
#endif
#if HAS_AVX512
#define SSE_ALIGN 64
#elif HAS_AVX
#define SSE_ALIGN 32
#elif HAS_SSE4_1 // Essayer d'autoriser le SSE avec des versions antérieures
#define SSE_ALIGN 16
#else
#define SSE_ALIGN 0
#endif
#define ALIGN_FOR(T) SSE_ALIGN < alignof(T) ? alignof(T) : SSE_ALIGN
// Public interface
template<typename FP, size_t ROW, size_t COL>
class Matrix {
static_assert(ROW * COL > 0, "Dimensions of the matrix must be strictly positive.");
public:
Matrix() = default;
Matrix(std::initializer_list<FP>);
// accessors
constexpr size_t width() const noexcept;
constexpr size_t height() const noexcept;
FP const& operator()(size_t i, size_t j) const noexcept;
FP& operator()(size_t i, size_t j) noexcept;
FP const* data() const noexcept;
FP* data() noexcept;
FP const* begin() const noexcept;
FP* begin() noexcept;
FP const* end() const noexcept;
FP* end() noexcept;
private:
alignas(ALIGN_FOR(FP)) float d[ROW*COL] = {};
};
template<typename FP, size_t ROW, size_t COL1, size_t COL2>
Matrix<FP, ROW, COL2> operator*(Matrix<FP, ROW, COL1> const& lhs, Matrix<FP, COL1, COL2> const& rhs);
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> operator+(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs);
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> operator-(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs);
template<typename FP, size_t ROW, size_t COL>
std::ostream& operator<<(std::ostream& o, Matrix<FP, ROW, COL> const& m);
// Implementation details
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL>::Matrix(std::initializer_list<FP> init)
{
std::copy_n(init.begin(), std::min(init.size(), ROW*COL), d);
}
template<typename FP, size_t ROW, size_t COL>
constexpr size_t Matrix<FP, ROW, COL>::width() const noexcept
{
return COL;
}
template<typename FP, size_t ROW, size_t COL>
constexpr size_t Matrix<FP, ROW, COL>::height() const noexcept
{
return ROW;
}
template<typename FP, size_t ROW, size_t COL>
FP const& Matrix<FP, ROW, COL>::operator()(size_t i, size_t j) const noexcept
{
return d[i*width() + j];
}
template<typename FP, size_t ROW, size_t COL>
FP& Matrix<FP, ROW, COL>::operator()(size_t i, size_t j) noexcept
{
return d[i*width() + j];
}
template<typename FP, size_t ROW, size_t COL>
FP const* Matrix<FP, ROW, COL>::data() const noexcept
{
return d;
}
template<typename FP, size_t ROW, size_t COL>
FP* Matrix<FP, ROW, COL>::data() noexcept
{
return d;
}
template<typename FP, size_t ROW, size_t COL>
FP const* Matrix<FP, ROW, COL>::begin() const noexcept
{
return data();
}
template<typename FP, size_t ROW, size_t COL>
FP* Matrix<FP, ROW, COL>::begin() noexcept
{
return data();
}
template<typename FP, size_t ROW, size_t COL>
FP const* Matrix<FP, ROW, COL>::end() const noexcept
{
return &d[ROW * COL];
}
template<typename FP, size_t ROW, size_t COL>
FP* Matrix<FP, ROW, COL>::end() noexcept
{
return &d[ROW * COL];
}
template <typename FP, size_t ROW, size_t COL>
Matrix<FP, COL, ROW> transpose(const Matrix<FP, ROW, COL>& m){
Matrix<FP, COL, ROW> output;
for (size_t i = 0; i < m.height(); ++i)
for (size_t j = 0; j < m.width(); ++j){
output(j, i) = m(i, j);
}
return output;
}
namespace {
namespace detail {
template<typename FP, size_t ROW, size_t COL1, size_t COL2>
struct multiply_helper
{
using return_type = Matrix<FP, ROW, COL2>;
using op1_type = Matrix<FP, ROW, COL1>;
using op2_type = Matrix<FP, COL1, COL2>;
// Fallback implementation
static return_type multiply(op1_type const& a, op2_type const& b)
{
return_type output;
for (size_t i = 0; i < ROW; ++i) {
for (size_t j = 0; j < COL2; ++j) {
for (size_t k = 0; k < COL1; ++k) {
output(i, j) += a(i, k) * b(k, j);
}
}
}
return output;
}
};
#if HAS_SSE4_1
template<size_t ROW, size_t COL2>
struct multiply_helper<float, ROW, 4, COL2>
{
using return_type = Matrix<float, ROW, COL2>;
using op1_type = Matrix<float, ROW, 4>;
using op2_type = Matrix<float, 4, COL2>;
// Implementation using SSE
static return_type multiply(op1_type const& a, op2_type const& b)
{
auto b_transposed = transpose(b);
return_type result;
for(size_t line = 0; line < a.height(); ++line)
{
__m128 cur_line = _mm_load_ps(a.data() + line*a.width());
size_t done_cols = 0;
while(done_cols < b.width())
{
//auto count = ctz((uintptr_t)(void*)(b_transposed.data()+done_cols*b.height()));
//size_t current_alignment = (1ULL << (count-4));
// We mesure the alignment of memory by counting the trailing zero bits
// We will use the biggest simd instructions that are available and applicabale
// Meaning we use them if there still enough data to fill the simd registers
// And the memory is correctly aligned
// After reflexion, it would seem that this useless, the only way for the pointer
// To not be aligned for the maximum size simd is after executing a smaller simd iteration
// Meaning we are at the end of the loop, after that the pointer is reset to the first element
// which is correctly aligned
switch(b.width()-done_cols)
{
case 0:
case 1:
#if !HAS_AVX
default:
#endif
{
// SSE 128bits
__m128 col = _mm_load_ps(b_transposed.data() + done_cols*b.height());
__m128 r = _mm_dp_ps(cur_line, col, 0xF1);
result(line, done_cols) = _mm_cvtss_f32(r);
++done_cols;
break;
}
#if HAS_AVX
case 2:
case 3:
#if !HAS_AVX512
default:
#endif
{
// SSE 256bits
__m256 line_doubled = _mm256_castps128_ps256(cur_line);
line_doubled = _mm256_insertf128_ps(line_doubled, cur_line, 1);
__m256 cols = _mm256_load_ps(b_transposed.data() + done_cols*b.height());
__m256 r = _mm256_mul_ps(line_doubled, cols);
r = _mm256_hadd_ps(r, r);
r = _mm256_hadd_ps(r, r);
result(line, done_cols) = _mm256_cvtss_f32(r);
r = _mm256_permute2f128_ps(r, r, 1);
result(line, done_cols+1) = _mm256_cvtss_f32(r);
done_cols += 2;
break;
}
#if HAS_AVX512
default: // 4 and over
{
// SSE 512bits
__m256 line_doubled = _mm256_castps128_ps256(cur_line);
line_doubled = _mm256_insertf128_ps(line_doubled, cur_line, 1);
__m512 line_quad = _mm512_castps256_ps512(line_doubled);
line_quad = _mm512_insertf32x8(line_quad, line_doubled, 1);
__m512 cols = _mm512_load_ps(b_transposed.data() + done_cols*b.height());
__m512 r = _mm512_mul_ps(line_quad, cols);
__m256 lo = _mm512_castps512_ps256(r);
__m256 hi = _mm256_castpd_ps(_mm512_extractf64x4_pd(_mm512_castps_pd(r),1));
lo = _mm256_hadd_ps(lo, lo);
lo = _mm256_hadd_ps(lo, lo);
hi = _mm256_hadd_ps(hi, hi);
hi = _mm256_hadd_ps(hi, hi);
result(line, done_cols) = _mm256_cvtss_f32(lo);
lo = _mm256_permute2f128_ps(lo, lo, 1);
result(line, done_cols) = _mm256_cvtss_f32(lo);
result(line, done_cols+2) = _mm256_cvtss_f32(hi);
hi = _mm256_permute2f128_ps(hi, hi, 1);
result(line, done_cols+3) = _mm256_cvtss_f32(hi);
done_cols += 4;
break;
}
#endif
#endif
}
}
}
return result;
}
};
#endif
template<typename FP, size_t ROW, size_t COL>
struct add_helper {
using op_type = Matrix<FP, ROW, COL>;
// Fallback implementation
static op_type add(op_type const& a, op_type const& b) {
op_type output;
size_t done = 0;
while (done < ROW * COL) {
switch (ROW * COL - done) {
case 0:
break; // Nothing
case 1:
case 2:
case 3:
#if !HAS_SSE4_1
default:
#endif
// Basic implementation
*(output.data() + done) = *(a.data() + done) + *(b.data() + done);
++done;
break;
#if HAS_SSE4_1
case 4:
case 5:
case 6:
case 7:
#if !HAS_AVX
default:
#endif
{
__m128 lhs = _mm_load_ps(a.data() + done);
__m128 rhs = _mm_load_ps(b.data() + done);
__m128 r = _mm_add_ps(lhs, rhs);
_mm_store_ps(output.data() + done, r);
done += 4;
break;
}
#if HAS_AVX
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
#if !HAS_AVX512
default:
#endif
{
__m256 lhs = _mm256_load_ps(a.data() + done);
__m256 rhs = _mm256_load_ps(b.data() + done);
__m256 r = _mm256_add_ps(lhs, rhs);
_mm256_store_ps(output.data() + done, r);
done += 8;
break;
}
#if HAS_AVX512
default:
{
__m512 lhs = _mm512_load_ps(a.data() + done);
__m512 rhs = _mm512_load_ps(b.data() + done);
__m512 r = _mm512_add_ps(lhs, rhs);
_mm512_store_ps(output.data() + done, r);
done += 16;
break;
}
#endif // HAS_AVX512
#endif // HAS_AVX
#endif // HAS_SSE4_1
}
}
return output;
}
};
template<typename FP, size_t ROW, size_t COL>
struct sub_helper {
using op_type = Matrix<FP, ROW, COL>;
// Fallback implementation
static op_type sub(op_type const& a, op_type const& b) {
op_type output;
size_t done = 0;
while (done < ROW * COL) {
switch (ROW * COL - done) {
case 0:
break; // Nothing
case 1:
case 2:
case 3:
#if !HAS_SSE4_1
default:
#endif
// Basic implementation
*(output.data() + done) = *(a.data() + done) - *(b.data() + done);
++done;
break;
#if HAS_SSE4_1
case 4:
case 5:
case 6:
case 7:
#if !HAS_AVX
default:
#endif
{
__m128 lhs = _mm_load_ps(a.data() + done);
__m128 rhs = _mm_load_ps(b.data() + done);
__m128 r = _mm_sub_ps(lhs, rhs);
_mm_store_ps(output.data() + done, r);
done += 4;
break;
}
#if HAS_AVX
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
#if !HAS_AVX512
default:
#endif
{
__m256 lhs = _mm256_load_ps(a.data() + done);
__m256 rhs = _mm256_load_ps(b.data() + done);
__m256 r = _mm256_sub_ps(lhs, rhs);
_mm256_store_ps(output.data() + done, r);
done += 8;
break;
}
#if HAS_AVX512
default:
{
__m512 lhs = _mm512_load_ps(a.data() + done);
__m512 rhs = _mm512_load_ps(b.data() + done);
__m512 r = _mm512_sub_ps(lhs, rhs);
_mm512_store_ps(output.data() + done, r);
done += 16;
break;
}
#endif // HAS_AVX512
#endif // HAS_AVX
#endif // HAS_SSE4_1
}
}
return output;
}
};
}
}
template<typename FP, size_t ROW, size_t COL1, size_t COL2>
Matrix<FP, ROW, COL2> multiply(Matrix<FP, ROW, COL1> const& lhs, Matrix<FP, COL1, COL2> const& rhs)
{
return detail::multiply_helper<FP, ROW, COL1, COL2>::multiply(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> add(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs) {
return detail::add_helper<FP, ROW, COL>::add(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> substract(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs) {
return detail::sub_helper<FP, ROW, COL>::sub(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL1, size_t COL2>
Matrix<FP, ROW, COL2> operator*(Matrix<FP, ROW, COL1> const& lhs, Matrix<FP, COL1, COL2> const& rhs)
{
return multiply(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> operator+(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs)
{
return add<FP, ROW, COL>(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL>
Matrix<FP, ROW, COL> operator-(Matrix<FP, ROW, COL> const& lhs, Matrix<FP, ROW, COL> const& rhs) {
return substract<FP, ROW, COL>(lhs, rhs);
}
template<typename FP, size_t ROW, size_t COL>
std::ostream& operator<<(std::ostream& o, Matrix<FP, ROW, COL> const& m) {
for (size_t i = 0; i < m.height(); ++i) {
o << "[ ";
for (size_t j = 0; j < m.width(); ++j) {
o << m(i, j) << " ";
}
o << "]\n";
}
return o;
}
#endif // MATRIX_HPP