1#ifndef TATAMI_DELAYED_SUBSET_SORTED_HPP
2#define TATAMI_DELAYED_SUBSET_SORTED_HPP
13#include "sanisizer/sanisizer.hpp"
28namespace DelayedSubsetSorted_internal {
30template<
typename Index_>
31struct DenseParallelResults {
32 std::vector<Index_> collapsed;
33 std::vector<Index_> expansion;
36template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
37DenseParallelResults<Index_> format_dense_parallel(
const SubsetStorage_& indices,
const Index_ len,
const ToIndex_ to_index) {
38 DenseParallelResults<Index_> output;
39 output.expansion.reserve(len);
40 output.collapsed.reserve(len);
43 auto last = indices[to_index(0)];
44 output.expansion.push_back(1);
45 output.collapsed.push_back(last);
47 for (Index_ i = 1; i < len; ++i) {
48 const auto current = indices[to_index(i)];
49 if (current == last) {
50 ++(output.expansion.back());
53 output.expansion.push_back(1);
54 output.collapsed.push_back(last);
62template<
bool oracle_,
typename Value_,
typename Index_>
63class ParallelDense final :
public DenseExtractor<oracle_, Value_, Index_> {
65 template<
class SubsetStorage_>
67 const Matrix<Value_, Index_>& matrix,
68 const SubsetStorage_& subset,
70 MaybeOracle<oracle_, Index_> oracle,
73 auto processed = format_dense_parallel<Index_>(subset, subset.size(), [&](
const Index_ i) -> Index_ { return i; });
74 initialize(matrix, std::move(processed), subset.size(), row, std::move(oracle), opt);
77 template<
class SubsetStorage_>
79 const Matrix<Value_, Index_>& matrix,
80 const SubsetStorage_& subset,
82 MaybeOracle<oracle_, Index_> oracle,
83 const Index_ block_start,
84 const Index_ block_length,
87 auto processed = format_dense_parallel<Index_>(subset, block_length, [&](
const Index_ i) -> Index_ {
return i + block_start; });
88 initialize(matrix, std::move(processed), block_length, row, std::move(oracle), opt);
91 template<
class SubsetStorage_>
93 const Matrix<Value_, Index_>& matrix,
94 const SubsetStorage_& subset,
96 MaybeOracle<oracle_, Index_> oracle,
97 VectorPtr<Index_> indices_ptr,
100 const auto& indices = *indices_ptr;
101 auto processed = format_dense_parallel<Index_>(subset, indices.size(), [&](
const Index_ i) -> Index_ { return indices[i]; });
102 initialize(matrix, std::move(processed), indices.size(), row, std::move(oracle), opt);
107 const Matrix<Value_, Index_>& mat,
108 DenseParallelResults<Index_> processed,
111 MaybeOracle<oracle_, Index_> oracle,
114 my_shift = extent - processed.collapsed.size();
115 my_ext = make_Extractor<false, oracle_>(mat, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(processed.collapsed)), opt);
116 my_expansion = std::move(processed.expansion);
120 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
121 auto input = my_ext->fetch(i, buffer + my_shift);
131 for (
const auto e : my_expansion) {
139 std::fill_n(copy, e, *input);
148 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
149 std::vector<Index_> my_expansion;
153template<
typename Index_>
154struct SparseParallelExpansion {
163 std::vector<Index_> start;
164 std::vector<Index_> length;
169template<
typename Index_>
170struct SparseParallelResults {
171 std::vector<Index_> collapsed;
172 SparseParallelExpansion<Index_> expansion;
175template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
176SparseParallelResults<Index_> format_sparse_parallel(
const SubsetStorage_& indices,
const Index_ len,
const ToIndex_ to_index) {
177 SparseParallelResults<Index_> output;
180 output.collapsed.reserve(len);
181 const auto first = indices[to_index(0)];
187 output.expansion.offset = first;
188 const Index_ allocation = indices[to_index(len - 1)] - output.expansion.offset + 1;
190 output.expansion.length.resize(output.expansion.start.size());
193 output.expansion.start[0] = 0;
194 output.expansion.length[0] = 1;
195 output.collapsed.push_back(first);
198 for (Index_ i = 1; i < len; ++i) {
199 const auto current = indices[to_index(i)];
200 if (current == last) {
201 ++(output.expansion.length[lookup]);
205 lookup = current - output.expansion.offset;
206 output.expansion.start[lookup] = i;
207 output.expansion.length[lookup] = 1;
208 output.collapsed.push_back(current);
216template<
bool oracle_,
typename Value_,
typename Index_>
217class ParallelSparseCore {
219 template<
class SubsetStorage_,
class ToIndex_>
221 const Matrix<Value_, Index_>& matrix,
222 const SubsetStorage_& subset,
225 MaybeOracle<oracle_, Index_> oracle,
229 auto processed = format_sparse_parallel<Index_>(subset, extent, std::forward<ToIndex_>(to_index));
230 my_shift = extent - processed.collapsed.size();
232 my_needs_value = opt.sparse_extract_value;
233 my_needs_index = opt.sparse_extract_index;
234 opt.sparse_extract_index =
true;
235 if (!my_needs_index) {
240 my_ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(processed.collapsed)), opt);
241 my_expansion = std::move(processed.expansion);
244 template<
class ToIndex_>
245 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer,
const ToIndex_ to_index) {
248 const auto vinit = (my_needs_value ? value_buffer + my_shift : NULL);
249 const auto iinit = (my_needs_index ? index_buffer + my_shift : my_holding_ibuffer.data());
250 const auto input = my_ext->fetch(i, vinit, iinit);
252 auto vcopy = value_buffer;
253 auto icopy = index_buffer;
256 auto vsrc = input.value;
257 bool replace_value = my_needs_value && vsrc != vcopy;
266 for (Index_ i = 0; i < input.number; ++i) {
267 const auto eindex = input.index[i] - my_expansion.offset;
268 const auto nexpand = my_expansion.length[eindex];
272 const auto v = *vsrc;
273 std::fill_n(vcopy, nexpand, v);
276 replace_value = (vcopy != vsrc);
279 if (my_needs_index) {
280 const auto sexpand = my_expansion.start[eindex];
281 for (Index_ e = 0; e < nexpand; ++e, ++icopy) {
282 *icopy = to_index(sexpand + e);
287 return SparseRange<Value_, Index_>(
289 (my_needs_value ? value_buffer : NULL),
290 (my_needs_index ? index_buffer : NULL)
295 bool my_needs_value, my_needs_index;
296 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
297 std::vector<Index_> my_holding_ibuffer;
298 SparseParallelExpansion<Index_> my_expansion;
302template<
bool oracle_,
typename Value_,
typename Index_>
303class ParallelFullSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
305 template<
class SubsetStorage_>
307 const Matrix<Value_, Index_>& matrix,
308 const SubsetStorage_& subset,
310 MaybeOracle<oracle_, Index_> oracle,
313 my_core(matrix, subset, subset.size(), row, std::move(oracle), opt, Helper())
316 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
317 return my_core.fetch(i, value_buffer, index_buffer, Helper());
321 ParallelSparseCore<oracle_, Value_, Index_> my_core;
324 Index_ operator()(
const Index_ i)
const {
330template<
bool oracle_,
typename Value_,
typename Index_>
331class ParallelBlockSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
333 template<
class SubsetStorage_>
335 const Matrix<Value_, Index_>& matrix,
336 const SubsetStorage_& subset,
338 MaybeOracle<oracle_, Index_> oracle,
339 const Index_ block_start,
340 const Index_ block_length,
343 my_core(matrix, subset, block_length, row, std::move(oracle), opt, Helper(block_start)),
344 my_block_start(block_start)
347 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
348 return my_core.fetch(i, value_buffer, index_buffer, Helper(my_block_start));
352 ParallelSparseCore<oracle_, Value_, Index_> my_core;
353 Index_ my_block_start;
356 Helper(Index_ block_start) : block_start(block_start) {}
358 Index_ operator()(
const Index_ i)
const {
359 return i + block_start;
364template<
bool oracle_,
typename Value_,
typename Index_>
365class ParallelIndexSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
367 template<
class SubsetStorage_>
369 const Matrix<Value_, Index_>& matrix,
370 const SubsetStorage_& subset,
372 MaybeOracle<oracle_, Index_> oracle,
373 VectorPtr<Index_> indices_ptr,
376 my_core(matrix, subset, indices_ptr->size(), row, std::move(oracle), opt, Helper(*indices_ptr)),
377 my_indices_ptr(std::move(indices_ptr))
380 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
381 return my_core.fetch(i, value_buffer, index_buffer, Helper(*my_indices_ptr));
385 ParallelSparseCore<oracle_, Value_, Index_> my_core;
386 VectorPtr<Index_> my_indices_ptr;
389 Helper(
const std::vector<Index_>& indices) : indices(indices) {}
390 const std::vector<Index_>& indices;
391 Index_ operator()(
const Index_ i)
const {
413template<
typename Value_,
typename Index_,
class SubsetStorage_>
426 SubsetStorage_ subset,
428 const bool check =
true
430 my_matrix(std::move(matrix)),
431 my_subset(std::move(subset)),
435 sanisizer::can_cast<Index_>(my_subset.size());
438 const Index_ nsub = my_subset.size();
439 for (Index_ i = 1; i < nsub; ++i) {
440 if (my_subset[i] < my_subset[i-1]) {
441 throw std::runtime_error(
"subset should be sorted");
448 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
449 SubsetStorage_ my_subset;
452 Index_ get_mapping_dim()
const {
454 return my_matrix->nrow();
456 return my_matrix->ncol();
463 return my_subset.size();
465 return my_matrix->nrow();
471 return my_matrix->ncol();
473 return my_subset.size();
478 return my_matrix->is_sparse();
482 return my_matrix->is_sparse_proportion();
486 return my_matrix->prefer_rows();
490 return my_matrix->prefer_rows_proportion();
494 return my_matrix->uses_oracle(row);
497 using Matrix<Value_, Index_>::dense_column;
499 using Matrix<Value_, Index_>::dense_row;
501 using Matrix<Value_, Index_>::sparse_column;
503 using Matrix<Value_, Index_>::sparse_row;
509 template<
typename ... Args_>
510 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
514 if (row == my_by_row) {
515 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
519 std::forward<Args_>(args)...
522 return std::make_unique<DelayedSubsetSorted_internal::ParallelDense<false, Value_, Index_> >(
527 std::forward<Args_>(args)...
533 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
537 return populate_myopic_dense(row, opt);
540 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
542 const Index_ block_start,
543 const Index_ block_length,
546 return populate_myopic_dense(row, block_start, block_length, opt);
549 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
554 return populate_myopic_dense(row, std::move(indices_ptr), opt);
562 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > populate_sparse(
567 if constexpr(selection_ == DimensionSelectionType::FULL) {
568 return std::make_unique<DelayedSubsetSorted_internal::ParallelFullSparse<oracle_, Value_, Index_> >(
573 std::forward<Args_>(args)...
575 }
else if constexpr(selection_ == DimensionSelectionType::BLOCK) {
576 return std::make_unique<DelayedSubsetSorted_internal::ParallelBlockSparse<oracle_, Value_, Index_> >(
581 std::forward<Args_>(args)...
584 return std::make_unique<DelayedSubsetSorted_internal::ParallelIndexSparse<oracle_, Value_, Index_> >(
589 std::forward<Args_>(args)...
595 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
599 if (row == my_by_row) {
600 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
604 std::forward<Args_>(args)...
607 return populate_sparse<selection_, false>(row,
false, std::forward<Args_>(args)...);
612 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
616 return populate_myopic_sparse<DimensionSelectionType::FULL>(row, opt);
619 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
621 const Index_ block_start,
622 const Index_ block_length,
625 return populate_myopic_sparse<DimensionSelectionType::BLOCK>(row, block_start, block_length, opt);
628 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
633 return populate_myopic_sparse<DimensionSelectionType::INDEX>(row, std::move(indices_ptr), opt);
640 template<
typename ... Args_>
641 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
646 if (row == my_by_row) {
647 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
652 std::forward<Args_>(args)...
655 return std::make_unique<DelayedSubsetSorted_internal::ParallelDense<true, Value_, Index_> >(
660 std::forward<Args_>(args)...
666 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
671 return populate_oracular_dense(row, std::move(oracle), opt);
674 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
677 const Index_ block_start,
678 const Index_ block_length,
681 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
684 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
690 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
698 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
703 if (row == my_by_row) {
704 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
709 std::forward<Args_>(args)...
712 return populate_sparse<selection_, true>(row, std::move(oracle), std::forward<Args_>(args)...);
717 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
722 return populate_oracular_sparse<DimensionSelectionType::FULL>(row, std::move(oracle), opt);
725 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
728 const Index_ block_start,
729 const Index_ block_length,
732 return populate_oracular_sparse<DimensionSelectionType::BLOCK>(row, std::move(oracle), block_start, block_length, opt);
735 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
741 return populate_oracular_sparse<DimensionSelectionType::INDEX>(row, std::move(oracle), std::move(indices_ptr), opt);
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Delayed subsetting of a matrix with sorted indices.
Definition DelayedSubsetSorted.hpp:414
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSorted.hpp:717
double prefer_rows_proportion() const
Definition DelayedSubsetSorted.hpp:489
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetSorted.hpp:612
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSorted.hpp:725
Index_ nrow() const
Definition DelayedSubsetSorted.hpp:461
bool prefer_rows() const
Definition DelayedSubsetSorted.hpp:485
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSorted.hpp:540
double is_sparse_proportion() const
Definition DelayedSubsetSorted.hpp:481
bool is_sparse() const
Definition DelayedSubsetSorted.hpp:477
DelayedSubsetSorted(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row, const bool check=true)
Definition DelayedSubsetSorted.hpp:424
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSorted.hpp:549
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSorted.hpp:666
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSorted.hpp:674
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetSorted.hpp:533
bool uses_oracle(const bool row) const
Definition DelayedSubsetSorted.hpp:493
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSorted.hpp:619
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSorted.hpp:628
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSorted.hpp:735
Index_ ncol() const
Definition DelayedSubsetSorted.hpp:469
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSorted.hpp:684
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Flexible representations for matrix data.
Definition Extractor.hpp:15
DimensionSelectionType
Definition Options.hpp:25
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:85
typename std::conditional< oracle_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Definition make_Extractor.hpp:23
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:284
Options for accessing data from a Matrix instance.
Definition Options.hpp:30