-
Notifications
You must be signed in to change notification settings - Fork 90
/
coo.cpp
432 lines (353 loc) · 14.9 KB
/
coo.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include "ginkgo/core/matrix/coo.hpp"
#include <algorithm>
#include <numeric>
#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/precision_dispatch.hpp>
#include <ginkgo/core/base/temporary_clone.hpp>
#include <ginkgo/core/base/utils.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/dense.hpp>
#include "core/base/device_matrix_data_kernels.hpp"
#include "core/components/absolute_array_kernels.hpp"
#include "core/components/fill_array_kernels.hpp"
#include "core/components/format_conversion_kernels.hpp"
#include "core/matrix/coo_kernels.hpp"
namespace gko {
namespace matrix {
namespace coo {
namespace {
GKO_REGISTER_OPERATION(spmv, coo::spmv);
GKO_REGISTER_OPERATION(advanced_spmv, coo::advanced_spmv);
GKO_REGISTER_OPERATION(spmv2, coo::spmv2);
GKO_REGISTER_OPERATION(advanced_spmv2, coo::advanced_spmv2);
GKO_REGISTER_OPERATION(convert_idxs_to_ptrs, components::convert_idxs_to_ptrs);
GKO_REGISTER_OPERATION(fill_in_dense, coo::fill_in_dense);
GKO_REGISTER_OPERATION(extract_diagonal, coo::extract_diagonal);
GKO_REGISTER_OPERATION(fill_array, components::fill_array);
GKO_REGISTER_OPERATION(inplace_absolute_array,
components::inplace_absolute_array);
GKO_REGISTER_OPERATION(outplace_absolute_array,
components::outplace_absolute_array);
GKO_REGISTER_OPERATION(aos_to_soa, components::aos_to_soa);
} // anonymous namespace
} // namespace coo
template <typename ValueType, typename IndexType>
std::unique_ptr<Coo<ValueType, IndexType>> Coo<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
size_type num_nonzeros)
{
return std::unique_ptr<Coo>{new Coo{exec, size, num_nonzeros}};
}
template <typename ValueType, typename IndexType>
std::unique_ptr<Coo<ValueType, IndexType>> Coo<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
array<value_type> values, array<index_type> col_idxs,
array<index_type> row_idxs)
{
return std::unique_ptr<Coo>{new Coo{exec, size, std::move(values),
std::move(col_idxs),
std::move(row_idxs)}};
}
template <typename ValueType, typename IndexType>
std::unique_ptr<const Coo<ValueType, IndexType>>
Coo<ValueType, IndexType>::create_const(
std::shared_ptr<const Executor> exec, const dim<2>& size,
gko::detail::const_array_view<ValueType>&& values,
gko::detail::const_array_view<IndexType>&& col_idxs,
gko::detail::const_array_view<IndexType>&& row_idxs)
{
// cast const-ness away, but return a const object afterwards,
// so we can ensure that no modifications take place.
return create(exec, size, gko::detail::array_const_cast(std::move(values)),
gko::detail::array_const_cast(std::move(col_idxs)),
gko::detail::array_const_cast(std::move(row_idxs)));
}
template <typename ValueType, typename IndexType>
Coo<ValueType, IndexType>::Coo(std::shared_ptr<const Executor> exec,
const dim<2>& size, size_type num_nonzeros)
: EnableLinOp<Coo>(exec, size),
values_(exec, num_nonzeros),
col_idxs_(exec, num_nonzeros),
row_idxs_(exec, num_nonzeros)
{}
template <typename ValueType, typename IndexType>
Coo<ValueType, IndexType>::Coo(std::shared_ptr<const Executor> exec,
const dim<2>& size, array<value_type> values,
array<index_type> col_idxs,
array<index_type> row_idxs)
: EnableLinOp<Coo>(exec, size),
values_{exec, std::move(values)},
col_idxs_{exec, std::move(col_idxs)},
row_idxs_{exec, std::move(row_idxs)}
{
GKO_ASSERT_EQ(values_.get_size(), col_idxs_.get_size());
GKO_ASSERT_EQ(values_.get_size(), row_idxs_.get_size());
}
template <typename ValueType, typename IndexType>
LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> b,
ptr_param<LinOp> x)
{
this->validate_application_parameters(b.get(), x.get());
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}
template <typename ValueType, typename IndexType>
const LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> b,
ptr_param<LinOp> x) const
{
this->validate_application_parameters(b.get(), x.get());
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}
template <typename ValueType, typename IndexType>
LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> alpha,
ptr_param<const LinOp> b,
ptr_param<LinOp> x)
{
this->validate_application_parameters(b.get(), x.get());
GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, alpha).get(),
make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}
template <typename ValueType, typename IndexType>
const LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> alpha,
ptr_param<const LinOp> b,
ptr_param<LinOp> x) const
{
this->validate_application_parameters(b.get(), x.get());
GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, alpha).get(),
make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::apply_impl(const LinOp* b, LinOp* x) const
{
precision_dispatch_real_complex<ValueType>(
[this](auto dense_b, auto dense_x) {
this->get_executor()->run(coo::make_spmv(this, dense_b, dense_x));
},
b, x);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::apply_impl(const LinOp* alpha, const LinOp* b,
const LinOp* beta, LinOp* x) const
{
precision_dispatch_real_complex<ValueType>(
[this](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) {
this->get_executor()->run(coo::make_advanced_spmv(
dense_alpha, this, dense_b, dense_beta, dense_x));
},
alpha, b, beta, x);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::apply2_impl(const LinOp* b, LinOp* x) const
{
precision_dispatch_real_complex<ValueType>(
[this](auto dense_b, auto dense_x) {
this->get_executor()->run(coo::make_spmv2(this, dense_b, dense_x));
},
b, x);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::apply2_impl(const LinOp* alpha, const LinOp* b,
LinOp* x) const
{
precision_dispatch_real_complex<ValueType>(
[this](auto dense_alpha, auto dense_b, auto dense_x) {
this->get_executor()->run(
coo::make_advanced_spmv2(dense_alpha, this, dense_b, dense_x));
},
alpha, b, x);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::convert_to(
Coo<next_precision<ValueType>, IndexType>* result) const
{
result->values_ = this->values_;
result->row_idxs_ = this->row_idxs_;
result->col_idxs_ = this->col_idxs_;
result->set_size(this->get_size());
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::move_to(
Coo<next_precision<ValueType>, IndexType>* result)
{
this->convert_to(result);
}
#if GINKGO_ENABLE_HALF
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::convert_to(
Coo<next_precision<next_precision<ValueType>>, IndexType>* result) const
{
result->values_ = this->values_;
result->row_idxs_ = this->row_idxs_;
result->col_idxs_ = this->col_idxs_;
result->set_size(this->get_size());
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::move_to(
Coo<next_precision<next_precision<ValueType>>, IndexType>* result)
{
this->convert_to(result);
}
#endif
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::convert_to(
Csr<ValueType, IndexType>* result) const
{
auto exec = this->get_executor();
result->set_size(this->get_size());
result->row_ptrs_.resize_and_reset(this->get_size()[0] + 1);
result->col_idxs_ = this->col_idxs_;
result->values_ = this->values_;
exec->run(coo::make_convert_idxs_to_ptrs(
this->get_const_row_idxs(), this->get_num_stored_elements(),
this->get_size()[0],
make_temporary_clone(exec, &result->row_ptrs_)->get_data()));
result->make_srow();
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::move_to(Csr<ValueType, IndexType>* result)
{
auto exec = this->get_executor();
const auto nnz = this->get_num_stored_elements();
result->set_size(this->get_size());
result->row_ptrs_.resize_and_reset(this->get_size()[0] + 1);
result->col_idxs_ = std::move(this->col_idxs_);
result->values_ = std::move(this->values_);
exec->run(coo::make_convert_idxs_to_ptrs(
this->get_const_row_idxs(), nnz, this->get_size()[0],
make_temporary_clone(exec, &result->row_ptrs_)->get_data()));
result->make_srow();
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::convert_to(Dense<ValueType>* result) const
{
auto exec = this->get_executor();
auto tmp_result = make_temporary_output_clone(exec, result);
tmp_result->resize(this->get_size());
tmp_result->fill(zero<ValueType>());
exec->run(coo::make_fill_in_dense(this, tmp_result.get()));
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::move_to(Dense<ValueType>* result)
{
this->convert_to(result);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::resize(dim<2> new_size, size_type nnz)
{
this->set_size(new_size);
this->row_idxs_.resize_and_reset(nnz);
this->col_idxs_.resize_and_reset(nnz);
this->values_.resize_and_reset(nnz);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::read(const mat_data& data)
{
auto size = data.size;
auto exec = this->get_executor();
this->set_size(size);
this->row_idxs_.resize_and_reset(data.nonzeros.size());
this->col_idxs_.resize_and_reset(data.nonzeros.size());
this->values_.resize_and_reset(data.nonzeros.size());
device_mat_data view{exec, size, this->row_idxs_.as_view(),
this->col_idxs_.as_view(), this->values_.as_view()};
const auto host_data =
make_array_view(exec->get_master(), data.nonzeros.size(),
const_cast<matrix_data_entry<ValueType, IndexType>*>(
data.nonzeros.data()));
exec->run(
coo::make_aos_to_soa(*make_temporary_clone(exec, &host_data), view));
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::read(const device_mat_data& data)
{
this->set_size(data.get_size());
// copy the arrays from device matrix data into the arrays of
// this. Compared to the read(device_mat_data&&) version, the internal
// arrays keep their current ownership status
this->values_ = make_const_array_view(data.get_executor(),
data.get_num_stored_elements(),
data.get_const_values());
this->col_idxs_ = make_const_array_view(data.get_executor(),
data.get_num_stored_elements(),
data.get_const_col_idxs());
this->row_idxs_ = make_const_array_view(data.get_executor(),
data.get_num_stored_elements(),
data.get_const_row_idxs());
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::read(device_mat_data&& data)
{
this->set_size(data.get_size());
auto arrays = data.empty_out();
this->values_ = std::move(arrays.values);
this->col_idxs_ = std::move(arrays.col_idxs);
this->row_idxs_ = std::move(arrays.row_idxs);
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::write(mat_data& data) const
{
auto tmp = make_temporary_clone(this->get_executor()->get_master(), this);
data = {this->get_size(), {}};
for (size_type i = 0; i < tmp->get_num_stored_elements(); ++i) {
const auto row = tmp->row_idxs_.get_const_data()[i];
const auto col = tmp->col_idxs_.get_const_data()[i];
const auto val = tmp->values_.get_const_data()[i];
data.nonzeros.emplace_back(row, col, val);
}
}
template <typename ValueType, typename IndexType>
std::unique_ptr<Diagonal<ValueType>>
Coo<ValueType, IndexType>::extract_diagonal() const
{
auto exec = this->get_executor();
const auto diag_size = std::min(this->get_size()[0], this->get_size()[1]);
auto diag = Diagonal<ValueType>::create(exec, diag_size);
exec->run(coo::make_fill_array(diag->get_values(), diag->get_size()[0],
zero<ValueType>()));
exec->run(coo::make_extract_diagonal(this, diag.get()));
return diag;
}
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::compute_absolute_inplace()
{
auto exec = this->get_executor();
exec->run(coo::make_inplace_absolute_array(
this->get_values(), this->get_num_stored_elements()));
}
template <typename ValueType, typename IndexType>
std::unique_ptr<typename Coo<ValueType, IndexType>::absolute_type>
Coo<ValueType, IndexType>::compute_absolute() const
{
auto exec = this->get_executor();
auto abs_coo = absolute_type::create(exec, this->get_size(),
this->get_num_stored_elements());
abs_coo->col_idxs_ = col_idxs_;
abs_coo->row_idxs_ = row_idxs_;
exec->run(coo::make_outplace_absolute_array(this->get_const_values(),
this->get_num_stored_elements(),
abs_coo->get_values()));
return abs_coo;
}
#define GKO_DECLARE_COO_MATRIX(ValueType, IndexType) \
class Coo<ValueType, IndexType>
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_COO_MATRIX);
} // namespace matrix
} // namespace gko