-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbufferguard.cpp
277 lines (238 loc) · 8.76 KB
/
bufferguard.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
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
#include <algorithm>
#include <array>
#include <cstring>
#include <fmt/core.h>
#include <fstream>
#include <llama/DumpMapping.hpp>
#include <llama/llama.hpp>
// clang-format off
namespace tag
{
struct X{};
struct Y{};
struct Z{};
} // namespace tag
using Vector = llama::Record<
llama::Field<tag::X, int>,
llama::Field<tag::Y, int>,
llama::Field<tag::Z, int>
>;
// clang-format on
template<template<typename, typename> typename InnerMapping, typename TRecordDim>
struct GuardMapping2D : llama::ArrayExtentsDynamic<2>
{
using ArrayExtents = llama::ArrayExtentsDynamic<2>;
using ArrayIndex = llama::ArrayIndex<2>;
using RecordDim = TRecordDim;
constexpr GuardMapping2D() = default;
constexpr explicit GuardMapping2D(ArrayExtents extents, RecordDim = {})
: llama::ArrayExtentsDynamic<2>(extents)
, left({extents[0] - 2})
, right({extents[0] - 2})
, top({extents[1] - 2})
, bot({extents[1] - 2})
, center({extents[0] - 2, extents[1] - 2})
{
}
constexpr auto extents() const -> ArrayExtents
{
return *this; // NOLINT(cppcoreguidelines-slicing)
}
constexpr auto blobSize(std::size_t i) const -> std::size_t
{
if(i >= centerOff)
return center.blobSize(i - centerOff);
if(i >= botOff)
return bot.blobSize(i - botOff);
if(i >= topOff)
return top.blobSize(i - topOff);
if(i >= rightOff)
return right.blobSize(i - rightOff);
if(i >= leftOff)
return left.blobSize(i - leftOff);
if(i >= rightBotOff)
return rightBot.blobSize(i - rightBotOff);
if(i >= rightTopOff)
return rightTop.blobSize(i - rightTopOff);
if(i >= leftBotOff)
return leftBot.blobSize(i - leftBotOff);
if(i >= leftTopOff)
return leftTop.blobSize(i - leftTopOff);
std::abort();
}
template<std::size_t... RecordCoords, std::size_t N = 0>
constexpr auto blobNrAndOffset(
ArrayIndex ai,
llama::Array<std::size_t, N> = {},
llama::RecordCoord<RecordCoords...> rc = {}) const -> llama::NrAndOffset
{
// [0][0] is at left top
const auto [row, col] = ai;
const auto [rowMax, colMax] = extents();
if(col == 0)
{
if(row == 0)
return offsetBlobNr(leftTop.blobNrAndOffset({}, rc), leftTopOff);
if(row == rowMax - 1)
return offsetBlobNr(leftBot.blobNrAndOffset({}, rc), leftBotOff);
return offsetBlobNr(left.blobNrAndOffset({row - 1}, rc), leftOff);
}
if(col == colMax - 1)
{
if(row == 0)
return offsetBlobNr(rightTop.blobNrAndOffset({}, rc), rightTopOff);
if(row == rowMax - 1)
return offsetBlobNr(rightBot.blobNrAndOffset({}, rc), rightBotOff);
return offsetBlobNr(right.blobNrAndOffset({row - 1}, rc), rightOff);
}
if(row == 0)
return offsetBlobNr(top.blobNrAndOffset({col - 1}, rc), topOff);
if(row == rowMax - 1)
return offsetBlobNr(bot.blobNrAndOffset({col - 1}, rc), botOff);
return offsetBlobNr(center.blobNrAndOffset({row - 1, col - 1}, rc), centerOff);
}
constexpr auto centerBlobs() const
{
return blobIndices(center, centerOff);
}
constexpr auto leftTopBlobs() const
{
return blobIndices(leftTop, leftTopOff);
}
constexpr auto leftBotBlobs() const
{
return blobIndices(leftBot, leftBotOff);
}
constexpr auto leftBlobs() const
{
return blobIndices(left, leftOff);
}
constexpr auto rightTopBlobs() const
{
return blobIndices(rightTop, rightTopOff);
}
constexpr auto rightBotBlobs() const
{
return blobIndices(rightBot, rightBotOff);
}
constexpr auto rightBlobs() const
{
return blobIndices(right, rightOff);
}
constexpr auto topBlobs() const
{
return blobIndices(top, topOff);
}
constexpr auto botBlobs() const
{
return blobIndices(bot, botOff);
}
private:
constexpr auto offsetBlobNr(llama::NrAndOffset nao, std::size_t blobNrOffset) const -> llama::NrAndOffset
{
nao.nr += blobNrOffset;
return nao;
}
template<typename Mapping>
constexpr auto blobIndices(const Mapping&, std::size_t offset) const
{
std::array<std::size_t, Mapping::blobCount> a{};
std::generate(a.begin(), a.end(), [i = offset]() mutable { return i++; });
return a;
}
llama::mapping::One<llama::ArrayExtents<>, RecordDim> leftTop;
llama::mapping::One<llama::ArrayExtents<>, RecordDim> leftBot;
llama::mapping::One<llama::ArrayExtents<>, RecordDim> rightTop;
llama::mapping::One<llama::ArrayExtents<>, RecordDim> rightBot;
InnerMapping<llama::ArrayExtentsDynamic<1>, RecordDim> left;
InnerMapping<llama::ArrayExtentsDynamic<1>, RecordDim> right;
InnerMapping<llama::ArrayExtentsDynamic<1>, RecordDim> top;
InnerMapping<llama::ArrayExtentsDynamic<1>, RecordDim> bot;
InnerMapping<llama::ArrayExtentsDynamic<2>, RecordDim> center;
static constexpr auto leftTopOff = std::size_t{0};
static constexpr auto leftBotOff = leftTopOff + decltype(leftTop)::blobCount;
static constexpr auto rightTopOff = leftBotOff + decltype(leftBot)::blobCount;
static constexpr auto rightBotOff = rightTopOff + decltype(rightTop)::blobCount;
static constexpr auto leftOff = rightBotOff + decltype(rightBot)::blobCount;
static constexpr auto rightOff = leftOff + decltype(left)::blobCount;
static constexpr auto topOff = rightOff + decltype(right)::blobCount;
static constexpr auto botOff = topOff + decltype(top)::blobCount;
static constexpr auto centerOff = botOff + decltype(bot)::blobCount;
public:
static constexpr auto blobCount = centerOff + decltype(center)::blobCount;
};
template<typename View>
void printView(const View& view, std::size_t rows, std::size_t cols)
{
for(std::size_t row = 0; row < rows; row++)
{
for(std::size_t col = 0; col < cols; col++)
std::cout << fmt::format(
"[{:3},{:3},{:3}]",
view(row, col)(tag::X{}),
view(row, col)(tag::Y{}),
view(row, col)(tag::Z{}));
std::cout << '\n';
}
}
template<template<typename, typename> typename Mapping>
void run(const std::string& mappingName)
{
std::cout << "\n===== Mapping " << mappingName << " =====\n\n";
constexpr auto rows = 7;
constexpr auto cols = 5;
const auto extents = llama::ArrayExtents{rows, cols};
const auto mapping = GuardMapping2D<Mapping, Vector>{extents};
std::ofstream{"bufferguard_" + mappingName + ".svg"} << llama::toSvg(mapping);
auto view1 = llama::allocViewUninitialized(mapping);
int i = 0;
for(std::size_t row = 0; row < rows; row++)
for(std::size_t col = 0; col < cols; col++)
{
view1(row, col)(tag::X{}) = ++i;
view1(row, col)(tag::Y{}) = ++i;
view1(row, col)(tag::Z{}) = ++i;
}
std::cout << "View 1:\n";
printView(view1, rows, cols);
auto view2 = llama::allocViewUninitialized(mapping);
for(std::size_t row = 0; row < rows; row++)
for(std::size_t col = 0; col < cols; col++)
view2(row, col) = 0; // broadcast
std::cout << "\nView 2:\n";
printView(view2, rows, cols);
auto copyBlobs = [&](auto& srcView, auto& dstView, auto srcBlobs, auto dstBlobs)
{
static_assert(srcBlobs.size() == dstBlobs.size());
for(auto i = 0; i < srcBlobs.size(); i++)
{
const auto src = srcBlobs[i];
const auto dst = dstBlobs[i];
assert(mapping.blobSize(src) == mapping.blobSize(dst));
std::memcpy(&dstView.storageBlobs[dst][0], &srcView.storageBlobs[src][0], mapping.blobSize(src));
}
};
std::cout << "\nCopy view 1 right -> view 2 left:\n";
copyBlobs(view1, view2, mapping.rightBlobs(), mapping.leftBlobs());
std::cout << "View 2:\n";
printView(view2, rows, cols);
std::cout << "\nCopy view 1 left top -> view 2 right bot:\n";
copyBlobs(view1, view2, mapping.leftTopBlobs(), mapping.rightBotBlobs());
std::cout << "View 2:\n";
printView(view2, rows, cols);
std::cout << "\nCopy view 2 center -> view 1 center:\n";
copyBlobs(view2, view1, mapping.centerBlobs(), mapping.centerBlobs());
std::cout << "View 1:\n";
printView(view1, rows, cols);
}
auto main() -> int
try
{
run<llama::mapping::PreconfiguredAoS<>::type>("AoS");
run<llama::mapping::PreconfiguredSoA<>::type>("SoA");
run<llama::mapping::PreconfiguredSoA<true>::type>("SoA_MB");
}
catch(const std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
}