1#ifndef TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
2#define TATAMI_CONVERT_TO_COMPRESSED_SPARSE_H
24namespace convert_to_compressed_sparse_internal {
26template<
typename Value_,
typename Index_,
typename Count_>
27void count_compressed_sparse_non_zeros_consistent(
const tatami::Matrix<Value_, Index_>& matrix, Index_ primary, Index_ secondary,
bool row, Count_* output,
int threads) {
30 opt.sparse_extract_value =
false;
31 opt.sparse_extract_index =
false;
32 opt.sparse_ordered_index =
false;
34 parallelize([&](
int, Index_ start, Index_ length) ->
void {
35 auto wrk = consecutive_extractor<true>(matrix, row, start, length, opt);
36 for (Index_ x = 0; x < length; ++x) {
37 auto range = wrk->fetch(NULL, NULL);
38 output[start + x] = range.number;
43 parallelize([&](
int, Index_ start, Index_ length) ->
void {
44 std::vector<Value_> buffer_v(secondary);
45 auto wrk = consecutive_extractor<false>(matrix, row, start, length);
46 for (Index_ p = start, pe = start + length; p < pe; ++p) {
47 auto ptr = wrk->fetch(buffer_v.data());
49 for (Index_ s = 0; s < secondary; ++s) {
50 count += (ptr[s] != 0);
58template<
typename Value_,
typename Index_,
typename Count_>
59void count_compressed_sparse_non_zeros_inconsistent(
const tatami::Matrix<Value_, Index_>& matrix, Index_ primary, Index_ secondary,
bool row, Count_* output,
int threads) {
60 std::vector<std::vector<Count_> > nz_counts(threads - 1);
61 for (
auto& x : nz_counts) {
67 opt.sparse_extract_value =
false;
68 opt.sparse_ordered_index =
false;
70 parallelize([&](
int t, Index_ start, Index_ length) ->
void {
71 std::vector<Index_> buffer_i(primary);
72 auto wrk = consecutive_extractor<true>(matrix, !row, start, length, opt);
73 auto my_counts = (t > 0 ? nz_counts[t - 1].data() : output);
75 for (Index_ x = 0; x < length; ++x) {
76 auto range = wrk->fetch(NULL, buffer_i.data());
77 for (Index_ i = 0; i < range.number; ++i) {
78 ++my_counts[range.index[i]];
81 }, secondary, threads);
84 parallelize([&](
int t, Index_ start, Index_ length) ->
void {
85 auto wrk = consecutive_extractor<false>(matrix, !row, start, length);
86 std::vector<Value_> buffer_v(primary);
87 auto my_counts = (t > 0 ? nz_counts[t - 1].data() : output);
89 for (Index_ x = 0; x < length; ++x) {
90 auto ptr = wrk->fetch(buffer_v.data());
91 for (Index_ p = 0; p < primary; ++p) {
92 my_counts[p] += (ptr[p] != 0);
95 }, secondary, threads);
98 for (
auto& y : nz_counts) {
99 for (Index_ p = 0; p < primary; ++p) {
105template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
106void fill_compressed_sparse_matrix_consistent(
109 InputIndex_ secondary,
111 const Pointer_* pointers,
112 StoredValue_* output_value,
113 StoredIndex_* output_index,
118 opt.sparse_ordered_index =
false;
120 parallelize([&](
int, InputIndex_ start, InputIndex_ length) ->
void {
121 std::vector<InputValue_> buffer_v(secondary);
122 std::vector<InputIndex_> buffer_i(secondary);
123 auto wrk = consecutive_extractor<true>(matrix, row, start, length, opt);
125 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
131 auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
132 auto offset = pointers[p];
133 std::copy_n(range.value, range.number, output_value + offset);
134 std::copy_n(range.index, range.number, output_index + offset);
136 }, primary, threads);
139 parallelize([&](
int, InputIndex_ start, InputIndex_ length) ->
void {
140 std::vector<InputValue_> buffer_v(secondary);
141 auto wrk = consecutive_extractor<false>(matrix, row, start, length);
143 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
144 auto ptr = wrk->fetch(buffer_v.data());
145 auto offset = pointers[p];
146 for (InputIndex_ s = 0; s < secondary; ++s) {
149 output_value[offset] = val;
150 output_index[offset] = s;
155 }, primary, threads);
159template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
160void fill_compressed_sparse_matrix_inconsistent(
163 InputIndex_ secondary,
165 const Pointer_* pointers,
166 StoredValue_* output_value,
167 StoredIndex_* output_index,
172 opt.sparse_ordered_index =
false;
174 parallelize([&](
int, InputIndex_ start, InputIndex_ length) ->
void {
175 std::vector<InputValue_> buffer_v(length);
176 std::vector<InputIndex_> buffer_i(length);
177 auto wrk = consecutive_extractor<true>(matrix, !row,
static_cast<InputIndex_
>(0), secondary, start, length, opt);
178 std::vector<Pointer_> offset_copy(pointers + start, pointers + start + length);
180 for (InputIndex_ x = 0; x < secondary; ++x) {
181 auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
182 for (InputIndex_ i = 0; i < range.number; ++i) {
183 auto& pos = offset_copy[range.index[i] - start];
184 output_value[pos] = range.value[i];
185 output_index[pos] = x;
189 }, primary, threads);
192 parallelize([&](
int, InputIndex_ start, InputIndex_ length) ->
void {
193 std::vector<InputValue_> buffer_v(length);
194 auto wrk = consecutive_extractor<false>(matrix, !row,
static_cast<InputIndex_
>(0), secondary, start, length);
195 std::vector<Pointer_> offset_copy(pointers + start, pointers + start + length);
197 for (InputIndex_ x = 0; x < secondary; ++x) {
198 auto ptr = wrk->fetch(buffer_v.data());
199 for (InputIndex_ p = 0; p < length; ++p) {
202 auto& pos = offset_copy[p];
203 output_value[pos] = val;
204 output_index[pos] = x;
209 }, primary, threads);
243template<
typename Value_,
typename Index_,
typename Count_>
245 Index_ NR = matrix.
nrow();
246 Index_ NC = matrix.
ncol();
247 Index_ primary = (row ? NR : NC);
248 Index_ secondary = (row ? NC : NR);
249 std::fill_n(output, primary, 0);
252 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output, options.
num_threads);
254 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output, options.
num_threads);
287template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
291 const Pointer_* pointers,
292 StoredValue_* output_value,
293 StoredIndex_* output_index,
296 InputIndex_ NR = matrix.
nrow();
297 InputIndex_ NC = matrix.
ncol();
298 InputIndex_ primary = (row ? NR : NC);
299 InputIndex_ secondary = (row ? NC : NR);
302 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_consistent(matrix, primary, secondary, row, pointers, output_value, output_index, options.
num_threads);
304 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_inconsistent(matrix, primary, secondary, row, pointers, output_value, output_index, options.
num_threads);
318template<
typename Value_,
typename Index_,
typename Po
inter_>
369template<
typename StoredValue_,
typename StoredIndex_,
typename StoredPo
inter_ = std::
size_t,
typename InputValue_,
typename InputIndex_>
378 auto& output_v = output.
value;
379 auto& output_i = output.
index;
382 InputIndex_ NR = matrix.
nrow();
383 InputIndex_ NC = matrix.
ncol();
384 InputIndex_ primary = (row ? NR : NC);
385 InputIndex_ secondary = (row ? NC : NR);
398 const auto& store_v = frag.value;
399 const auto& store_i = frag.index;
401 output_p.resize(
static_cast<std::size_t
>(primary) + 1);
402 for (InputIndex_ p = 0; p < primary; ++p) {
403 output_p[p + 1] = output_p[p] + store_v[p].size();
406 output_v.reserve(output_p.back());
407 output_i.reserve(output_p.back());
408 for (InputIndex_ p = 0; p < primary; ++p) {
409 output_v.insert(output_v.end(), store_v[p].begin(), store_v[p].end());
410 output_i.insert(output_i.end(), store_i[p].begin(), store_i[p].end());
415 output_p.resize(
static_cast<std::size_t
>(primary) + 1);
416 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_consistent(matrix, primary, secondary, row, output_p.data() + 1, options.
num_threads);
417 for (InputIndex_ i = 1; i <= primary; ++i) {
418 output_p[i] += output_p[i - 1];
422 output_v.resize(output_p.back());
423 output_i.resize(output_p.back());
424 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_consistent(
437 output_p.resize(
static_cast<std::size_t
>(primary) + 1);
438 convert_to_compressed_sparse_internal::count_compressed_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, output_p.data() + 1, options.
num_threads);
439 for (InputIndex_ i = 1; i <= primary; ++i) {
440 output_p[i] += output_p[i - 1];
444 output_v.resize(output_p.back());
445 output_i.resize(output_p.back());
446 convert_to_compressed_sparse_internal::fill_compressed_sparse_matrix_inconsistent(
497 typename StoredValue_ = Value_,
498 typename StoredIndex_ = Index_,
499 typename StoredPointer_ = std::size_t,
500 typename InputValue_,
514 return std::shared_ptr<Matrix<Value_, Index_> >(
518 std::vector<StoredValue_>,
519 std::vector<StoredIndex_>,
520 std::vector<StoredPointer_>
524 std::move(comp.value),
525 std::move(comp.index),
526 std::move(comp.pointers),
529 CompressedSparseMatrixOptions copt;
541template<
typename Value_,
typename Index_,
typename Count_>
548 CountCompressedSparseNonZerosOptions copt;
549 copt.num_threads = threads;
555template<
typename InputValue_,
typename InputIndex_,
typename Po
inter_,
typename StoredValue_,
typename StoredIndex_>
558 const Pointer_* pointers,
559 StoredValue_* output_value,
560 StoredIndex_* output_index,
570 FillCompressedSparseContentsOptions fopt;
571 fopt.num_threads = threads;
577template<
typename StoredValue_,
typename StoredIndex_,
typename StoredPo
inter_ = std::
size_t,
typename InputValue_,
typename InputIndex_>
578CompressedSparseContents<StoredValue_, StoredIndex_, StoredPointer_>
retrieve_compressed_sparse_contents(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
bool two_pass,
int threads = 1) {
583 RetrieveCompressedSparseContentsOptions opt;
584 opt.two_pass = two_pass;
585 opt.num_threads = threads;
591template<
typename Value_ =
double,
typename Index_ =
int,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
592std::shared_ptr<Matrix<Value_, Index_> >
convert_to_compressed_sparse(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
bool two_pass =
false,
int threads = 1) {
597 ConvertToCompressedSparseOptions opt;
598 opt.two_pass = two_pass;
599 opt.num_threads = threads;
605template <
bool row_,
typename Value_,
typename Index_,
typename InputValue_,
typename InputIndex_>
610template <
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
611std::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.
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:503
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.
Flexible representations for matrix data.
Definition Extractor.hpp:15
CompressedSparseContents< StoredValue_, StoredIndex_, StoredPointer_ > retrieve_compressed_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, bool row, const RetrieveCompressedSparseContentsOptions &options)
Definition convert_to_compressed_sparse.hpp:370
std::shared_ptr< Matrix< Value_, Index_ > > convert_to_compressed_sparse(const Matrix< InputValue_, InputIndex_ > &matrix, bool row, const ConvertToCompressedSparseOptions &options)
Definition convert_to_compressed_sparse.hpp:503
void parallelize(Function_ fun, Index_ tasks, int threads)
Definition parallelize.hpp:42
FragmentedSparseContents< StoredValue_, StoredIndex_ > retrieve_fragmented_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, bool row, const RetrieveFragmentedSparseContentsOptions &options)
Definition convert_to_fragmented_sparse.hpp:77
void fill_compressed_sparse_contents(const tatami::Matrix< InputValue_, InputIndex_ > &matrix, bool row, const Pointer_ *pointers, StoredValue_ *output_value, StoredIndex_ *output_index, const FillCompressedSparseContentsOptions &options)
Definition convert_to_compressed_sparse.hpp:288
void count_compressed_sparse_non_zeros(const tatami::Matrix< Value_, Index_ > &matrix, bool row, Count_ *output, const CountCompressedSparseNonZerosOptions &options)
Definition convert_to_compressed_sparse.hpp:244
Parallelized iteration over a tatami::Matrix.
Compressed sparse contents.
Definition convert_to_compressed_sparse.hpp:319
std::vector< Index_ > index
Definition convert_to_compressed_sparse.hpp:328
std::vector< Value_ > value
Definition convert_to_compressed_sparse.hpp:323
std::vector< Pointer_ > pointers
Definition convert_to_compressed_sparse.hpp:333
Options for convert_to_compressed_sparse().
Definition convert_to_compressed_sparse.hpp:464
bool two_pass
Definition convert_to_compressed_sparse.hpp:469
int num_threads
Definition convert_to_compressed_sparse.hpp:474
Options for count_compressed_sparse_non_zeros().
Definition convert_to_compressed_sparse.hpp:221
int num_threads
Definition convert_to_compressed_sparse.hpp:225
Options for fill_compressed_sparse_contents().
Definition convert_to_compressed_sparse.hpp:261
int num_threads
Definition convert_to_compressed_sparse.hpp:265
Options for retrieve_compressed_sparse_contents().
Definition convert_to_compressed_sparse.hpp:339
int num_threads
Definition convert_to_compressed_sparse.hpp:349
bool two_pass
Definition convert_to_compressed_sparse.hpp:344
Options for retrieve_fragmented_sparse_contents().
Definition convert_to_fragmented_sparse.hpp:57
int num_threads
Definition convert_to_fragmented_sparse.hpp:61