-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbench-from.cc
218 lines (204 loc) · 9.05 KB
/
bench-from.cc
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
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/bench - Selection ops in ra::
// (c) Daniel Llorens - 2015, 2017
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#include <iostream>
#include <iomanip>
#include <string>
#include "ra/test.hh"
using std::cout, std::endl, std::flush, ra::TestRecorder, ra::Benchmark;
using real = double;
int main()
{
TestRecorder tr(cout);
cout.precision(4);
tr.section("rank1(rank1)");
{
auto rank1_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
{
cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
using Array1 = std::decay_t<decltype(A_)>;
Array1 A = ra::iota(Asize);
ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
Array1 B({Isize}, 0);
auto II = I.data();
auto AA = A.data();
auto BB = B.data();
Benchmark bm { N, 3 };
auto report = [&](std::string const & tag, auto && bv)
{
tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
.test_eq(ra::iota(Isize)*Istep, B);
};
report("indexing on raw pointers",
bm.run([&] {
for (int i=0; i<Isize; ++i) {
BB[i] = AA[II[i]];
}
}));
report("vectorized selection",
bm.run([&] {
B = A(I);
}));
report("write out the indexing loop",
bm.run([&] {
for_each([&A](auto & b, auto i) { b = A(i); }, B, I);
}));
report("loop on scalar selection",
bm.run([&]() {
for (int i=0; i<Isize; ++i) {
B(i) = A(I(i));
}
}));
};
tr.section("fixed rank");
rank1_test(ra::Unique<real, 1>(), 10000, 500, 20, 5000);
rank1_test(ra::Unique<real, 1>(), 1000, 50, 20, 10*5000);
rank1_test(ra::Unique<real, 1>(), 100, 5, 20, 100*5000);
rank1_test(ra::Unique<real, 1>(), 10000, 500, 2, 5000);
rank1_test(ra::Unique<real, 1>(), 1000, 50, 2, 10*5000);
rank1_test(ra::Unique<real, 1>(), 100, 5, 2, 100*5000);
tr.section("var rank");
rank1_test(ra::Unique<real>(), 10000, 500, 20, 5000);
rank1_test(ra::Unique<real>(), 1000, 50, 20, 10*5000);
rank1_test(ra::Unique<real>(), 100, 5, 20, 100*5000);
rank1_test(ra::Unique<real>(), 10000, 500, 2, 5000);
rank1_test(ra::Unique<real>(), 1000, 50, 2, 10*5000);
rank1_test(ra::Unique<real>(), 100, 5, 2, 100*5000);
}
tr.section("rank2(rank1, rank1)");
{
auto rank1_11_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
{
cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
using Array2 = std::decay_t<decltype(A_)>;
Array2 A({Asize, Asize}, ra::_0 + ra::_1);
ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
Array2 B({Isize, Isize}, 0);
auto II = I.data();
auto AA = A.data();
auto BB = B.data();
Benchmark bm { N, 3 };
auto report = [&](std::string const &tag, auto && bv)
{
tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
.test_eq(Istep*(ra::_0 + ra::_1), B);
};
report("2D indexing on raw pointers",
bm.run([&] {
for (int i=0; i<Isize; ++i) {
for (int j=0; j<Isize; ++j) {
BB[i*Isize + j] = AA[II[i]*Asize + II[j]];
}
}
}));
report("vectorized selection",
bm.run([&] {
B = A(I, I);
}));
};
tr.section("fixed rank");
rank1_11_test(ra::Unique<real, 2>(), 1000, 50, 20, 5000);
rank1_11_test(ra::Unique<real, 2>(), 100, 5, 20, 10*10*5000);
rank1_11_test(ra::Unique<real, 2>(), 1000, 50, 2, 5000);
rank1_11_test(ra::Unique<real, 2>(), 100, 5, 2, 10*10*5000);
rank1_11_test(ra::Unique<real, 2>(), 10, 5, 2, 10*10*5000);
tr.section("var rank");
rank1_11_test(ra::Unique<real>(), 1000, 50, 20, 5000);
rank1_11_test(ra::Unique<real>(), 100, 5, 20, 10*10*5000);
rank1_11_test(ra::Unique<real>(), 1000, 50, 2, 5000);
rank1_11_test(ra::Unique<real>(), 100, 5, 2, 10*10*5000);
rank1_11_test(ra::Unique<real>(), 10, 5, 2, 10*10*5000);
}
tr.section("rank3(rank1, rank1, rank1)");
{
auto rank1_111_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
{
cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
using Array3 = std::decay_t<decltype(A_)>;
Array3 A({Asize, Asize, Asize}, 10000*ra::_0 + 100*ra::_1 + 1*ra::_2);
ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
Array3 B({Isize, Isize, Isize}, 0);
auto II = I.data();
auto AA = A.data();
auto BB = B.data();
Benchmark bm { N, 3 };
auto report = [&](std::string const &tag, auto && bv)
{
tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
.test_eq(Istep*(10000*ra::_0 + 100*ra::_1 + 1*ra::_2), B);
};
report("3D indexing on raw pointers",
bm.run([&] {
for (int i=0; i<Isize; ++i) {
for (int j=0; j<Isize; ++j) {
for (int k=0; k<Isize; ++k) {
BB[k+Isize*(j+Isize*i)] = AA[II[k]+Asize*(II[j]+Asize*II[i])];
}
}
}
}));
report("vectorized selection",
bm.run([&] {
B = A(I, I, I);
}));
};
tr.section("fixed rank");
rank1_111_test(ra::Unique<real, 3>(), 40, 20, 2, 2000);
rank1_111_test(ra::Unique<real, 3>(), 100, 5, 20, 4*4*4*2000);
rank1_111_test(ra::Unique<real, 3>(), 10, 5, 2, 4*4*4*2000);
}
tr.section("rank4(rank1, rank1, rank1, rank1)");
{
auto rank1_1111_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
{
cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
using Array4 = std::decay_t<decltype(A_)>;
ra::Unique<real, 4> A(ra::Small<int, 4>(Asize), 1000000*ra::_0 + 10000*ra::_1 + 100*ra::_2 + 1*ra::_3);
ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
Array4 B(ra::Small<int, 4>(Isize), 0);
auto II = I.data();
auto AA = A.data();
auto BB = B.data();
Benchmark bm { N, 3 };
auto report = [&](std::string const &tag, auto && bv)
{
tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
.test_eq(Istep*(1000000*ra::_0 + 10000*ra::_1 + 100*ra::_2 + 1*ra::_3), B);
};
report("3D indexing on raw pointers",
bm.run([&] {
for (int i=0; i<Isize; ++i) {
for (int j=0; j<Isize; ++j) {
for (int k=0; k<Isize; ++k) {
for (int l=0; l<Isize; ++l) {
BB[l+Isize*(k+Isize*(j+Isize*i))] = AA[II[l]+Asize*(II[k]+Asize*(II[j]+Asize*II[i]))];
}
}
}
}
}));
report("vectorized selection",
bm.run([&] {
B = A(I, I, I, I);
}));
report("slice one axis at a time", // TODO one way A(i, i, i, i) could work
bm.run([&] {
for (int i=0; i<Isize; ++i) {
for (int j=0; j<Isize; ++j) {
for (int k=0; k<Isize; ++k) {
B(i, j, k) = A(I[i], I[j], I[k])(I);
}
}
}
}));
};
tr.section("fixed rank");
rank1_1111_test(ra::Unique<real, 4>(), 40, 20, 2, 100);
rank1_1111_test(ra::Unique<real, 4>(), 10, 5, 2, 4*4*4*4*100);
}
return tr.summary();
}