1#ifndef TATAMI_DELAYED_SUBSET_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_UNIQUE_HPP
23namespace DelayedSubsetUnique_internal {
25template<
typename Index_>
26struct DenseParallelResults {
27 std::vector<Index_> sorted;
28 std::vector<Index_> permutation;
31template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
32DenseParallelResults<Index_> format_dense_parallel(
const SubsetStorage_& subset, Index_ len, ToIndex_ to_index) {
33 std::vector<std::pair<Index_, Index_> > collected;
34 collected.reserve(len);
35 for (Index_ i = 0; i < len; ++i) {
36 collected.emplace_back(subset[to_index(i)], i);
38 std::sort(collected.begin(), collected.end());
40 DenseParallelResults<Index_> output;
41 output.sorted.reserve(len);
42 output.permutation.reserve(len);
43 for (
const auto& pp : collected) {
44 output.sorted.push_back(pp.first);
45 output.permutation.push_back(pp.second);
51template<
bool oracle_,
typename Value_,
typename Index_>
52class ParallelDense final :
public DenseExtractor<oracle_, Value_, Index_> {
54 template<
class SubsetStorage_>
55 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) {
56 auto processed = format_dense_parallel<Index_>(subset, subset.size(), [&](Index_ i) -> Index_ { return i; });
57 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
60 template<
class SubsetStorage_>
61 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length,
const Options& opt) {
62 auto processed = format_dense_parallel<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
63 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
66 template<
class SubsetStorage_>
67 ParallelDense(
const Matrix<Value_, Index_>* matrix,
const SubsetStorage_& subset,
bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr,
const Options& opt) {
68 const auto& indices = *indices_ptr;
69 auto processed = format_dense_parallel<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
70 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
74 void initialize(
const Matrix<Value_, Index_>* matrix, DenseParallelResults<Index_> processed,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) {
75 my_holding_vbuffer.resize(processed.sorted.size());
76 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(processed.sorted), opt);
77 my_permutation = std::move(processed.permutation);
81 const Value_* fetch(Index_ i, Value_* buffer) {
82 auto src = my_ext->fetch(i, my_holding_vbuffer.data());
87 for (
auto p : my_permutation) {
96 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
97 std::vector<Value_> my_holding_vbuffer;
98 std::vector<Index_> my_permutation;
101template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
102std::vector<Index_> format_sparse_parallel(
const SubsetStorage_& subset, Index_ len, ToIndex_ to_index) {
103 std::vector<Index_> collected;
104 collected.reserve(len);
105 for (Index_ i = 0; i < len; ++i) {
106 collected.emplace_back(subset[to_index(i)]);
108 std::sort(collected.begin(), collected.end());
112template<
bool oracle_,
typename Value_,
typename Index_>
113class ParallelSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
115 template<
class SubsetStorage_>
117 const Matrix<Value_, Index_>* matrix,
118 const SubsetStorage_& subset,
119 const std::vector<Index_>& remap,
120 bool row, MaybeOracle<oracle_, Index_> oracle,
125 auto processed = format_sparse_parallel<Index_>(subset, subset.size(), [](Index_ i) -> Index_ { return i; });
126 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
129 template<
class SubsetStorage_>
131 const Matrix<Value_, Index_>* matrix,
132 const SubsetStorage_& subset,
133 const std::vector<Index_>& remap,
135 MaybeOracle<oracle_, Index_> oracle,
142 auto processed = format_sparse_parallel<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
143 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
146 template<
class SubsetStorage_>
148 const Matrix<Value_, Index_>* matrix,
149 const SubsetStorage_& subset,
150 const std::vector<Index_>& remap,
152 MaybeOracle<oracle_, Index_> oracle,
153 VectorPtr<Index_> indices_ptr,
158 const auto& indices = *indices_ptr;
159 auto processed = format_sparse_parallel<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
160 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
164 void initialize(
const Matrix<Value_, Index_>* matrix, std::vector<Index_> sorted,
bool row, MaybeOracle<oracle_, Index_> oracle, Options opt) {
165 my_needs_value = opt.sparse_extract_value;
166 my_needs_index = opt.sparse_extract_index;
167 my_needs_sort = opt.sparse_ordered_index;
171 if (!my_needs_sort) {
172 if (my_needs_index) {
176 }
else if (my_needs_value) {
177 opt.sparse_extract_index =
true;
178 my_sortspace.reserve(sorted.size());
179 if (my_needs_index) {
182 my_holding_ibuffer.resize(sorted.size());
185 }
else if (my_needs_index) {
189 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(sorted), opt);
193 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
194 auto input = my_ext->fetch(i, value_buffer, (my_holding_ibuffer.empty() ? index_buffer : my_holding_ibuffer.data()));
198 if (!my_needs_sort) {
199 if (my_needs_index) {
200 for (Index_ i = 0; i < input.number; ++i) {
201 index_buffer[i] = my_remapping[input.index[i]];
203 input.index = index_buffer;
206 }
else if (my_needs_value) {
209 my_sortspace.clear();
210 for (Index_ i = 0; i < input.number; ++i) {
211 my_sortspace.emplace_back(my_remapping[input.index[i]], input.value[i]);
213 std::sort(my_sortspace.begin(), my_sortspace.end());
215 auto vcopy = value_buffer;
216 for (
const auto& ss : my_sortspace) {
220 input.value = value_buffer;
222 if (my_needs_index) {
223 auto icopy = index_buffer;
224 for (
const auto& ss : my_sortspace) {
228 input.index = index_buffer;
233 }
else if (my_needs_index) {
234 for (Index_ i = 0; i < input.number; ++i) {
235 index_buffer[i] = my_remapping[input.index[i]];
237 std::sort(index_buffer, index_buffer + input.number);
238 input.index = index_buffer;
245 const std::vector<Index_>& my_remapping;
246 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
247 bool my_needs_value, my_needs_index, my_needs_sort;
248 std::vector<std::pair<Index_, Value_> > my_sortspace;
249 std::vector<Index_> my_holding_ibuffer;
268template<
typename Value_,
typename Index_,
class SubsetStorage_>
280 my_matrix(std::move(matrix)), my_subset(std::move(subset)), my_by_row(by_row)
282 Index_ fulldim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
285 std::vector<unsigned char> checks(fulldim);
286 for (Index_ i = 0, end = my_subset.size(); i < end; ++i) {
287 auto& found = checks[my_subset[i]];
289 throw std::runtime_error(
"my_subset should be unique");
295 my_mapping_single.resize(fulldim);
296 for (Index_ i = 0, end = my_subset.size(); i < end; ++i) {
297 my_mapping_single[my_subset[i]] = i;
302 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
303 SubsetStorage_ my_subset;
305 std::vector<Index_> my_mapping_single;
310 return my_subset.size();
312 return my_matrix->nrow();
318 return my_matrix->ncol();
320 return my_subset.size();
325 return my_matrix->is_sparse();
329 return my_matrix->is_sparse_proportion();
333 return my_matrix->prefer_rows();
337 return my_matrix->prefer_rows_proportion();
341 return my_matrix->uses_oracle(row);
356 template<
typename ... Args_>
357 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
bool row, Args_&& ... args)
const {
358 if (row == my_by_row) {
359 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
361 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<false, Value_, Index_> >(my_matrix.get(), my_subset, row,
false, std::forward<Args_>(args)...);
366 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options& opt)
const {
367 return populate_myopic_dense(row, opt);
370 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
371 return populate_myopic_dense(row, block_start, block_length, opt);
375 return populate_myopic_dense(row, std::move(indices_ptr), opt);
382 template<
typename ... Args_>
383 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
bool row, Args_&& ... args)
const {
384 if (row == my_by_row) {
385 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
387 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<false, Value_, Index_> >(my_matrix.get(), my_subset, my_mapping_single, row,
false, std::forward<Args_>(args)...);
392 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
393 return populate_myopic_sparse(row, opt);
396 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
397 return populate_myopic_sparse(row, block_start, block_length, opt);
401 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
408 template<
typename ... Args_>
409 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
410 if (row == my_by_row) {
411 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
413 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<true, Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
418 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
419 return populate_oracular_dense(row, std::move(oracle), opt);
422 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 {
423 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
427 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
434 template<
typename ... Args_>
435 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
436 if (row == my_by_row) {
437 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
439 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<true, Value_, Index_> >(my_matrix.get(), my_subset, my_mapping_single, row, std::move(oracle), std::forward<Args_>(args)...);
444 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
445 return populate_oracular_sparse(row, std::move(oracle), opt);
448 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 {
449 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
453 return populate_oracular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
Virtual class for a matrix of some numeric type.
Delayed subsetting of a matrix with unique indices.
Definition DelayedSubsetUnique.hpp:269
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:444
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:366
bool prefer_rows() const
Definition DelayedSubsetUnique.hpp:332
Index_ ncol() const
Definition DelayedSubsetUnique.hpp:316
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:374
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 DelayedSubsetUnique.hpp:448
double is_sparse_proportion() const
Definition DelayedSubsetUnique.hpp:328
bool is_sparse() const
Definition DelayedSubsetUnique.hpp:324
double prefer_rows_proportion() const
Definition DelayedSubsetUnique.hpp:336
DelayedSubsetUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, bool by_row, bool check=true)
Definition DelayedSubsetUnique.hpp:279
bool uses_oracle(bool row) const
Definition DelayedSubsetUnique.hpp:340
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:400
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:396
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:392
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:370
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:452
Index_ nrow() const
Definition DelayedSubsetUnique.hpp:308
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:418
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 DelayedSubsetUnique.hpp:422
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:426
Virtual class for a matrix.
Definition Matrix.hpp:59
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_row() const
Definition Matrix.hpp:287
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_column() const
Definition Matrix.hpp:328
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_column() const
Definition Matrix.hpp:537
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_row() const
Definition Matrix.hpp:496
Predict future access requests on the target dimension.
Definition Oracle.hpp:23
Copy data from one buffer to another.
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