tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
convert_to_compressed_sparse.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
2#define TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
3
6#include "../utils/parallelize.hpp"
7#include "../utils/consecutive_extractor.hpp"
8
9#include <memory>
10#include <vector>
11
18namespace tatami {
19
24
25template<typename Value_, typename Index_, typename Count_>
27 if (matrix->is_sparse()) {
28 Options opt;
29 opt.sparse_extract_value = false;
30 opt.sparse_extract_index = false;
31 opt.sparse_ordered_index = false;
32
33 parallelize([&](int, Index_ start, Index_ length) -> void {
35 for (Index_ x = 0; x < length; ++x) {
36 auto range = wrk->fetch(NULL, NULL);
37 output[start + x] = range.number;
38 }
39 }, primary, threads);
40
41 } else {
42 parallelize([&](int, Index_ start, Index_ length) -> void {
43 std::vector<Value_> buffer_v(secondary);
45 for (Index_ p = start, pe = start + length; p < pe; ++p) {
46 auto ptr = wrk->fetch(buffer_v.data());
47 Count_ count = 0;
48 for (Index_ s = 0; s < secondary; ++s) {
49 count += (ptr[s] != 0);
50 }
51 output[p] = count;
52 }
53 }, primary, threads);
54 }
55}
56
57template<typename Value_, typename Index_, typename Count_>
59 std::vector<std::vector<Count_> > nz_counts(threads - 1);
60 for (auto& x : nz_counts) {
61 x.resize(primary);
62 }
63
64 if (matrix->is_sparse()) {
65 Options opt;
66 opt.sparse_extract_value = false;
67 opt.sparse_ordered_index = false;
68
69 parallelize([&](int t, Index_ start, Index_ length) -> void {
70 std::vector<Index_> buffer_i(primary);
72 auto my_counts = (t > 0 ? nz_counts[t - 1].data() : output);
73
74 for (Index_ x = 0; x < length; ++x) {
75 auto range = wrk->fetch(NULL, buffer_i.data());
76 for (Index_ i = 0; i < range.number; ++i) {
77 ++my_counts[range.index[i]];
78 }
79 }
80 }, secondary, threads);
81
82 } else {
83 parallelize([&](int t, Index_ start, Index_ length) -> void {
85 std::vector<Value_> buffer_v(primary);
86 auto my_counts = (t > 0 ? nz_counts[t - 1].data() : output);
87
88 for (Index_ x = 0; x < length; ++x) {
89 auto ptr = wrk->fetch(buffer_v.data());
90 for (Index_ p = 0; p < primary; ++p) {
91 my_counts[p] += (ptr[p] != 0);
92 }
93 }
94 }, secondary, threads);
95 }
96
97 for (auto& y : nz_counts) {
98 for (Index_ p = 0; p < primary; ++p) {
99 output[p] += y[p];
100 }
101 }
102}
103
104template<typename InputValue_, typename InputIndex_, typename Pointer_, typename StoredValue_, typename StoredIndex_>
107 InputIndex_ primary,
108 InputIndex_ secondary,
109 bool row,
110 const Pointer_* pointers,
113 int threads)
114{
115 if (matrix->is_sparse()) {
116 Options opt;
117 opt.sparse_ordered_index = false;
118
119 parallelize([&](int, InputIndex_ start, InputIndex_ length) -> void {
120 std::vector<InputValue_> buffer_v(secondary);
121 std::vector<InputIndex_> buffer_i(secondary);
123
124 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
125 // Resist the urge to `fetch()` straight into 'output_v'
126 // and 'output_i', as implementations may assume that they
127 // have the entire 'length' length to play with, and the
128 // output vectors only have whatever is allocated from the
129 // first pass (which might be nothing for an all-zero matrix).
130 auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
131 auto offset = pointers[p];
132 std::copy_n(range.value, range.number, output_value + offset);
133 std::copy_n(range.index, range.number, output_index + offset);
134 }
135 }, primary, threads);
136
137 } else {
138 parallelize([&](int, InputIndex_ start, InputIndex_ length) -> void {
139 std::vector<InputValue_> buffer_v(secondary);
141
142 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
143 auto ptr = wrk->fetch(buffer_v.data());
144 auto offset = pointers[p];
145 for (InputIndex_ s = 0; s < secondary; ++s) {
146 auto val = ptr[s];
147 if (val != 0) {
150 ++offset;
151 }
152 }
153 }
154 }, primary, threads);
155 }
156}
157
158template<typename InputValue_, typename InputIndex_, typename Pointer_, typename StoredValue_, typename StoredIndex_>
161 InputIndex_ primary,
162 InputIndex_ secondary,
163 bool row,
164 const Pointer_* pointers,
167 int threads)
168{
169 if (matrix->is_sparse()) {
170 Options opt;
171 opt.sparse_ordered_index = false;
172
173 parallelize([&](int, InputIndex_ start, InputIndex_ length) -> void {
174 std::vector<InputValue_> buffer_v(length);
175 std::vector<InputIndex_> buffer_i(length);
176 auto wrk = consecutive_extractor<true>(matrix, !row, static_cast<InputIndex_>(0), secondary, start, length, opt);
177 std::vector<Pointer_> offset_copy(pointers + start, pointers + start + length);
178
179 for (InputIndex_ x = 0; x < secondary; ++x) {
180 auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
181 for (InputIndex_ i = 0; i < range.number; ++i) {
182 auto& pos = offset_copy[range.index[i] - start];
183 output_value[pos] = range.value[i];
184 output_index[pos] = x;
185 ++pos;
186 }
187 }
188 }, primary, threads);
189
190 } else {
191 parallelize([&](int, InputIndex_ start, InputIndex_ length) -> void {
192 std::vector<InputValue_> buffer_v(length);
193 auto wrk = consecutive_extractor<false>(matrix, !row, static_cast<InputIndex_>(0), secondary, start, length);
194 std::vector<Pointer_> offset_copy(pointers + start, pointers + start + length);
195
196 for (InputIndex_ x = 0; x < secondary; ++x) {
197 auto ptr = wrk->fetch(buffer_v.data());
198 for (InputIndex_ p = 0; p < length; ++p) {
199 auto val = ptr[p];
200 if (val != 0) {
201 auto& pos = offset_copy[p];
203 output_index[pos] = x;
204 ++pos;
205 }
206 }
207 }
208 }, primary, threads);
209 }
210}
211
212}
232template<typename Value_, typename Index_, typename Count_>
234 Index_ NR = matrix->nrow();
235 Index_ NC = matrix->ncol();
236 Index_ primary = (row ? NR : NC);
237 Index_ secondary = (row ? NC : NR);
238 std::fill_n(output, primary, 0);
239
240 if (row == matrix->prefer_rows()) {
241 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output, threads);
242 } else {
243 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output, threads);
244 }
245}
246
266template<typename InputValue_, typename InputIndex_, typename Pointer_, typename StoredValue_, typename StoredIndex_>
269 bool row,
270 const Pointer_* pointers,
273 int threads)
274{
275 InputIndex_ NR = matrix->nrow();
276 InputIndex_ NC = matrix->ncol();
277 InputIndex_ primary = (row ? NR : NC);
278 InputIndex_ secondary = (row ? NC : NR);
279
280 if (row == matrix->prefer_rows()) {
281 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_consistent(matrix, primary, secondary, row, pointers, output_value, output_index, threads);
282 } else {
283 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_inconsistent(matrix, primary, secondary, row, pointers, output_value, output_index, threads);
284 }
285}
286
297template<typename Value_, typename Index_, typename Pointer_ = size_t>
302 std::vector<Value_> value;
303
307 std::vector<Index_> index;
308
312 std::vector<Pointer_> pointers;
313};
314
333template<typename StoredValue_, typename StoredIndex_, typename StoredPointer_ = size_t, typename InputValue_, typename InputIndex_>
336 auto& output_v = output.value;
337 auto& output_i = output.index;
338 auto& output_p = output.pointers;
339
340 InputIndex_ NR = matrix->nrow();
341 InputIndex_ NC = matrix->ncol();
342 InputIndex_ primary = (row ? NR : NC);
343 InputIndex_ secondary = (row ? NC : NR);
344
345 if (!two_pass) {
346 // Doing a single fragmented run and then concatenating everything together.
348 const auto& store_v = frag.value;
349 const auto& store_i = frag.index;
350
351 output_p.resize(static_cast<size_t>(primary) + 1);
352 for (InputIndex_ p = 0; p < primary; ++p) {
353 output_p[p + 1] = output_p[p] + store_v[p].size();
354 }
355
356 output_v.reserve(output_p.back());
357 output_i.reserve(output_p.back());
358 for (InputIndex_ p = 0; p < primary; ++p) {
359 output_v.insert(output_v.end(), store_v[p].begin(), store_v[p].end());
360 output_i.insert(output_i.end(), store_i[p].begin(), store_i[p].end());
361 }
362
363 } else if (row == matrix->prefer_rows()) {
364 // First pass to figure out how many non-zeros there are.
365 output_p.resize(static_cast<size_t>(primary) + 1);
366 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output_p.data() + 1, threads);
367 for (InputIndex_ i = 1; i <= primary; ++i) {
368 output_p[i] += output_p[i - 1];
369 }
370
371 // Second pass to actually fill our vectors.
372 output_v.resize(output_p.back());
373 output_i.resize(output_p.back());
374 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_consistent(
375 matrix,
376 primary,
377 secondary,
378 row,
379 output_p.data(),
380 output_v.data(),
381 output_i.data(),
382 threads
383 );
384
385 } else {
386 // First pass to figure out how many non-zeros there are.
387 output_p.resize(static_cast<size_t>(primary) + 1);
388 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output_p.data() + 1, threads);
389 for (InputIndex_ i = 1; i <= primary; ++i) {
390 output_p[i] += output_p[i - 1];
391 }
392
393 // Second pass to actually fill our vectors.
394 output_v.resize(output_p.back());
395 output_i.resize(output_p.back());
396 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_inconsistent(
397 matrix,
398 primary,
399 secondary,
400 row,
401 output_p.data(),
402 output_v.data(),
403 output_i.data(),
404 threads
405 );
406 }
407
408 return output;
409}
410
427template<
428 typename Value_ = double,
429 typename Index_ = int,
430 typename StoredValue_ = Value_,
431 typename StoredIndex_ = Index_,
432 typename InputValue_,
433 typename InputIndex_
434>
435std::shared_ptr<Matrix<Value_, Index_> > convert_to_compressed_sparse(const Matrix<InputValue_, InputIndex_>* matrix, bool row, bool two_pass = false, int threads = 1) {
437 return std::shared_ptr<Matrix<Value_, Index_> >(
439 Value_,
440 Index_,
441 std::vector<StoredValue_>,
442 std::vector<StoredIndex_>,
443 std::vector<size_t>
444 >(
445 matrix->nrow(),
446 matrix->ncol(),
447 std::move(comp.value),
448 std::move(comp.index),
449 std::move(comp.pointers),
450 row,
451 false // no need for checks, as we guarantee correctness.
452 )
453 );
454}
455
459// Backwards compatbility.
460template <bool row_, typename Value_, typename Index_, typename InputValue_, typename InputIndex_>
463}
464
465template <bool row_, typename Value_, typename Index_, typename StoredValue_ = Value_, typename StoredIndex_ = Index_, typename InputValue_, typename InputIndex_>
466std::shared_ptr<Matrix<Value_, Index_> > convert_to_compressed_sparse(const Matrix<InputValue_, InputIndex_>* matrix, bool two_pass = false, int threads = 1) {
468}
473}
474
475#endif
Compressed sparse matrix representation.
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:477
Virtual class for a matrix.
Definition Matrix.hpp:59
Convert a matrix into a fragmented sparse format.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< Matrix< Value_, Index_ > > convert_to_compressed_sparse(const Matrix< InputValue_, InputIndex_ > *matrix, bool row, bool two_pass=false, int threads=1)
Definition convert_to_compressed_sparse.hpp:435
void parallelize(Function_ fun, Index_ tasks, int threads)
Definition parallelize.hpp:42
CompressedSparseContents< StoredValue_, StoredIndex_, StoredPointer_ > retrieve_compressed_sparse_contents(const Matrix< InputValue_, InputIndex_ > *matrix, bool row, bool two_pass, int threads=1)
Definition convert_to_compressed_sparse.hpp:334
void fill_compressed_sparse_contents(const tatami::Matrix< InputValue_, InputIndex_ > *matrix, bool row, const Pointer_ *pointers, StoredValue_ *output_value, StoredIndex_ *output_index, int threads)
Definition convert_to_compressed_sparse.hpp:267
void count_compressed_sparse_non_zeros(const tatami::Matrix< Value_, Index_ > *matrix, bool row, Count_ *output, int threads)
Definition convert_to_compressed_sparse.hpp:233
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Compressed sparse contents.
Definition convert_to_compressed_sparse.hpp:298
std::vector< Index_ > index
Definition convert_to_compressed_sparse.hpp:307
std::vector< Value_ > value
Definition convert_to_compressed_sparse.hpp:302
std::vector< Pointer_ > pointers
Definition convert_to_compressed_sparse.hpp:312