-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathblock_operator.cpp
More file actions
305 lines (254 loc) · 9.06 KB
/
block_operator.cpp
File metadata and controls
305 lines (254 loc) · 9.06 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include "ginkgo/core/base/block_operator.hpp"
#include <utility>
#include <ginkgo/core/base/precision_dispatch.hpp>
#include <ginkgo/core/matrix/dense.hpp>
#include "core/base/dispatch_helper.hpp"
namespace gko {
namespace {
template <typename Fn>
auto dispatch_dense(Fn&& fn, LinOp* v)
{
return run<matrix::Dense, float, double,
#if GINKGO_ENABLE_HALF
float16, std::complex<float16>,
#endif
#if GINKGO_ENABLE_BFLOAT16
bfloat16, std::complex<bfloat16>,
#endif
std::complex<float>, std::complex<double>>(v,
std::forward<Fn>(fn));
}
template <typename LinOpType>
auto create_vector_blocks(LinOpType* vector,
const std::vector<detail::value_span>& spans)
{
return [=](size_type i) {
return dispatch_dense(
[&](auto* dense) -> std::unique_ptr<LinOpType> {
GKO_ENSURE_IN_BOUNDS(i, spans.size());
return dense->create_submatrix(spans[i],
{0, dense->get_size()[1]});
},
const_cast<LinOp*>(vector));
};
}
const LinOp* find_non_zero_in_row(
const std::vector<std::vector<std::shared_ptr<const LinOp>>>& blocks,
size_type row)
{
auto it = std::find_if(blocks[row].begin(), blocks[row].end(),
[](const auto& b) { return b.get() != nullptr; });
GKO_THROW_IF_INVALID(it != blocks[row].end(),
"Encountered row with only nullptrs.");
return it->get();
}
const LinOp* find_non_zero_in_col(
const std::vector<std::vector<std::shared_ptr<const LinOp>>>& blocks,
size_type col)
{
auto it = std::find_if(blocks.begin(), blocks.end(), [col](const auto& b) {
return b[col].get() != nullptr;
});
GKO_THROW_IF_INVALID(it != blocks.end(),
"Encountered columns with only nullptrs.");
return it->at(col).get();
}
void validate_blocks(
const std::vector<std::vector<std::shared_ptr<const LinOp>>>& blocks)
{
GKO_THROW_IF_INVALID(blocks.empty() || !blocks.front().empty(),
"Blocks must either be empty, or a 2D std::vector.");
// all rows have same number of columns
for (size_type row = 1; row < blocks.size(); ++row) {
GKO_ASSERT_EQ(blocks[row].size(), blocks.front().size());
}
// within each row and each column the blocks have the same number of rows
// and columns respectively
for (size_type row = 0; row < blocks.size(); ++row) {
auto non_zero_row = find_non_zero_in_row(blocks, row);
for (size_type col = 0; col < blocks.front().size(); ++col) {
auto non_zero_col = find_non_zero_in_col(blocks, col);
if (blocks[row][col]) {
GKO_ASSERT_EQUAL_COLS(blocks[row][col], non_zero_col);
GKO_ASSERT_EQUAL_ROWS(blocks[row][col], non_zero_row);
}
}
}
}
template <typename Fn>
std::vector<detail::value_span> compute_local_spans(
size_type num_blocks,
const std::vector<std::vector<std::shared_ptr<const LinOp>>>& blocks,
Fn&& get_size)
{
validate_blocks(blocks);
std::vector<detail::value_span> local_spans;
size_type offset = 0;
for (size_type i = 0; i < num_blocks; ++i) {
auto local_size = get_size(i);
local_spans.emplace_back(offset, offset + local_size);
offset += local_size;
}
return local_spans;
}
dim<2> compute_global_size(
const std::vector<std::vector<std::shared_ptr<const LinOp>>>& blocks)
{
validate_blocks(blocks);
if (blocks.empty()) {
return {};
}
size_type num_rows = 0;
for (size_type row = 0; row < blocks.size(); ++row) {
num_rows += find_non_zero_in_row(blocks, row)->get_size()[0];
}
size_type num_cols = 0;
for (size_type col = 0; col < blocks.front().size(); ++col) {
num_cols += find_non_zero_in_col(blocks, col)->get_size()[1];
}
return {num_rows, num_cols};
}
} // namespace
std::unique_ptr<BlockOperator> BlockOperator::create(
std::shared_ptr<const Executor> exec)
{
return std::unique_ptr<BlockOperator>(new BlockOperator(std::move(exec)));
}
std::unique_ptr<BlockOperator> BlockOperator::create(
std::shared_ptr<const Executor> exec,
std::vector<std::vector<std::shared_ptr<const LinOp>>> blocks)
{
return std::unique_ptr<BlockOperator>(
new BlockOperator(std::move(exec), std::move(blocks)));
}
BlockOperator::BlockOperator(std::shared_ptr<const Executor> exec)
: EnableClonableLinOp<BlockOperator>(std::move(exec))
{}
BlockOperator::BlockOperator(
std::shared_ptr<const Executor> exec,
std::vector<std::vector<std::shared_ptr<const LinOp>>> blocks)
: EnableClonableLinOp<BlockOperator>(exec, compute_global_size(blocks)),
block_size_(blocks.empty()
? dim<2>{}
: dim<2>(blocks.size(), blocks.front().size())),
row_spans_(compute_local_spans(
block_size_[0], blocks,
[&](auto i) {
return find_non_zero_in_row(blocks, i)->get_size()[0];
})),
col_spans_(compute_local_spans(block_size_[1], blocks, [&](auto i) {
return find_non_zero_in_col(blocks, i)->get_size()[1];
}))
{
for (auto& row : blocks) {
for (auto& block : row) {
if (block && block->get_executor() != exec) {
blocks_.push_back(gko::clone(exec, block));
} else {
blocks_.push_back(std::move(block));
}
}
}
}
void init_one_cache(std::shared_ptr<const Executor> exec,
const detail::DenseCache<default_precision>& one_cache)
{
if (one_cache.get() == nullptr) {
one_cache.init(std::move(exec), {1, 1});
one_cache->fill(one<default_precision>());
}
}
void BlockOperator::apply_impl(const LinOp* b, LinOp* x) const
{
auto block_b = create_vector_blocks(b, col_spans_);
auto block_x = create_vector_blocks(x, row_spans_);
init_one_cache(this->get_executor(), one_);
for (size_type row = 0; row < block_size_[0]; ++row) {
bool first_in_row = true;
for (size_type col = 0; col < block_size_[1]; ++col) {
if (!block_at(row, col)) {
continue;
}
if (first_in_row) {
block_at(row, col)->apply(block_b(col), block_x(row));
first_in_row = false;
} else {
block_at(row, col)->apply(one_.get(), block_b(col), one_.get(),
block_x(row));
}
}
}
}
void BlockOperator::apply_impl(const LinOp* alpha, const LinOp* b,
const LinOp* beta, LinOp* x) const
{
auto block_b = create_vector_blocks(b, col_spans_);
auto block_x = create_vector_blocks(x, row_spans_);
init_one_cache(this->get_executor(), one_);
for (size_type row = 0; row < block_size_[0]; ++row) {
bool first_in_row = true;
for (size_type col = 0; col < block_size_[1]; ++col) {
if (!block_at(row, col)) {
continue;
}
if (first_in_row) {
block_at(row, col)->apply(alpha, block_b(col), beta,
block_x(row));
first_in_row = false;
} else {
block_at(row, col)->apply(alpha, block_b(col), one_.get(),
block_x(row));
}
}
}
}
BlockOperator::BlockOperator(const BlockOperator& other)
: EnableClonableLinOp<BlockOperator>(other.get_executor())
{
*this = other;
}
BlockOperator::BlockOperator(BlockOperator&& other) noexcept
: EnableClonableLinOp<BlockOperator>(other.get_executor())
{
*this = std::move(other);
}
BlockOperator& BlockOperator::operator=(const BlockOperator& other)
{
if (this != &other) {
auto exec = this->get_executor();
set_size(other.get_size());
block_size_ = other.get_block_size();
col_spans_ = other.col_spans_;
row_spans_ = other.row_spans_;
blocks_.clear();
for (const auto& block : other.blocks_) {
blocks_.emplace_back(block == nullptr ? nullptr
: gko::clone(exec, block));
}
}
return *this;
}
BlockOperator& BlockOperator::operator=(BlockOperator&& other)
{
if (this != &other) {
auto exec = this->get_executor();
set_size(other.get_size());
other.set_size({});
block_size_ = std::exchange(other.block_size_, dim<2>{});
col_spans_ = std::move(other.col_spans_);
row_spans_ = std::move(other.row_spans_);
blocks_ = std::move(other.blocks_);
if (exec != other.get_executor()) {
for (auto& block : blocks_) {
if (block != nullptr) {
block = gko::clone(exec, block);
}
}
}
}
return *this;
}
} // namespace gko