1#ifndef TATAMI_DELAYED_SUBSET_HPP
2#define TATAMI_DELAYED_SUBSET_HPP
21namespace DelayedSubset_internal {
23template<
typename Index_>
24struct DenseParallelResults {
25 std::vector<Index_> collapsed;
26 std::vector<Index_> reindex;
29template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
30DenseParallelResults<Index_> format_dense_parallel_base(
const SubsetStorage_& subset, Index_ len, ToIndex_ to_index) {
31 std::vector<std::pair<Index_, Index_> > collected;
32 collected.reserve(len);
33 for (Index_ i = 0; i < len; ++i) {
34 collected.emplace_back(subset[to_index(i)], i);
36 std::sort(collected.begin(), collected.end());
38 DenseParallelResults<Index_> output;
39 if (collected.size()) {
40 output.collapsed.reserve(len);
41 output.reindex.resize(len);
43 Index_ last = collected.front().first;
44 output.collapsed.push_back(last);
45 output.reindex[collected.front().second] = 0;
48 for (Index_ i = 1; i < len; ++i) {
49 const auto& pp = collected[i];
50 if (pp.first != last) {
52 output.collapsed.push_back(last);
55 output.reindex[pp.second] = counter;
62template<
bool oracle_,
typename Value_,
typename Index_>
63class ParallelDense :
public DenseExtractor<oracle_, Value_, Index_> {
65 template<
class SubsetStorage_>
66 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) {
67 auto processed = format_dense_parallel_base<Index_>(subset, subset.size(), [&](Index_ i) -> Index_ { return i; });
68 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
71 template<
class SubsetStorage_>
72 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length,
const Options& opt) {
73 auto processed = format_dense_parallel_base<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
74 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
77 template<
class SubsetStorage_>
78 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr,
const Options& opt) {
79 const auto& indices = *indices_ptr;
80 auto processed = format_dense_parallel_base<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
81 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
85 void initialize(
const Matrix<Value_, Index_>* matrix, DenseParallelResults<Index_> processed,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) {
86 my_holding_vbuffer.resize(processed.collapsed.size());
87 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(processed.collapsed), opt);
88 my_reindex.swap(processed.reindex);
92 const Value_* fetch(Index_ i, Value_* buffer) {
93 auto src = my_ext->fetch(i, my_holding_vbuffer.data());
97 for (
auto p : my_reindex) {
106 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
107 std::vector<Value_> my_holding_vbuffer;
108 std::vector<Index_> my_reindex;
111template<
typename Index_>
112struct SparseParallelReindex {
119 std::vector<Index_> pool_ptrs;
120 std::vector<Index_> pool_indices;
124template<
typename Index_>
125struct SparseParallelResults {
126 std::vector<Index_> collapsed;
127 SparseParallelReindex<Index_> reindex;
130template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
131SparseParallelResults<Index_> format_sparse_parallel_base(
const SubsetStorage_& indices, Index_ len, ToIndex_ to_index) {
132 std::vector<std::pair<Index_, Index_> > collected;
133 collected.reserve(len);
134 for (Index_ i = 0; i < len; ++i) {
135 auto curdex = to_index(i);
136 collected.emplace_back(indices[curdex], curdex);
138 std::sort(collected.begin(), collected.end());
140 SparseParallelResults<Index_> output;
142 if (collected.size()) {
143 output.collapsed.reserve(len);
144 output.reindex.pool_indices.reserve(len);
145 Index_ first = collected.front().first;
154 output.reindex.offset = first;
155 auto allocation = collected.back().first - output.reindex.offset + 1;
156 output.reindex.pool_ptrs.resize(allocation + 1);
159 output.reindex.pool_ptrs[counter] = 0;
161 output.reindex.pool_indices.push_back(collected.front().second);
162 output.reindex.pool_ptrs[counter] = 1;
163 output.collapsed.push_back(first);
166 for (Index_ i = 1; i < len; ++i) {
167 const auto& pp = collected[i];
168 auto current = pp.first;
169 if (current == last) {
170 output.reindex.pool_indices.push_back(pp.second);
171 ++(output.reindex.pool_ptrs[counter]);
175 Index_ pool_size = output.reindex.pool_indices.size();
176 counter = current - output.reindex.offset;
177 output.reindex.pool_ptrs[counter] = pool_size;
179 output.reindex.pool_indices.push_back(pp.second);
180 output.reindex.pool_ptrs[counter] = pool_size + 1;
181 output.collapsed.push_back(current);
189template<
bool oracle_,
typename Value_,
typename Index_>
190class ParallelSparse :
public SparseExtractor<oracle_, Value_, Index_> {
192 template<
class SubsetStorage_>
193 ParallelSparse(
const Matrix<Value_, Index_>* mat,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) {
194 auto processed = format_sparse_parallel_base<Index_>(subset, subset.size(), [](Index_ i) -> Index_ { return i; });
195 initialize(mat, std::move(processed), subset.size(), row, std::move(oracle), opt);
198 template<
class SubsetStorage_>
199 ParallelSparse(
const Matrix<Value_, Index_>* mat,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length,
const Options& opt) {
200 auto processed = format_sparse_parallel_base<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
201 initialize(mat, std::move(processed), block_length, row, std::move(oracle), opt);
204 template<
class SubsetStorage_>
205 ParallelSparse(
const Matrix<Value_, Index_>* mat,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr,
const Options& opt) {
206 const auto& indices = *indices_ptr;
207 auto processed = format_sparse_parallel_base<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
208 initialize(mat, std::move(processed), indices.size(), row, std::move(oracle), opt);
212 void initialize(
const Matrix<Value_, Index_>* mat, SparseParallelResults<Index_> processed,
size_t extent,
bool row, MaybeOracle<oracle_, Index_> oracle, Options opt) {
213 my_shift = extent - processed.collapsed.size();
215 my_needs_value = opt.sparse_extract_value;
216 my_needs_index = opt.sparse_extract_index;
217 my_needs_sort = opt.sparse_ordered_index;
219 if (my_needs_sort && my_needs_value) {
220 my_sortspace.reserve(extent);
225 opt.sparse_extract_index =
true;
226 if (!my_needs_index) {
227 my_holding_ibuffer.resize(processed.collapsed.size());
230 my_ext = new_extractor<true, oracle_>(mat, row, std::move(oracle), std::move(processed.collapsed), opt);
231 my_reindex = std::move(processed.reindex);
235 SparseRange<Value_, Index_> fetch(Index_ i, Value_* vbuffer, Index_* ibuffer) {
236 auto vinit = (my_needs_value ? vbuffer + my_shift : NULL);
237 auto iinit = (my_needs_index ? ibuffer + my_shift : my_holding_ibuffer.data());
238 auto input = my_ext->fetch(i, vinit, iinit);
240 if (!my_needs_sort) {
249 auto vcopy = vbuffer;
250 auto icopy = ibuffer;
252 auto vsrc = input.value;
253 bool replace_value = my_needs_value && vsrc != vcopy;
255 for (Index_ i = 0; i < input.number; ++i) {
256 auto lookup = input.index[i] - my_reindex.offset;
257 auto start = my_reindex.pool_ptrs[lookup];
258 auto num = my_reindex.pool_ptrs[lookup + 1] - start;
263 std::fill_n(vcopy, num, val);
266 replace_value = (vcopy != vsrc);
269 if (my_needs_index) {
274 std::copy_n(my_reindex.pool_indices.begin() + start, num, icopy);
279 input.number = count;
280 if (my_needs_value) {
281 input.value = vbuffer;
283 if (my_needs_index) {
284 input.index = ibuffer;
289 }
else if (my_needs_value) {
293 my_sortspace.clear();
294 for (Index_ i = 0; i < input.number; ++i) {
295 auto val = input.value[i];
296 auto lookup = input.index[i] - my_reindex.offset;
297 auto start = my_reindex.pool_ptrs[lookup];
298 auto end = my_reindex.pool_ptrs[lookup + 1];
299 for (Index_ j = start; j < end; ++j) {
300 my_sortspace.emplace_back(my_reindex.pool_indices[j], val);
303 std::sort(my_sortspace.begin(), my_sortspace.end());
304 input.number = my_sortspace.size();
306 auto vcopy = vbuffer;
307 for (
const auto& ss : my_sortspace) {
311 input.value = vbuffer;
313 if (my_needs_index) {
314 auto icopy = ibuffer;
315 for (
const auto& ss : my_sortspace) {
319 input.index = ibuffer;
330 auto icopy = ibuffer;
332 for (Index_ i = 0; i < input.number; ++i) {
333 auto lookup = input.index[i] - my_reindex.offset;
334 auto start = my_reindex.pool_ptrs[lookup];
335 auto num = my_reindex.pool_ptrs[lookup + 1] - start;
338 if (my_needs_index) {
339 std::copy_n(my_reindex.pool_indices.begin() + start, num, icopy);
344 input.number = count;
345 if (my_needs_index) {
346 std::sort(ibuffer, ibuffer + count);
347 input.index = ibuffer;
357 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
358 bool my_needs_value, my_needs_index, my_needs_sort;
359 SparseParallelReindex<Index_> my_reindex;
360 std::vector<std::pair<Index_, Value_> > my_sortspace;
361 std::vector<Index_> my_holding_ibuffer;
381template<
typename Value_,
typename Index_,
class SubsetStorage_>
392 my_matrix(std::move(matrix)), my_subset(std::move(subset)), my_by_row(by_row) {}
395 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
396 SubsetStorage_ my_subset;
402 return my_subset.size();
404 return my_matrix->nrow();
410 return my_matrix->ncol();
412 return my_subset.size();
417 return my_matrix->is_sparse();
421 return my_matrix->is_sparse_proportion();
425 return my_matrix->prefer_rows();
429 return my_matrix->prefer_rows_proportion();
433 return my_matrix->uses_oracle(row);
444 template<
typename ... Args_>
445 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
bool row, Args_&& ... args)
const {
446 if (row == my_by_row) {
447 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
449 return std::make_unique<DelayedSubset_internal::ParallelDense<false, Value_, Index_> >(my_matrix.get(), my_subset, row,
false, std::forward<Args_>(args)...);
454 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options& opt)
const {
455 return populate_myopic_dense(row, opt);
458 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
459 return populate_myopic_dense(row, block_start, block_length, opt);
463 return populate_myopic_dense(row, std::move(my_subset_ptr), opt);
470 template<
typename ... Args_>
471 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
bool row, Args_&& ... args)
const {
472 if (row == my_by_row) {
473 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
475 return std::make_unique<DelayedSubset_internal::ParallelSparse<false, Value_, Index_> >(my_matrix.get(), my_subset, row,
false, std::forward<Args_>(args)...);
480 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
481 return populate_myopic_sparse(row, opt);
484 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
485 return populate_myopic_sparse(row, block_start, block_length, opt);
489 return populate_myopic_sparse(row, std::move(my_subset_ptr), opt);
496 template<
typename ... Args_>
497 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
498 if (row == my_by_row) {
499 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
501 return std::make_unique<DelayedSubset_internal::ParallelDense<true, Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
506 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
507 return populate_oracular_dense(row, std::move(oracle), opt);
510 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length,
const Options& opt)
const {
511 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
515 return populate_oracular_dense(row, std::move(oracle), std::move(my_subset_ptr), opt);
522 template<
typename ... Args_>
523 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
524 if (row == my_by_row) {
525 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
527 return std::make_unique<DelayedSubset_internal::ParallelSparse<true, Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
532 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
533 return populate_oracular_sparse(row, std::move(oracle), opt);
536 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length,
const Options& opt)
const {
537 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
541 return populate_oracular_sparse(row, std::move(oracle), std::move(my_subset_ptr), opt);
Delayed subsetting of a matrix with general indices.
Definition DelayedSubset.hpp:382
bool uses_oracle(bool row) const
Definition DelayedSubset.hpp:432
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:540
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:458
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:484
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubset.hpp:532
Index_ ncol() const
Definition DelayedSubset.hpp:408
bool prefer_rows() const
Definition DelayedSubset.hpp:424
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:510
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:488
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubset.hpp:480
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubset.hpp:506
DelayedSubset(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, bool by_row)
Definition DelayedSubset.hpp:391
double prefer_rows_proportion() const
Definition DelayedSubset.hpp:428
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:514
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubset.hpp:454
Index_ nrow() const
Definition DelayedSubset.hpp:400
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubset.hpp:536
bool is_sparse() const
Definition DelayedSubset.hpp:416
double is_sparse_proportion() const
Definition DelayedSubset.hpp:420
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > my_subset_ptr, const Options &opt) const
Definition DelayedSubset.hpp:462
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:21
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
Options for accessing data from a Matrix instance.
Definition Options.hpp:30