-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplication.cpp
More file actions
128 lines (110 loc) · 3.59 KB
/
multiplication.cpp
File metadata and controls
128 lines (110 loc) · 3.59 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
#define ANKERL_NANOBENCH_IMPLEMENT
#include "nanobench.h"
#include "CLI/App.hpp"
#include "CLI/Formatter.hpp"
#include "CLI/Config.hpp"
#include <random>
#include <cmath>
#include <algorithm>
#include <vector>
#include "tatami/tatami.hpp"
template<bool reallocate_>
double multiply(const tatami::Matrix<double, int>& matrix, const int iterations) {
auto NR = matrix.nrow();
auto NC = matrix.ncol();
std::mt19937_64 rng(123456);
std::normal_distribution<double> ndist;
std::vector<double> rhs(NC);
auto outer_buffer = [&]{
if constexpr(reallocate_) {
return true;
} else {
return std::vector<double>(NC);
}
}();
auto outer_ext = [&]{
if constexpr(reallocate_) {
return true;
} else {
return matrix.dense_row();
}
}();
double output = 0;
for (int it = 0; it < iterations; ++it) {
auto inner_buffer = [&]{
if constexpr(reallocate_) {
return std::vector<double>(NC);
} else {
return true;
}
}();
auto inner_ext = [&]{
if constexpr(reallocate_) {
return matrix.dense_row();
} else {
return true;
}
}();
auto& buffer = [&]() -> std::vector<double>& {
if constexpr(reallocate_) {
return inner_buffer;
} else {
return outer_buffer;
}
}();
auto& ext = [&]() -> std::unique_ptr<tatami::MyopicDenseExtractor<double, int> >& {
if constexpr(reallocate_) {
return inner_ext;
} else {
return outer_ext;
}
}();
for (auto& s : rhs) {
s = ndist(rng);
}
double sum = 0;
for (decltype(NR) r = 0; r < NR; ++r) {
const auto ptr = ext->fetch(r, buffer.data());
tatami::copy_n(ptr, NC, buffer.data());
sum += std::inner_product(buffer.begin(), buffer.end(), rhs.data(), 0.0);
}
output += sum;
}
return output;
}
int main(int argc, char* argv[]) {
CLI::App app{"Expanded testing checks"};
int nr;
app.add_option("-r,--nrow", nr, "Number of rows")->default_val(10000);
int nc;
app.add_option("-c,--ncol", nc, "Number of columns")->default_val(2000);
int iterations;
app.add_option("-i,--iter", iterations, "Number of iterations")->default_val(10);
CLI11_PARSE(app, argc, argv);
std::cout << "Testing a " << nr << " x " << nc << " matrix" << std::endl;
std::mt19937_64 rng(98765);
std::vector<double> contents(nr * nc);
std::normal_distribution<double> ndist;
for (auto& x : contents) {
x = ndist(rng);
}
tatami::DenseMatrix<double, int, std::vector<double> > matrix(nr, nc, std::move(contents), true);
double expected = -1;
ankerl::nanobench::Bench().run("reallocated", [&](){
double sum = multiply<true>(matrix, iterations);
if (expected < 0) {
expected = sum;
} else if (std::abs(sum - expected) > 0.00001) {
std::cerr << "WARNING: different result from outer summation (" << sum << ")" << std::endl;
}
});
ankerl::nanobench::Bench().run("reused", [&](){
double sum = multiply<false>(matrix, iterations);
if (expected < 0) {
expected = sum;
} else if (std::abs(sum - expected) > 0.00001) {
std::cerr << "WARNING: different result from outer summation (" << sum << ")" << std::endl;
}
});
return 0;
}