-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtranspose.cpp
160 lines (147 loc) · 4.59 KB
/
transpose.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
160
// 3D transpose microbenchmark
#include <complex>
#include <cassert>
#include <benchmark/benchmark.h>
static int x;
static int y;
static int z;
using type = std::complex<float>;
static std::vector<type> input;
static int index(int i, int j, int k) {
return i * y * z + j * z + k;
}
static void bm_assign(benchmark::State& state) {
std::vector<type> output;
while (state.KeepRunning()) {
output = input;
benchmark::DoNotOptimize(output);
}
}
static void bm_naive_zyx(benchmark::State& state) {
std::vector<type> output = input;
while (state.KeepRunning()) {
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k) {
int n1 = i * y * z + j * z + k;
int n2 = i + j * x + k * y * x;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
static void bm_naive_xzy(benchmark::State& state) {
std::vector<type> output = input;
while (state.KeepRunning()) {
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k) {
int n1 = i * y * z + j * z + k;
int n2 = i * y * z + j + k * y;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
static void bm_naive_yxz(benchmark::State& state) {
std::vector<type> output = input;
while (state.KeepRunning()) {
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k) {
int n1 = i * y * z + j * z + k;
int n2 = i * z + j * x * z + k;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
// equivalent of 2D transpose
static void bm_naive_zxy(benchmark::State& state) {
std::vector<type> output = input;
while (state.KeepRunning()) {
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k) {
int n1 = (i * y + j) * z + k;
int n2 = i * y + j + k * x * y;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
static void bm_naive_yzx(benchmark::State& state) {
std::vector<type> output = input;
while (state.KeepRunning()) {
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k) {
int n1 = i * y * z + j * z + k;
int n2 = j * x * z + k * x + i;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
static void bm_tiled_zxy(benchmark::State& state) {
std::vector<type> output = input;
const int N = 8;
while (state.KeepRunning()) {
int xy = x * y;
for (int i = 0; i < xy; i += N)
for (int k = 0; k < z; k += N)
for (int i2 = i; i2 < std::min(i + N, xy); ++i2)
for (int k2 = k; k2 < std::min(k + N, z); ++k2) {
int n1 = i2 * z + k2;
int n2 = i2 + k2 * xy;
output[n2] = input[n1];
}
benchmark::DoNotOptimize(output);
}
}
static void bm_inplace_zxy(benchmark::State& state) {
std::vector<char> is_swapped(input.size());
const int N = 8;
while (state.KeepRunning()) {
std::fill(is_swapped.begin(), is_swapped.end(), 0);
int xy = x * y;
for (int i = 0; i < xy; i += N)
for (int k = 0; k < z; k += N)
for (int i2 = i; i2 < std::min(i + N, xy); ++i2)
for (int k2 = k; k2 < std::min(k + N, z); ++k2) {
int n1 = i2 * z + k2;
if (!is_swapped[n1]) {
int n2 = i2 + k2 * xy;
std::swap(input[n2], input[n1]);
is_swapped[n2] = 1;
}
}
benchmark::DoNotOptimize(input);
}
}
int main(int argc, char** argv) {
if (argc < 4) {
printf("Call it with 3D size as arguments.\n");
return 1;
}
x = std::stoi(argv[argc-3]);
y = std::stoi(argv[argc-2]);
z = std::stoi(argv[argc-1]);
input.resize(x * y * z);
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
for (int k = 0; k < z; ++k)
input[index(i, j, k)] = {0.3f * i + 0.1f * j * j - 0.1f * k * k,
10.f * i - sqrtf(i)};
benchmark::RegisterBenchmark("assign", bm_assign);
benchmark::RegisterBenchmark("naive zyx", bm_naive_zyx);
benchmark::RegisterBenchmark("naive xzy", bm_naive_xzy);
benchmark::RegisterBenchmark("naive yxz", bm_naive_yxz);
benchmark::RegisterBenchmark("naive zxy", bm_naive_zxy);
benchmark::RegisterBenchmark("naive yzx", bm_naive_yzx);
benchmark::RegisterBenchmark("tiled zxy", bm_tiled_zxy);
benchmark::RegisterBenchmark("in-place zxy", bm_inplace_zxy);
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
}
// vim:sw=2:ts=2:et