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 :
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 size_t extent = processed.sorted.size();
76 my_holding_vbuffer.resize(extent);
77 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(processed.sorted), opt);
78 my_permutation = std::move(processed.permutation);
82 const Value_* fetch(Index_ i, Value_* buffer) {
83 auto src = my_ext->fetch(i, my_holding_vbuffer.data());
88 for (
auto p : my_permutation) {
97 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
98 std::vector<Value_> my_holding_vbuffer;
99 std::vector<Index_> my_permutation;
102template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
103std::vector<Index_> format_sparse_parallel(
const SubsetStorage_& subset, Index_ len, ToIndex_ to_index) {
104 std::vector<Index_> collected;
105 collected.reserve(len);
106 for (Index_ i = 0; i < len; ++i) {
107 collected.emplace_back(subset[to_index(i)]);
109 std::sort(collected.begin(), collected.end());
113template<
bool oracle_,
typename Value_,
typename Index_>
114class ParallelSparse :
public SparseExtractor<oracle_, Value_, Index_> {
116 template<
class SubsetStorage_>
118 const Matrix<Value_, Index_>* matrix,
119 const SubsetStorage_& subset,
120 const std::vector<Index_>& remap,
121 bool row, MaybeOracle<oracle_, Index_> oracle,
126 auto processed = format_sparse_parallel<Index_>(subset, subset.size(), [](Index_ i) -> Index_ { return i; });
127 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
130 template<
class SubsetStorage_>
132 const Matrix<Value_, Index_>* matrix,
133 const SubsetStorage_& subset,
134 const std::vector<Index_>& remap,
136 MaybeOracle<oracle_, Index_> oracle,
143 auto processed = format_sparse_parallel<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
144 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
147 template<
class SubsetStorage_>
149 const Matrix<Value_, Index_>* matrix,
150 const SubsetStorage_& subset,
151 const std::vector<Index_>& remap,
153 MaybeOracle<oracle_, Index_> oracle,
154 VectorPtr<Index_> indices_ptr,
159 const auto& indices = *indices_ptr;
160 auto processed = format_sparse_parallel<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
161 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
165 void initialize(
const Matrix<Value_, Index_>* matrix, std::vector<Index_> sorted,
bool row, MaybeOracle<oracle_, Index_> oracle, Options opt) {
166 my_needs_value = opt.sparse_extract_value;
167 my_needs_index = opt.sparse_extract_index;
168 my_needs_sort = opt.sparse_ordered_index;
172 if (!my_needs_sort) {
173 if (my_needs_index) {
177 }
else if (my_needs_value) {
178 opt.sparse_extract_index =
true;
179 my_sortspace.reserve(sorted.size());
180 if (my_needs_index) {
183 my_holding_ibuffer.resize(sorted.size());
186 }
else if (my_needs_index) {
190 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(sorted), opt);
194 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
195 auto input = my_ext->fetch(i, value_buffer, (my_holding_ibuffer.empty() ? index_buffer : my_holding_ibuffer.data()));
199 if (!my_needs_sort) {
200 if (my_needs_index) {
201 for (Index_ i = 0; i < input.number; ++i) {
202 index_buffer[i] = my_remapping[input.index[i]];
204 input.index = index_buffer;
207 }
else if (my_needs_value) {
210 my_sortspace.clear();
211 for (Index_ i = 0; i < input.number; ++i) {
212 my_sortspace.emplace_back(my_remapping[input.index[i]], input.value[i]);
214 std::sort(my_sortspace.begin(), my_sortspace.end());
216 auto vcopy = value_buffer;
217 for (
const auto& ss : my_sortspace) {
221 input.value = value_buffer;
223 if (my_needs_index) {
224 auto icopy = index_buffer;
225 for (
const auto& ss : my_sortspace) {
229 input.index = index_buffer;
234 }
else if (my_needs_index) {
235 for (Index_ i = 0; i < input.number; ++i) {
236 index_buffer[i] = my_remapping[input.index[i]];
238 std::sort(index_buffer, index_buffer + input.number);
239 input.index = index_buffer;
246 const std::vector<Index_>& my_remapping;
247 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
248 bool my_needs_value, my_needs_index, my_needs_sort;
249 std::vector<std::pair<Index_, Value_> > my_sortspace;
250 std::vector<Index_> my_holding_ibuffer;
269template<
typename Value_,
typename Index_,
class SubsetStorage_>
281 my_matrix(std::move(matrix)), my_subset(std::move(subset)), my_by_row(by_row)
283 Index_ fulldim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
286 std::vector<unsigned char> checks(fulldim);
287 for (Index_ i = 0, end = my_subset.size(); i < end; ++i) {
288 auto& found = checks[my_subset[i]];
290 throw std::runtime_error(
"my_subset should be unique");
296 my_mapping_single.resize(fulldim);
297 for (Index_ i = 0, end = my_subset.size(); i < end; ++i) {
298 my_mapping_single[my_subset[i]] = i;
303 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
304 SubsetStorage_ my_subset;
306 std::vector<Index_> my_mapping_single;
311 return my_subset.size();
313 return my_matrix->nrow();
319 return my_matrix->ncol();
321 return my_subset.size();
326 return my_matrix->is_sparse();
330 return my_matrix->is_sparse_proportion();
334 return my_matrix->prefer_rows();
338 return my_matrix->prefer_rows_proportion();
342 return my_matrix->uses_oracle(row);
357 template<
typename ... Args_>
358 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
bool row, Args_&& ... args)
const {
359 if (row == my_by_row) {
360 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
362 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<false, Value_, Index_> >(my_matrix.get(), my_subset, row,
false, std::forward<Args_>(args)...);
367 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options& opt)
const {
368 return populate_myopic_dense(row, opt);
371 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
372 return populate_myopic_dense(row, block_start, block_length, opt);
376 return populate_myopic_dense(row, std::move(indices_ptr), opt);
383 template<
typename ... Args_>
384 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
bool row, Args_&& ... args)
const {
385 if (row == my_by_row) {
386 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
388 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<false, Value_, Index_> >(my_matrix.get(), my_subset, my_mapping_single, row,
false, std::forward<Args_>(args)...);
393 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
394 return populate_myopic_sparse(row, opt);
397 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
398 return populate_myopic_sparse(row, block_start, block_length, opt);
402 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
409 template<
typename ... Args_>
410 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
411 if (row == my_by_row) {
412 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
414 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<true, Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
419 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
420 return populate_oracular_dense(row, std::move(oracle), opt);
423 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 {
424 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
428 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
435 template<
typename ... Args_>
436 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Args_&& ... args)
const {
437 if (row == my_by_row) {
438 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
440 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)...);
445 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
446 return populate_oracular_sparse(row, std::move(oracle), opt);
449 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 {
450 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
454 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:270
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:445
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:367
bool prefer_rows() const
Definition DelayedSubsetUnique.hpp:333
Index_ ncol() const
Definition DelayedSubsetUnique.hpp:317
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:375
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:449
double is_sparse_proportion() const
Definition DelayedSubsetUnique.hpp:329
bool is_sparse() const
Definition DelayedSubsetUnique.hpp:325
double prefer_rows_proportion() const
Definition DelayedSubsetUnique.hpp:337
DelayedSubsetUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, bool by_row, bool check=true)
Definition DelayedSubsetUnique.hpp:280
bool uses_oracle(bool row) const
Definition DelayedSubsetUnique.hpp:341
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:401
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:397
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:393
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:371
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:453
Index_ nrow() const
Definition DelayedSubsetUnique.hpp:309
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:419
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:423
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:427
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:21
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