1#ifndef TATAMI_DELAYED_SUBSET_HPP
2#define TATAMI_DELAYED_SUBSET_HPP
11#include "sanisizer/sanisizer.hpp"
26namespace DelayedSubset_internal {
28template<
typename Index_>
29struct DenseParallelResults {
30 std::vector<Index_> collapsed;
31 std::vector<Index_> reindex;
34template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
35DenseParallelResults<Index_> format_dense_parallel_base(
const SubsetStorage_& subset,
const Index_ len,
const ToIndex_ to_index) {
36 std::vector<std::pair<Index_, Index_> > collected;
37 collected.reserve(len);
38 for (Index_ i = 0; i < len; ++i) {
39 collected.emplace_back(subset[to_index(i)], i);
41 std::sort(collected.begin(), collected.end());
43 DenseParallelResults<Index_> output;
44 if (collected.size()) {
45 output.collapsed.reserve(len);
48 Index_ last = collected.front().first;
49 output.collapsed.push_back(last);
50 output.reindex[collected.front().second] = 0;
53 for (Index_ i = 1; i < len; ++i) {
54 const auto& pp = collected[i];
55 if (pp.first != last) {
57 output.collapsed.push_back(last);
60 output.reindex[pp.second] = counter;
67template<
bool oracle_,
typename Value_,
typename Index_>
68class ParallelDense final :
public DenseExtractor<oracle_, Value_, Index_> {
70 template<
class SubsetStorage_>
72 const Matrix<Value_, Index_>& matrix,
73 const SubsetStorage_& subset,
75 MaybeOracle<oracle_, Index_> oracle,
78 auto processed = format_dense_parallel_base<Index_>(subset, subset.size(), [&](
const Index_ i) -> Index_ { return i; });
79 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
82 template<
class SubsetStorage_>
84 const Matrix<Value_, Index_>& matrix,
85 const SubsetStorage_& subset,
87 MaybeOracle<oracle_, Index_> oracle,
88 const Index_ block_start,
89 const Index_ block_length,
92 auto processed = format_dense_parallel_base<Index_>(subset, block_length, [&](
const Index_ i) -> Index_ {
return i + block_start; });
93 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
96 template<
class SubsetStorage_>
98 const Matrix<Value_, Index_>& matrix,
99 const SubsetStorage_& subset,
101 MaybeOracle<oracle_, Index_> oracle,
102 VectorPtr<Index_> indices_ptr,
105 const auto& indices = *indices_ptr;
106 auto processed = format_dense_parallel_base<Index_>(subset, indices.size(), [&](
const Index_ i) -> Index_ { return indices[i]; });
107 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
112 const Matrix<Value_, Index_>& matrix,
113 DenseParallelResults<Index_> processed,
115 MaybeOracle<oracle_, Index_> oracle,
119 my_ext = make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(processed.collapsed)), opt);
120 my_reindex.swap(processed.reindex);
124 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
125 const auto src = my_ext->fetch(i, my_holding_vbuffer.data());
129 for (
auto p : my_reindex) {
138 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
139 std::vector<Value_> my_holding_vbuffer;
140 std::vector<Index_> my_reindex;
143template<
typename Index_>
144struct SparseParallelReindex {
151 std::vector<Index_> pool_ptrs;
152 std::vector<Index_> pool_indices;
156template<
typename Index_>
157struct SparseParallelResults {
158 std::vector<Index_> collapsed;
159 SparseParallelReindex<Index_> reindex;
162template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
163SparseParallelResults<Index_> format_sparse_parallel_base(
const SubsetStorage_& indices,
const Index_ len,
const ToIndex_ to_index) {
164 std::vector<std::pair<Index_, Index_> > collected;
165 collected.reserve(len);
166 for (Index_ i = 0; i < len; ++i) {
167 const auto curdex = to_index(i);
168 collected.emplace_back(indices[curdex], curdex);
170 std::sort(collected.begin(), collected.end());
172 SparseParallelResults<Index_> output;
174 if (collected.size()) {
175 output.collapsed.reserve(len);
176 output.reindex.pool_indices.reserve(len);
177 const Index_ first = collected.front().first;
183 output.reindex.offset = first;
184 const Index_ allocation = collected.back().first - output.reindex.offset + 1;
185 output.reindex.pool_ptrs.resize(sanisizer::sum<I<
decltype(output.reindex.pool_ptrs.size())> >(attest_for_Index(allocation), 1));
188 output.reindex.pool_ptrs[counter] = 0;
190 output.reindex.pool_indices.push_back(collected.front().second);
191 output.reindex.pool_ptrs[counter] = 1;
192 output.collapsed.push_back(first);
195 for (Index_ i = 1; i < len; ++i) {
196 const auto& pp = collected[i];
197 const auto current = pp.first;
198 if (current == last) {
199 output.reindex.pool_indices.push_back(pp.second);
200 ++(output.reindex.pool_ptrs[counter]);
204 const Index_ pool_size = output.reindex.pool_indices.size();
205 counter = current - output.reindex.offset;
206 output.reindex.pool_ptrs[counter] = pool_size;
208 output.reindex.pool_indices.push_back(pp.second);
209 output.reindex.pool_ptrs[counter] = pool_size + 1;
210 output.collapsed.push_back(current);
218template<
bool oracle_,
typename Value_,
typename Index_>
219class ParallelSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
221 template<
class SubsetStorage_>
223 const Matrix<Value_, Index_>& mat,
224 const SubsetStorage_& subset,
226 MaybeOracle<oracle_, Index_> oracle,
229 auto processed = format_sparse_parallel_base<Index_>(subset, subset.size(), [](
const Index_ i) -> Index_ { return i; });
230 initialize(mat, std::move(processed), subset.size(), row, std::move(oracle), opt);
233 template<
class SubsetStorage_>
235 const Matrix<Value_, Index_>& mat,
236 const SubsetStorage_& subset,
238 MaybeOracle<oracle_, Index_> oracle,
239 const Index_ block_start,
240 const Index_ block_length,
243 auto processed = format_sparse_parallel_base<Index_>(subset, block_length, [&](
const Index_ i) -> Index_ {
return i + block_start; });
244 initialize(mat, std::move(processed), block_length, row, std::move(oracle), opt);
247 template<
class SubsetStorage_>
249 const Matrix<Value_, Index_>& mat,
250 const SubsetStorage_& subset,
252 MaybeOracle<oracle_, Index_> oracle,
253 VectorPtr<Index_> indices_ptr,
256 const auto& indices = *indices_ptr;
257 auto processed = format_sparse_parallel_base<Index_>(subset, indices.size(), [&](
const Index_ i) -> Index_ { return indices[i]; });
258 initialize(mat, std::move(processed), indices.size(), row, std::move(oracle), opt);
263 const Matrix<Value_, Index_>& mat,
264 SparseParallelResults<Index_> processed,
267 MaybeOracle<oracle_, Index_> oracle,
270 const Index_ num_collapsed = processed.collapsed.size();
271 my_shift = extent - num_collapsed;
273 my_needs_value = opt.sparse_extract_value;
274 my_needs_index = opt.sparse_extract_index;
275 my_needs_sort = opt.sparse_ordered_index;
277 if (my_needs_sort && my_needs_value) {
278 my_sortspace.reserve(extent);
282 opt.sparse_extract_index =
true;
283 if (!my_needs_index) {
287 my_ext = make_Extractor<true, oracle_>(mat, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(processed.collapsed)), opt);
288 my_reindex = std::move(processed.reindex);
292 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const vbuffer, Index_*
const ibuffer) {
293 const auto vinit = (my_needs_value ? vbuffer + my_shift : NULL);
294 const auto iinit = (my_needs_index ? ibuffer + my_shift : my_holding_ibuffer.data());
295 auto input = my_ext->fetch(i, vinit, iinit);
297 if (!my_needs_sort) {
306 auto vcopy = vbuffer;
307 auto icopy = ibuffer;
309 auto vsrc = input.value;
310 bool replace_value = my_needs_value && vsrc != vcopy;
312 for (Index_ i = 0; i < input.number; ++i) {
313 const auto lookup = input.index[i] - my_reindex.offset;
314 const auto start = my_reindex.pool_ptrs[lookup];
315 const auto num = my_reindex.pool_ptrs[lookup + 1] - start;
320 std::fill_n(vcopy, num, val);
323 replace_value = (vcopy != vsrc);
326 if (my_needs_index) {
331 std::copy_n(my_reindex.pool_indices.begin() + start, num, icopy);
336 input.number = count;
337 if (my_needs_value) {
338 input.value = vbuffer;
340 if (my_needs_index) {
341 input.index = ibuffer;
346 }
else if (my_needs_value) {
350 my_sortspace.clear();
351 for (Index_ i = 0; i < input.number; ++i) {
352 const auto val = input.value[i];
353 const auto lookup = input.index[i] - my_reindex.offset;
354 const auto start = my_reindex.pool_ptrs[lookup];
355 const auto end = my_reindex.pool_ptrs[lookup + 1];
356 for (Index_ j = start; j < end; ++j) {
357 my_sortspace.emplace_back(my_reindex.pool_indices[j], val);
360 std::sort(my_sortspace.begin(), my_sortspace.end());
361 input.number = my_sortspace.size();
363 auto vcopy = vbuffer;
364 for (
const auto& ss : my_sortspace) {
368 input.value = vbuffer;
370 if (my_needs_index) {
371 auto icopy = ibuffer;
372 for (
const auto& ss : my_sortspace) {
376 input.index = ibuffer;
387 auto icopy = ibuffer;
389 for (Index_ i = 0; i < input.number; ++i) {
390 const auto lookup = input.index[i] - my_reindex.offset;
391 const auto start = my_reindex.pool_ptrs[lookup];
392 const auto num = my_reindex.pool_ptrs[lookup + 1] - start;
395 if (my_needs_index) {
396 std::copy_n(my_reindex.pool_indices.begin() + start, num, icopy);
401 input.number = count;
402 if (my_needs_index) {
403 std::sort(ibuffer, ibuffer + count);
404 input.index = ibuffer;
414 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
415 bool my_needs_value, my_needs_index, my_needs_sort;
416 SparseParallelReindex<Index_> my_reindex;
417 std::vector<std::pair<Index_, Value_> > my_sortspace;
418 std::vector<Index_> my_holding_ibuffer;
438template<
typename Value_,
typename Index_,
class SubsetStorage_>
450 SubsetStorage_ subset,
453 my_matrix(std::move(matrix)),
454 my_subset(std::move(subset)),
458 sanisizer::can_cast<Index_>(my_subset.size());
462 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
463 SubsetStorage_ my_subset;
469 return my_subset.size();
471 return my_matrix->nrow();
477 return my_matrix->ncol();
479 return my_subset.size();
484 return my_matrix->is_sparse();
488 return my_matrix->is_sparse_proportion();
492 return my_matrix->prefer_rows();
496 return my_matrix->prefer_rows_proportion();
500 return my_matrix->uses_oracle(row);
511 template<
typename ... Args_>
512 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
516 if (row == my_by_row) {
517 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
521 std::forward<Args_>(args)...
524 return std::make_unique<DelayedSubset_internal::ParallelDense<false, Value_, Index_> >(
529 std::forward<Args_>(args)...
535 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
539 return populate_myopic_dense(row, opt);
542 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
544 const Index_ block_start,
545 const Index_ block_length,
548 return populate_myopic_dense(row, block_start, block_length, opt);
551 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
556 return populate_myopic_dense(row, std::move(my_subset_ptr), opt);
563 template<
typename ... Args_>
564 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
568 if (row == my_by_row) {
569 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
573 std::forward<Args_>(args)...
576 return std::make_unique<DelayedSubset_internal::ParallelSparse<false, Value_, Index_> >(
581 std::forward<Args_>(args)...
587 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
591 return populate_myopic_sparse(row, opt);
594 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
596 const Index_ block_start,
597 const Index_ block_length,
600 return populate_myopic_sparse(row, block_start, block_length, opt);
603 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
608 return populate_myopic_sparse(row, std::move(my_subset_ptr), opt);
615 template<
typename ... Args_>
616 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
621 if (row == my_by_row) {
622 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
627 std::forward<Args_>(args)...
630 return std::make_unique<DelayedSubset_internal::ParallelDense<true, Value_, Index_> >(
635 std::forward<Args_>(args)...
641 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
646 return populate_oracular_dense(row, std::move(oracle), opt);
649 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
652 const Index_ block_start,
653 const Index_ block_length,
656 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
659 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
665 return populate_oracular_dense(row, std::move(oracle), std::move(my_subset_ptr), opt);
672 template<
typename ... Args_>
673 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
678 if (row == my_by_row) {
679 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
684 std::forward<Args_>(args)...
687 return std::make_unique<DelayedSubset_internal::ParallelSparse<true, Value_, Index_> >(
692 std::forward<Args_>(args)...
698 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
703 return populate_oracular_sparse(row, std::move(oracle), opt);
706 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
709 const Index_ block_start,
710 const Index_ block_length,
713 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
716 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
722 return populate_oracular_sparse(row, std::move(oracle), std::move(my_subset_ptr), opt);
Convert index type to container size.
Delayed subsetting of a matrix with general indices.
Definition DelayedSubset.hpp:439
bool uses_oracle(const bool row) const
Definition DelayedSubset.hpp:499
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:603
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubset.hpp:641
Index_ ncol() const
Definition DelayedSubset.hpp:475
bool prefer_rows() const
Definition DelayedSubset.hpp:491
DelayedSubset(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row)
Definition DelayedSubset.hpp:448
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubset.hpp:535
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:594
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubset.hpp:698
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:659
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 DelayedSubset.hpp:706
double prefer_rows_proportion() const
Definition DelayedSubset.hpp:495
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 DelayedSubset.hpp:649
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:551
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubset.hpp:587
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:716
Index_ nrow() const
Definition DelayedSubset.hpp:467
bool is_sparse() const
Definition DelayedSubset.hpp:483
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:542
double is_sparse_proportion() const
Definition DelayedSubset.hpp:487
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
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_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:284
Options for accessing data from a Matrix instance.
Definition Options.hpp:30