1#ifndef TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
2#define TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
10#include "convert_to_sparse_utils.hpp"
35template<
typename Value_,
typename Index_>
52 std::vector<std::vector<Value_> >
value;
59 std::vector<std::vector<Index_> >
index;
82template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
88 const InputIndex_ NR = matrix.
nrow();
89 const InputIndex_ NC = matrix.
ncol();
90 const InputIndex_ primary = (row ? NR : NC);
91 const InputIndex_ secondary = (row ? NC : NR);
94 auto& store_v = output.
value;
95 auto& store_i = output.
index;
98 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
103 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
104 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
105 auto& sv = store_v[p];
106 auto& si = store_i[p];
107 sv.reserve(range.number);
108 si.reserve(range.number);
111 for (InputIndex_ i = 0; i < range.number; ++i) {
112 sv.push_back(range.value[i]);
113 si.push_back(range.index[i]);
119 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
125 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
126 const auto ptr = wrk->fetch(buffer_v.data());
127 auto& sv = store_v[p];
128 auto& si = store_i[p];
130 for (InputIndex_ s = 0; s < secondary; ++s) {
131 const auto val = ptr[s];
159template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
166 return retrieve_fragmented_sparse_contents_consistent<StoredValue_, StoredIndex_>(matrix, row, options);
169 const InputIndex_ NR = matrix.
nrow();
170 const InputIndex_ NC = matrix.
ncol();
171 const InputIndex_ primary = (row ? NR : NC);
172 const InputIndex_ secondary = (row ? NC : NR);
177 auto tmp = retrieve_fragmented_sparse_contents_consistent<StoredValue_, StoredIndex_>(matrix, !row, options);
179 for (I<
decltype(secondary)> s = 0; s < secondary; ++s) {
180 const auto& sec_indices = tmp.index[s];
181 const auto num = sec_indices.size();
182 for (I<
decltype(num)> n = 0; n < num; ++n) {
183 primary_counts[sec_indices[n]] += 1;
188 for (InputIndex_ p = 0; p < primary; ++p) {
189 output.
index[p].reserve(primary_counts[p]);
190 output.
value[p].reserve(primary_counts[p]);
193 for (I<
decltype(secondary)> s = 0; s < secondary; ++s) {
194 const auto& sec_values = tmp.value[s];
195 const auto& sec_indices = tmp.index[s];
196 const auto num = sec_indices.size();
197 for (I<
decltype(num)> n = 0; n < num; ++n) {
198 const auto curp = sec_indices[n];
199 output.
value[curp].push_back(sec_values[n]);
200 output.
index[curp].push_back(s);
208 std::optional<std::vector<InputIndex_> > nnz_consistent;
210 count_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, nnz_inconsistent.data(), nnz_consistent, options.
num_threads);
213 for (InputIndex_ p = 0; p < primary; ++p) {
214 output.
index[p].reserve(nnz_inconsistent[p]);
215 output.
value[p].reserve(nnz_inconsistent[p]);
218 fill_sparse_matrix_inconsistent(
225 for (InputIndex_ i = 0; i < range.
number; ++i) {
230 [&](
const InputIndex_ s,
const InputValue_*
const ptr) ->
void {
231 for (InputIndex_ p = 0; p < primary; ++p) {
232 const auto val = ptr[p];
234 output.
value[p].push_back(val);
235 output.
index[p].push_back(s);
239 [&](
const InputIndex_ s,
const std::vector<InputValue_>& cur_values,
const std::vector<InputIndex_>& cur_primary_indices) {
240 const auto cur_count = cur_values.size();
241 for (I<
decltype(cur_count)> i = 0; i < cur_count; ++i) {
242 const auto primary = cur_primary_indices[i];
243 output.
value[primary].push_back(cur_values[i]);
244 output.
index[primary].push_back(s);
288 typename StoredValue_ = Value_,
289 typename StoredIndex_ = Index_,
290 typename InputValue_,
308 return std::shared_ptr<Matrix<Value_, Index_> >(
312 std::vector<std::vector<StoredValue_> >,
313 std::vector<std::vector<StoredIndex_> >
317 std::move(frag.value),
318 std::move(frag.index),
321 FragmentedSparseMatrixOptions fopt;
333template<
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
334std::shared_ptr<Matrix<Value_, Index_> >
convert_to_fragmented_sparse(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
int threads = 1) {
339 ConvertToFragmentedSparseOptions opt;
340 opt.num_threads = threads;
346template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
352 RetrieveFragmentedSparseContentsOptions opt;
353 opt.num_threads = threads;
359template <
bool row_,
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
364template <
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
Fragmented sparse matrix representation.
Convert index type to container size.
Fragmented sparse matrix representation.
Definition FragmentedSparseMatrix.hpp:570
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
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
FragmentedSparseContents< StoredValue_, StoredIndex_ > retrieve_fragmented_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const RetrieveFragmentedSparseContentsOptions &options)
Definition convert_to_fragmented_sparse.hpp:160
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Definition parallelize.hpp:58
I< decltype(std::declval< Container_ >().size())> cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:65
std::shared_ptr< Matrix< Value_, Index_ > > convert_to_fragmented_sparse(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const ConvertToFragmentedSparseOptions &options)
Definition convert_to_fragmented_sparse.hpp:293
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:82
auto consecutive_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, const Index_ iter_start, const Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Parallelized iteration over a tatami::Matrix.
Options for convert_to_fragmented_sparse().
Definition convert_to_fragmented_sparse.hpp:256
bool two_pass
Definition convert_to_fragmented_sparse.hpp:262
int num_threads
Definition convert_to_fragmented_sparse.hpp:267
Fragmented sparse contents.
Definition convert_to_fragmented_sparse.hpp:36
std::vector< std::vector< Value_ > > value
Definition convert_to_fragmented_sparse.hpp:52
std::vector< std::vector< Index_ > > index
Definition convert_to_fragmented_sparse.hpp:59
Options for retrieve_fragmented_sparse_contents().
Definition convert_to_fragmented_sparse.hpp:65
int num_threads
Definition convert_to_fragmented_sparse.hpp:76
bool two_pass
Definition convert_to_fragmented_sparse.hpp:71
A range of a sparse vector.
Definition SparseRange.hpp:32
const Value_ * value
Definition SparseRange.hpp:56
Index_ number
Definition SparseRange.hpp:50
const Index_ * index
Definition SparseRange.hpp:62