forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_view_assignment.cpp
159 lines (147 loc) · 5.5 KB
/
benchmark_view_assignment.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
154
155
156
157
158
159
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <benchmark/benchmark.h>
#include "xtensor/xnoalias.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xfixed.hpp"
#include "xtensor/xrandom.hpp"
namespace xt
{
inline void create_xview(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
auto v = xt::view(tens, 1, 2, all(), all());
}
}
inline void create_strided_view_outofplace(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
xstrided_slice_vector sv = {1, 2, all(), all()};
for (auto _ : state)
{
auto v = xt::strided_view(tens, sv);
}
}
inline void create_strided_view_inplace(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
auto v = xt::strided_view(tens, {1, 2, all(), all()});
}
}
inline void assign_create_view(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
auto v = xt::view(tens, i, j, all(), all());
xt::xtensor<double, 2> vas = v;
benchmark::ClobberMemory();
}
}
}
}
inline void assign_create_strided_view(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
auto v = xt::strided_view(tens, {i, j, all(), all()});
xt::xtensor<double, 2> vas = v;
benchmark::ClobberMemory();
}
}
}
}
inline void assign_create_manual_view(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
auto v = xt::view(tens, i, j, all(), all());
xt::xtensor<double, 2> vas(std::array<std::size_t, 2>({3, 3}));
std::copy(v.data() + v.data_offset(), v.data() + v.data_offset() + vas.size(), vas.begin());
benchmark::ClobberMemory();
}
}
}
}
inline void assign_create_manual_noview(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
ptrdiff_t offset = i * tens.strides()[0] + j * tens.strides()[1];
xt::xtensor<double, 2> vas(std::array<std::size_t, 2>({3, 3}));
std::copy(tens.data() + offset, tens.data() + offset + vas.size(), vas.begin());
benchmark::ClobberMemory();
}
}
}
}
inline void data_offset(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
volatile ptrdiff_t offset = i * tens.strides()[0] + j * tens.strides()[1];
static_cast<void>(offset);
}
}
}
}
inline void data_offset_view(benchmark::State& state)
{
xt::xtensor<double, 4> tens = xt::random::rand<double>({100, 100, 3, 3});
for (auto _ : state)
{
for (std::size_t i = 0; i < tens.shape()[0]; ++i)
{
for (std::size_t j = 0; j < tens.shape()[1]; ++j)
{
auto v = xt::view(tens, i, j, all(), all());
volatile ptrdiff_t offset = v.data_offset();
static_cast<void>(offset);
}
}
}
}
BENCHMARK(create_xview);
BENCHMARK(create_strided_view_outofplace);
BENCHMARK(create_strided_view_inplace);
BENCHMARK(assign_create_manual_noview);
BENCHMARK(assign_create_strided_view);
BENCHMARK(assign_create_view);
BENCHMARK(assign_create_manual_view);
BENCHMARK(data_offset);
BENCHMARK(data_offset_view);
}