1#ifndef TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
2#define TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
11#include "convert_to_sparse_utils.hpp"
30template<
typename Value_,
typename Index_,
typename Count_>
31void count_compressed_sparse_non_zeros_consistent(
34 const Index_ secondary,
39 sanisizer::cast<Count_>(secondary);
43 opt.sparse_extract_value =
false;
44 opt.sparse_extract_index =
false;
45 opt.sparse_ordered_index =
false;
47 parallelize([&](
const int,
const Index_ start,
const Index_ length) ->
void {
49 for (Index_ x = 0; x < length; ++x) {
50 const auto range = wrk->fetch(NULL, NULL);
51 output[start + x] = range.number;
56 parallelize([&](
const int,
const Index_ start,
const Index_ length) ->
void {
59 for (Index_ p = start, pe = start + length; p < pe; ++p) {
60 const auto ptr = wrk->fetch(buffer_v.data());
62 for (Index_ s = 0; s < secondary; ++s) {
63 count += (ptr[s] != 0);
74struct CountCompressedSparseNonZerosOptions {
79template<
typename Value_,
typename Index_,
typename Count_>
80void count_compressed_sparse_non_zeros(
84 const CountCompressedSparseNonZerosOptions& options
86 const Index_ NR = matrix.
nrow();
87 const Index_ NC = matrix.
ncol();
88 const Index_ primary = (row ? NR : NC);
89 const Index_ secondary = (row ? NC : NR);
92 count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output, options.num_threads);
94 std::fill_n(output, primary, 0);
95 count_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output, options.num_threads);
99template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
100void fill_compressed_sparse_matrix_consistent(
102 const InputIndex_ primary,
103 const InputIndex_ secondary,
105 const Pointer_*
const pointers,
106 StoredValue_*
const output_value,
107 StoredIndex_*
const output_index,
112 opt.sparse_ordered_index =
false;
114 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
119 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
125 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
126 const auto offset = pointers[p];
127 std::copy_n(range.value, range.number, output_value + offset);
128 std::copy_n(range.index, range.number, output_index + offset);
130 }, primary, threads);
133 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
137 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
138 const auto ptr = wrk->fetch(buffer_v.data());
139 auto offset = pointers[p];
140 for (InputIndex_ s = 0; s < secondary; ++s) {
141 const auto val = ptr[s];
143 output_value[offset] = val;
144 output_index[offset] = s;
149 }, primary, threads);
153template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
154void fill_compressed_sparse_matrix_inconsistent(
156 const InputIndex_ primary,
157 const InputIndex_ secondary,
159 const Pointer_*
const output_ptrs,
160 StoredValue_*
const output_value,
161 StoredIndex_*
const output_index,
162 std::optional<CountNonZerosPerThread<InputIndex_, Pointer_> >& per_thread
164 const bool is_sparse = matrix.
is_sparse();
165 if (per_thread.has_value()) {
167 auto& offsets = per_thread->counts;
168 Pointer_ accumulant = 0;
169 static_assert(std::is_same<I<
decltype(per_thread->counts[0][0])>, Pointer_>::value);
170 for (InputIndex_ i = 0; i < primary; ++i) {
171 for (
auto& pt : offsets) {
172 const auto count = pt[i];
178 parallelize([&](
const int,
const int th_start,
const int th_length) ->
void {
179 for (
int t = 0; t < th_length; ++t) {
180 auto& offsets = (per_thread->counts)[t + th_start];
181 const auto actual_start = (per_thread->starts)[t + th_start];
182 const auto actual_length = (per_thread->lengths)[t + th_start];
191 opt.sparse_ordered_index =
false;
196 for (InputIndex_ x = 0; x < actual_length; ++x) {
197 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
198 for (InputIndex_ i = 0; i < range.number; ++i) {
199 auto& pos = offsets[range.index[i]];
200 output_value[pos] = range.value[i];
201 output_index[pos] = x + actual_start;
209 for (InputIndex_ x = 0; x < actual_length; ++x) {
210 const auto ptr = wrk->fetch(buffer_v.data());
211 for (InputIndex_ p = 0; p < primary; ++p) {
212 const auto val = ptr[p];
214 auto& pos = offsets[p];
215 output_value[pos] = val;
216 output_index[pos] = x + actual_start;
223 }, per_thread->counts.size(), per_thread->counts.size());
226 std::vector<Pointer_> offsets(output_ptrs, output_ptrs + primary);
230 opt.sparse_ordered_index =
false;
234 for (InputIndex_ s = 0; s < secondary; ++s) {
235 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
236 for (InputIndex_ i = 0; i < range.number; ++i) {
237 auto& pos = offsets[range.index[i]];
238 output_value[pos] = range.value[i];
239 output_index[pos] = s;
247 for (InputIndex_ s = 0; s < secondary; ++s) {
248 const auto ptr = wrk->fetch(buffer_v.data());
249 for (InputIndex_ p = 0; p < primary; ++p) {
250 const auto val = ptr[p];
252 auto& pos = offsets[p];
253 output_value[pos] = val;
254 output_index[pos] = s;
265struct FillCompressedSparseContentsOptions {
270template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
271void fill_compressed_sparse_contents(
274 const Pointer_*
const pointers,
275 StoredValue_*
const output_value,
276 StoredIndex_*
const output_index,
277 const FillCompressedSparseContentsOptions& options
279 const InputIndex_ NR = matrix.
nrow();
280 const InputIndex_ NC = matrix.
ncol();
281 const InputIndex_ primary = (row ? NR : NC);
282 const InputIndex_ secondary = (row ? NC : NR);
285 fill_compressed_sparse_matrix_consistent(matrix, primary, secondary, row, pointers, output_value, output_index, options.num_threads);
287 std::optional<CountNonZerosPerThread<InputIndex_, Pointer_> > empty;
288 fill_compressed_sparse_matrix_inconsistent(
314template<
typename Value_,
typename Index_,
typename Po
inter_>
366template<
typename StoredValue_,
typename StoredIndex_,
typename StoredPo
inter_ = std::
size_t,
typename InputValue_,
typename InputIndex_>
376 auto& output_v = output.
value;
377 auto& output_i = output.
index;
380 const InputIndex_ NR = matrix.
nrow();
381 const InputIndex_ NC = matrix.
ncol();
382 const InputIndex_ primary = (row ? NR : NC);
383 const InputIndex_ secondary = (row ? NC : NR);
385 output_p.resize(sanisizer::sum<I<
decltype(output_p.size())> >(attest_for_Index(primary), 1));
389 std::vector<std::vector<InputValue_> > store_v;
390 std::vector<std::vector<InputIndex_> > store_i;
391 auto original_ranges = extract_sparse_matrix(matrix, store_v, store_i, options.
num_threads);
394 if (use_rows == row) {
396 for (InputIndex_ p = 0; p < primary; ++p) {
397 output_p[p + 1] = sanisizer::sum<StoredPointer_>(output_p[p], original_ranges[p].number);
400 output_v.reserve(output_p.back());
401 output_i.reserve(output_p.back());
402 for (InputIndex_ p = 0; p < primary; ++p) {
403 output_v.insert(output_v.end(), original_ranges[p].value, original_ranges[p].value + original_ranges[p].number);
404 output_i.insert(output_i.end(), original_ranges[p].index, original_ranges[p].index + original_ranges[p].number);
409 for (InputIndex_ s = 0; s < secondary; ++s) {
410 const auto& range = original_ranges[s];
411 for (InputIndex_ x = 0; x < range.number; ++x) {
412 output_p[range.index[x] + 1] += 1;
415 for (InputIndex_ p = 0; p < primary; ++p) {
416 output_p[p + 1] = sanisizer::sum<StoredPointer_>(output_p[p + 1], output_p[p]);
419 sanisizer::resize(output_v, output_p.back());
420 sanisizer::resize(output_i, output_p.back());
421 std::vector<StoredPointer_> offsets(output_p.begin(), output_p.begin() + primary);
422 for (InputIndex_ s = 0; s < secondary; ++s) {
423 const auto& range = original_ranges[s];
424 for (InputIndex_ i = 0; i < range.number; ++i) {
425 auto& pos = offsets[range.index[i]];
426 output_v[pos] = range.value[i];
435 count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output_p.data() + 1, options.
num_threads);
436 for (InputIndex_ i = 1; i <= primary; ++i) {
437 output_p[i] = sanisizer::sum<StoredPointer_>(output_p[i], output_p[i - 1]);
441 sanisizer::resize(output_v, output_p.back());
442 sanisizer::resize(output_i, output_p.back());
443 fill_compressed_sparse_matrix_consistent(
456 auto per_thread = count_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output_p.data() + 1, options.
num_threads);
457 for (InputIndex_ i = 1; i <= primary; ++i) {
458 output_p[i] = sanisizer::sum<StoredPointer_>(output_p[i], output_p[i - 1]);
462 sanisizer::resize(output_v, output_p.back());
463 sanisizer::resize(output_i, output_p.back());
464 fill_compressed_sparse_matrix_inconsistent(
516 typename StoredValue_ = Value_,
517 typename StoredIndex_ = Index_,
518 typename StoredPointer_ = std::size_t,
519 typename InputValue_,
537 return std::shared_ptr<Matrix<Value_, Index_> >(
541 std::vector<StoredValue_>,
542 std::vector<StoredIndex_>,
543 std::vector<StoredPointer_>
547 std::move(comp.value),
548 std::move(comp.index),
549 std::move(comp.pointers),
552 CompressedSparseMatrixOptions copt;
564template<
typename Value_,
typename Index_,
typename Count_>
566 return count_compressed_sparse_non_zeros(
571 CountCompressedSparseNonZerosOptions copt;
572 copt.num_threads = threads;
578template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
581 const Pointer_* pointers,
582 StoredValue_* output_value,
583 StoredIndex_* output_index,
586 fill_compressed_sparse_contents(
593 FillCompressedSparseContentsOptions fopt;
594 fopt.num_threads = threads;
600template<
typename StoredValue_,
typename StoredIndex_,
typename StoredPo
inter_ = std::
size_t,
typename InputValue_,
typename InputIndex_>
601CompressedSparseContents<StoredValue_, StoredIndex_, StoredPointer_>
retrieve_compressed_sparse_contents(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
bool two_pass,
int threads = 1) {
606 RetrieveCompressedSparseContentsOptions opt;
607 opt.two_pass = two_pass;
608 opt.num_threads = threads;
614template<
typename Value_ =
double,
typename Index_ =
int,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
615std::shared_ptr<Matrix<Value_, Index_> >
convert_to_compressed_sparse(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
bool two_pass =
false,
int threads = 1) {
620 ConvertToCompressedSparseOptions opt;
621 opt.two_pass = two_pass;
622 opt.num_threads = threads;
628template <
bool row_,
typename Value_,
typename Index_,
typename InputValue_,
typename InputIndex_>
633template <
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
634std::shared_ptr<Matrix<Value_, Index_> >
convert_to_compressed_sparse(
const Matrix<InputValue_, InputIndex_>* matrix,
bool two_pass =
false,
int threads = 1) {
Compressed sparse matrix representation.
Iterate across consecutive elements of the target dimension.
Convert index type to container size.
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:645
Virtual class for a matrix.
Definition Matrix.hpp:59
virtual Index_ ncol() const =0
virtual Index_ nrow() const =0
virtual bool prefer_rows() const =0
virtual bool is_sparse() const =0
Convert a matrix into a fragmented sparse format.
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< ConsecutiveOracle< Index_ > > make_ConsecutiveOracle(Index_ start, Index_ length)
Definition ConsecutiveOracle.hpp:56
CompressedSparseContents< StoredValue_, StoredIndex_, StoredPointer_ > retrieve_compressed_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const RetrieveCompressedSparseContentsOptions &options)
Definition convert_to_compressed_sparse.hpp:367
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Definition parallelize.hpp:58
std::unique_ptr< Extractor< sparse_, oracle_, Value_, Index_ > > make_Extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, const tatami::Options &opt)
Definition make_Extractor.hpp:69
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:68
std::shared_ptr< Matrix< Value_, Index_ > > convert_to_compressed_sparse(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const ConvertToCompressedSparseOptions &options)
Definition convert_to_compressed_sparse.hpp:522
Parallelized iteration over a tatami::Matrix.
Compressed sparse contents.
Definition convert_to_compressed_sparse.hpp:315
std::vector< Index_ > index
Definition convert_to_compressed_sparse.hpp:324
std::vector< Value_ > value
Definition convert_to_compressed_sparse.hpp:319
std::vector< Pointer_ > pointers
Definition convert_to_compressed_sparse.hpp:329
Options for convert_to_compressed_sparse().
Definition convert_to_compressed_sparse.hpp:482
bool two_pass
Definition convert_to_compressed_sparse.hpp:488
int num_threads
Definition convert_to_compressed_sparse.hpp:493
Options for retrieve_compressed_sparse_contents().
Definition convert_to_compressed_sparse.hpp:335
int num_threads
Definition convert_to_compressed_sparse.hpp:346
bool two_pass
Definition convert_to_compressed_sparse.hpp:341