1#ifndef TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
2#define TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
11#include "convert_to_sparse_utils.hpp"
36template<
typename Value_,
typename Index_>
53 std::vector<std::vector<Value_> >
value;
60 std::vector<std::vector<Index_> >
index;
83template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
89 const InputIndex_ NR = matrix.
nrow();
90 const InputIndex_ NC = matrix.
ncol();
91 const InputIndex_ primary = (row ? NR : NC);
92 const InputIndex_ secondary = (row ? NC : NR);
95 auto& store_v = output.
value;
96 auto& store_i = output.
index;
99 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
104 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
105 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
106 auto& sv = store_v[p];
107 auto& si = store_i[p];
109 sv.insert(sv.end(), range.value, range.value + range.number);
110 si.insert(si.end(), range.index, range.index + range.number);
115 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
119 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
120 const auto ptr = wrk->fetch(buffer_v.data());
121 auto& sv = store_v[p];
122 auto& si = store_i[p];
125 for (InputIndex_ s = 0; s < secondary; ++s) {
126 const auto val = ptr[s];
140template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
141FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_inconsistent_one_pass(
144 const int num_threads
146 const InputIndex_ NR = matrix.
nrow();
147 const InputIndex_ NC = matrix.
ncol();
148 const InputIndex_ primary = (row ? NR : NC);
149 const InputIndex_ secondary = (row ? NC : NR);
153 std::vector<std::vector<InputValue_> > store_v;
154 std::vector<std::vector<InputIndex_> > store_i;
155 auto original_ranges = extract_sparse_matrix(matrix, store_v, store_i, num_threads);
158 for (I<
decltype(secondary)> s = 0; s < secondary; ++s) {
159 const auto& sec_indices = original_ranges[s].index;
160 const auto num = original_ranges[s].number;
161 for (I<
decltype(num)> n = 0; n < num; ++n) {
162 primary_counts[sec_indices[n]] += 1;
166 FragmentedSparseContents<StoredValue_, StoredIndex_> output(primary);
167 for (InputIndex_ p = 0; p < primary; ++p) {
168 output.
index[p].reserve(primary_counts[p]);
169 output.
value[p].reserve(primary_counts[p]);
172 for (I<
decltype(secondary)> s = 0; s < secondary; ++s) {
173 const auto& sec_values = original_ranges[s].value;
174 const auto& sec_indices = original_ranges[s].index;
175 const auto num = original_ranges[s].number;
176 for (I<
decltype(num)> n = 0; n < num; ++n) {
177 const auto curp = sec_indices[n];
178 output.
value[curp].push_back(sec_values[n]);
179 output.
index[curp].push_back(s);
186template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
187FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_inconsistent_two_pass(
190 const int num_threads
192 const InputIndex_ NR = matrix.
nrow();
193 const InputIndex_ NC = matrix.
ncol();
194 const InputIndex_ primary = (row ? NR : NC);
195 const InputIndex_ secondary = (row ? NC : NR);
199 auto per_thread = count_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, nnz_inconsistent.data(), num_threads);
201 FragmentedSparseContents<StoredValue_, StoredIndex_> output(primary);
204 for (InputIndex_ p = 0; p < primary; ++p) {
205 assert(nnz_inconsistent[p] <= secondary);
206 output.
index[p].resize(nnz_inconsistent[p]);
207 output.
value[p].resize(nnz_inconsistent[p]);
210 const bool is_sparse = matrix.
is_sparse();
211 if (per_thread.has_value()) {
213 auto& offsets = per_thread->counts;
214 for (InputIndex_ i = 0; i < primary; ++i) {
215 InputIndex_ accumulant = 0;
216 static_assert(std::is_same<I<
decltype(per_thread->counts[0][0])>, InputIndex_>::value);
217 for (
auto& pt : offsets) {
218 const auto count = pt[i];
224 parallelize([&](
const int,
const int th_start,
const int th_length) ->
void {
225 for (
int t = 0; t < th_length; ++t) {
226 auto& offsets = (per_thread->counts)[t + th_start];
227 const auto actual_start = (per_thread->starts)[t + th_start];
228 const auto actual_length = (per_thread->lengths)[t + th_start];
233 opt.sparse_ordered_index =
false;
237 for (InputIndex_ x = 0; x < actual_length; ++x) {
238 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
239 for (InputIndex_ i = 0; i < range.number; ++i) {
240 const auto prim = range.index[i];
241 auto& pos = offsets[prim];
242 output.
value[prim][pos] = range.value[i];
243 output.
index[prim][pos] = x + actual_start;
251 for (InputIndex_ x = 0; x < actual_length; ++x) {
252 const auto ptr = wrk->fetch(buffer_v.data());
253 for (InputIndex_ p = 0; p < primary; ++p) {
254 const auto val = ptr[p];
256 auto& pos = offsets[p];
257 output.
value[p][pos] = val;
258 output.
index[p][pos] = x + actual_start;
265 }, per_thread->counts.size(), per_thread->counts.size());
272 opt.sparse_ordered_index =
false;
276 for (InputIndex_ s = 0; s < secondary; ++s) {
277 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
278 for (InputIndex_ i = 0; i < range.number; ++i) {
279 const auto prim = range.index[i];
280 auto& pos = offsets[prim];
281 output.
value[prim][pos] = range.value[i];
282 output.
index[prim][pos] = s;
290 for (InputIndex_ s = 0; s < secondary; ++s) {
291 const auto ptr = wrk->fetch(buffer_v.data());
292 for (InputIndex_ p = 0; p < primary; ++p) {
293 const auto val = ptr[p];
295 auto& pos = offsets[p];
296 output.
value[p][pos] = val;
297 output.
index[p][pos] = s;
323template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
330 return retrieve_fragmented_sparse_contents_consistent<StoredValue_, StoredIndex_>(matrix, row, options);
334 return retrieve_fragmented_sparse_inconsistent_one_pass<StoredValue_, StoredIndex_>(matrix, row, options.
num_threads);
337 return retrieve_fragmented_sparse_inconsistent_two_pass<StoredValue_, StoredIndex_>(matrix, row, options.
num_threads);
375 typename StoredValue_ = Value_,
376 typename StoredIndex_ = Index_,
377 typename InputValue_,
395 return std::shared_ptr<Matrix<Value_, Index_> >(
399 std::vector<std::vector<StoredValue_> >,
400 std::vector<std::vector<StoredIndex_> >
404 std::move(frag.value),
405 std::move(frag.index),
408 FragmentedSparseMatrixOptions fopt;
420template<
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
421std::shared_ptr<Matrix<Value_, Index_> >
convert_to_fragmented_sparse(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row,
int threads = 1) {
426 ConvertToFragmentedSparseOptions opt;
427 opt.num_threads = threads;
433template<
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
439 RetrieveFragmentedSparseContentsOptions opt;
440 opt.num_threads = threads;
446template <
bool row_,
typename StoredValue_,
typename StoredIndex_,
typename InputValue_,
typename InputIndex_>
451template <
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename StoredIndex_ = Index_,
typename InputValue_,
typename InputIndex_>
Iterate across consecutive elements of the target dimension.
Fragmented sparse matrix representation.
Convert index type to container size.
Fragmented sparse matrix representation.
Definition FragmentedSparseMatrix.hpp:627
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
auto cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:51
std::shared_ptr< ConsecutiveOracle< Index_ > > make_ConsecutiveOracle(Index_ start, Index_ length)
Definition ConsecutiveOracle.hpp:56
FragmentedSparseContents< StoredValue_, StoredIndex_ > retrieve_fragmented_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const RetrieveFragmentedSparseContentsOptions &options)
Definition convert_to_fragmented_sparse.hpp:324
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
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:380
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:68
Parallelized iteration over a tatami::Matrix.
Options for convert_to_fragmented_sparse().
Definition convert_to_fragmented_sparse.hpp:343
bool two_pass
Definition convert_to_fragmented_sparse.hpp:349
int num_threads
Definition convert_to_fragmented_sparse.hpp:354
Fragmented sparse contents.
Definition convert_to_fragmented_sparse.hpp:37
std::vector< std::vector< Value_ > > value
Definition convert_to_fragmented_sparse.hpp:53
std::vector< std::vector< Index_ > > index
Definition convert_to_fragmented_sparse.hpp:60
Options for accessing data from a Matrix instance.
Definition Options.hpp:30
Options for retrieve_fragmented_sparse_contents().
Definition convert_to_fragmented_sparse.hpp:66
int num_threads
Definition convert_to_fragmented_sparse.hpp:77
bool two_pass
Definition convert_to_fragmented_sparse.hpp:72