1#ifndef TATAMI_DELAYED_SUBSET_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_UNIQUE_HPP
24namespace DelayedSubsetUnique_internal {
26template<
typename Index_>
27struct DenseParallelResults {
28 std::vector<Index_> sorted;
29 std::vector<Index_> permutation;
32template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
33DenseParallelResults<Index_> format_dense_parallel(
const SubsetStorage_& subset,
const Index_ len,
const ToIndex_ to_index) {
34 std::vector<std::pair<Index_, Index_> > collected;
35 collected.reserve(len);
36 for (Index_ i = 0; i < len; ++i) {
37 collected.emplace_back(subset[to_index(i)], i);
39 std::sort(collected.begin(), collected.end());
41 DenseParallelResults<Index_> output;
42 output.sorted.reserve(len);
43 output.permutation.reserve(len);
44 for (
const auto& pp : collected) {
45 output.sorted.push_back(pp.first);
46 output.permutation.push_back(pp.second);
52template<
bool oracle_,
typename Value_,
typename Index_>
53class ParallelDense final :
public DenseExtractor<oracle_, Value_, Index_> {
55 template<
class SubsetStorage_>
57 const Matrix<Value_, Index_>& matrix,
58 const SubsetStorage_& subset,
60 MaybeOracle<oracle_, Index_> oracle,
63 auto processed = format_dense_parallel<Index_>(subset, subset.size(), [&](Index_ i) -> Index_ { return i; });
64 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
67 template<
class SubsetStorage_>
69 const Matrix<Value_, Index_>& matrix,
70 const SubsetStorage_& subset,
72 MaybeOracle<oracle_, Index_> oracle,
73 const Index_ block_start,
74 const Index_ block_length,
77 auto processed = format_dense_parallel<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
78 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
81 template<
class SubsetStorage_>
83 const Matrix<Value_, Index_>& matrix,
84 const SubsetStorage_& subset,
86 MaybeOracle<oracle_, Index_> oracle,
87 VectorPtr<Index_> indices_ptr,
90 const auto& indices = *indices_ptr;
91 auto processed = format_dense_parallel<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
92 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
97 const Matrix<Value_, Index_>& matrix,
98 DenseParallelResults<Index_> processed,
100 MaybeOracle<oracle_, Index_> oracle,
104 my_ext = make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(processed.sorted)), opt);
105 my_permutation = std::move(processed.permutation);
109 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
110 auto src = my_ext->fetch(i, my_holding_vbuffer.data());
115 for (
const auto p : my_permutation) {
124 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
125 std::vector<Value_> my_holding_vbuffer;
126 std::vector<Index_> my_permutation;
129template<
typename Index_,
class SubsetStorage_,
class ToIndex_>
130std::vector<Index_> format_sparse_parallel(
const SubsetStorage_& subset,
const Index_ len,
const ToIndex_ to_index) {
131 std::vector<Index_> collected;
132 collected.reserve(len);
133 for (Index_ i = 0; i < len; ++i) {
134 collected.emplace_back(subset[to_index(i)]);
136 std::sort(collected.begin(), collected.end());
140template<
bool oracle_,
typename Value_,
typename Index_>
141class ParallelSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
143 template<
class SubsetStorage_>
145 const Matrix<Value_, Index_>& matrix,
146 const SubsetStorage_& subset,
147 const std::vector<Index_>& remap,
149 MaybeOracle<oracle_, Index_> oracle,
154 auto processed = format_sparse_parallel<Index_>(subset, subset.size(), [](Index_ i) -> Index_ { return i; });
155 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
158 template<
class SubsetStorage_>
160 const Matrix<Value_, Index_>& matrix,
161 const SubsetStorage_& subset,
162 const std::vector<Index_>& remap,
164 MaybeOracle<oracle_, Index_> oracle,
165 const Index_ block_start,
166 const Index_ block_length,
171 auto processed = format_sparse_parallel<Index_>(subset, block_length, [&](Index_ i) -> Index_ {
return i + block_start; });
172 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
175 template<
class SubsetStorage_>
177 const Matrix<Value_, Index_>& matrix,
178 const SubsetStorage_& subset,
179 const std::vector<Index_>& remap,
181 MaybeOracle<oracle_, Index_> oracle,
182 VectorPtr<Index_> indices_ptr,
187 const auto& indices = *indices_ptr;
188 auto processed = format_sparse_parallel<Index_>(subset, indices.size(), [&](Index_ i) -> Index_ { return indices[i]; });
189 initialize(matrix, std::move(processed), row, std::move(oracle), opt);
193 void initialize(
const Matrix<Value_, Index_>& matrix, std::vector<Index_> sorted,
const bool row, MaybeOracle<oracle_, Index_> oracle, Options opt) {
194 my_needs_value = opt.sparse_extract_value;
195 my_needs_index = opt.sparse_extract_index;
196 my_needs_sort = opt.sparse_ordered_index;
200 if (!my_needs_sort) {
201 if (my_needs_index) {
205 }
else if (my_needs_value) {
206 opt.sparse_extract_index =
true;
207 my_sortspace.reserve(sorted.size());
208 if (my_needs_index) {
215 }
else if (my_needs_index) {
219 my_ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(sorted)), opt);
223 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
224 auto input = my_ext->fetch(i, value_buffer, (my_holding_ibuffer.empty() ? index_buffer : my_holding_ibuffer.data()));
228 if (!my_needs_sort) {
229 if (my_needs_index) {
230 for (Index_ i = 0; i < input.number; ++i) {
231 index_buffer[i] = my_remapping[input.index[i]];
233 input.index = index_buffer;
236 }
else if (my_needs_value) {
239 my_sortspace.clear();
240 for (Index_ i = 0; i < input.number; ++i) {
241 my_sortspace.emplace_back(my_remapping[input.index[i]], input.value[i]);
243 std::sort(my_sortspace.begin(), my_sortspace.end());
245 auto vcopy = value_buffer;
246 for (
const auto& ss : my_sortspace) {
250 input.value = value_buffer;
252 if (my_needs_index) {
253 auto icopy = index_buffer;
254 for (
const auto& ss : my_sortspace) {
258 input.index = index_buffer;
263 }
else if (my_needs_index) {
264 for (Index_ i = 0; i < input.number; ++i) {
265 index_buffer[i] = my_remapping[input.index[i]];
267 std::sort(index_buffer, index_buffer + input.number);
268 input.index = index_buffer;
275 const std::vector<Index_>& my_remapping;
276 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
277 bool my_needs_value, my_needs_index, my_needs_sort;
278 std::vector<std::pair<Index_, Value_> > my_sortspace;
279 std::vector<Index_> my_holding_ibuffer;
298template<
typename Value_,
typename Index_,
class SubsetStorage_>
311 SubsetStorage_ subset,
313 const bool check =
true
315 my_matrix(std::move(matrix)),
316 my_subset(std::move(subset)),
319 const Index_ fulldim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
320 const auto nsub = my_subset.size();
324 for (I<
decltype(nsub)> i = 0; i < nsub; ++i) {
325 auto& found = checks[my_subset[i]];
327 throw std::runtime_error(
"my_subset should be unique");
334 for (I<
decltype(nsub)> i = 0; i < nsub; ++i) {
335 my_mapping_single[my_subset[i]] = i;
340 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
341 SubsetStorage_ my_subset;
343 std::vector<Index_> my_mapping_single;
348 return my_subset.size();
350 return my_matrix->nrow();
356 return my_matrix->ncol();
358 return my_subset.size();
363 return my_matrix->is_sparse();
367 return my_matrix->is_sparse_proportion();
371 return my_matrix->prefer_rows();
375 return my_matrix->prefer_rows_proportion();
379 return my_matrix->uses_oracle(row);
382 using Matrix<Value_, Index_>::dense_column;
384 using Matrix<Value_, Index_>::dense_row;
386 using Matrix<Value_, Index_>::sparse_column;
388 using Matrix<Value_, Index_>::sparse_row;
394 template<
typename ... Args_>
395 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
399 if (row == my_by_row) {
400 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
404 std::forward<Args_>(args)...
407 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<false, Value_, Index_> >(
412 std::forward<Args_>(args)...
418 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
422 return populate_myopic_dense(row, opt);
425 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
427 const Index_ block_start,
428 const Index_ block_length,
431 return populate_myopic_dense(row, block_start, block_length, opt);
434 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
439 return populate_myopic_dense(row, std::move(indices_ptr), opt);
446 template<
typename ... Args_>
447 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
451 if (row == my_by_row) {
452 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
456 std::forward<Args_>(args)...
459 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<false, Value_, Index_> >(
465 std::forward<Args_>(args)...
471 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
475 return populate_myopic_sparse(row, opt);
478 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
480 const Index_ block_start,
481 const Index_ block_length,
484 return populate_myopic_sparse(row, block_start, block_length, opt);
487 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
492 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
499 template<
typename ... Args_>
500 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
505 if (row == my_by_row) {
506 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
511 std::forward<Args_>(args)...
514 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<true, Value_, Index_> >(
519 std::forward<Args_>(args)...
525 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
530 return populate_oracular_dense(row, std::move(oracle), opt);
533 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
536 const Index_ block_start,
537 const Index_ block_length,
540 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
543 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
549 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
556 template<
typename ... Args_>
557 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
562 if (row == my_by_row) {
563 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
568 std::forward<Args_>(args)...
571 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<true, Value_, Index_> >(
577 std::forward<Args_>(args)...
583 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
588 return populate_oracular_sparse(row, std::move(oracle), opt);
591 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
594 const Index_ block_start,
595 const Index_ block_length,
598 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
601 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
607 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:299
bool prefer_rows() const
Definition DelayedSubsetUnique.hpp:370
Index_ ncol() const
Definition DelayedSubsetUnique.hpp:354
double is_sparse_proportion() const
Definition DelayedSubsetUnique.hpp:366
bool is_sparse() const
Definition DelayedSubsetUnique.hpp:362
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:478
double prefer_rows_proportion() const
Definition DelayedSubsetUnique.hpp:374
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:583
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 DelayedSubsetUnique.hpp:533
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetUnique.hpp:425
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:418
bool uses_oracle(const bool row) const
Definition DelayedSubsetUnique.hpp:378
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 DelayedSubsetUnique.hpp:601
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetUnique.hpp:525
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 DelayedSubsetUnique.hpp:543
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:434
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetUnique.hpp:487
Index_ nrow() const
Definition DelayedSubsetUnique.hpp:346
DelayedSubsetUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row, const bool check=true)
Definition DelayedSubsetUnique.hpp:309
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 DelayedSubsetUnique.hpp:591
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetUnique.hpp:471
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
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
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
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:68
Options for accessing data from a Matrix instance.
Definition Options.hpp:30