tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DelayedSubsetUnique.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DELAYED_SUBSET_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_UNIQUE_HPP
3
4#include "utils.hpp"
5#include "../base/Matrix.hpp"
6#include "../utils/copy.hpp"
8
9#include <algorithm>
10#include <numeric>
11#include <memory>
12
19namespace tatami {
20
24namespace DelayedSubsetUnique_internal {
25
26template<typename Index_>
27struct DenseParallelResults {
28 std::vector<Index_> sorted;
29 std::vector<Index_> permutation;
30};
31
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);
38 }
39 std::sort(collected.begin(), collected.end());
40
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);
47 }
48
49 return output;
50}
51
52template<bool oracle_, typename Value_, typename Index_>
53class ParallelDense final : public DenseExtractor<oracle_, Value_, Index_> {
54public:
55 template<class SubsetStorage_>
56 ParallelDense(
57 const Matrix<Value_, Index_>& matrix,
58 const SubsetStorage_& subset,
59 const bool row,
60 MaybeOracle<oracle_, Index_> oracle,
61 const Options& opt
62 ) {
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);
65 }
66
67 template<class SubsetStorage_>
68 ParallelDense(
69 const Matrix<Value_, Index_>& matrix,
70 const SubsetStorage_& subset,
71 const bool row,
72 MaybeOracle<oracle_, Index_> oracle,
73 const Index_ block_start,
74 const Index_ block_length,
75 const Options& opt
76 ) {
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);
79 }
80
81 template<class SubsetStorage_>
82 ParallelDense(
83 const Matrix<Value_, Index_>& matrix,
84 const SubsetStorage_& subset,
85 const bool row,
86 MaybeOracle<oracle_, Index_> oracle,
87 VectorPtr<Index_> indices_ptr,
88 const Options& opt
89 ) {
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);
93 }
94
95private:
96 void initialize(
97 const Matrix<Value_, Index_>& matrix,
98 DenseParallelResults<Index_> processed,
99 const bool row,
100 MaybeOracle<oracle_, Index_> oracle,
101 const Options& opt
102 ) {
103 resize_container_to_Index_size(my_holding_vbuffer, processed.sorted.size()); // processed.sorted.size() should fit in an Index_, hence the cast is safe.
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);
106 }
107
108public:
109 const Value_* fetch(const Index_ i, Value_* const buffer) {
110 auto src = my_ext->fetch(i, my_holding_vbuffer.data());
111
112 // 'input' and 'output' should not point to the same array. In theory, it
113 // is possible to do an in-place permutation, but this requires another
114 // array anyway to track the permutation status, so we'll just keep it simple.
115 for (const auto p : my_permutation) {
116 buffer[p] = *src;
117 ++src;
118 }
119
120 return buffer;
121 }
122
123private:
124 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
125 std::vector<Value_> my_holding_vbuffer;
126 std::vector<Index_> my_permutation;
127};
128
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)]);
135 }
136 std::sort(collected.begin(), collected.end());
137 return collected;
138}
139
140template<bool oracle_, typename Value_, typename Index_>
141class ParallelSparse final : public SparseExtractor<oracle_, Value_, Index_> {
142public:
143 template<class SubsetStorage_>
144 ParallelSparse(
145 const Matrix<Value_, Index_>& matrix,
146 const SubsetStorage_& subset,
147 const std::vector<Index_>& remap,
148 const bool row,
149 MaybeOracle<oracle_, Index_> oracle,
150 const Options& opt
151 ) :
152 my_remapping(remap)
153 {
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);
156 }
157
158 template<class SubsetStorage_>
159 ParallelSparse(
160 const Matrix<Value_, Index_>& matrix,
161 const SubsetStorage_& subset,
162 const std::vector<Index_>& remap,
163 const bool row,
164 MaybeOracle<oracle_, Index_> oracle,
165 const Index_ block_start,
166 const Index_ block_length,
167 const Options& opt
168 ) :
169 my_remapping(remap)
170 {
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);
173 }
174
175 template<class SubsetStorage_>
176 ParallelSparse(
177 const Matrix<Value_, Index_>& matrix,
178 const SubsetStorage_& subset,
179 const std::vector<Index_>& remap,
180 const bool row,
181 MaybeOracle<oracle_, Index_> oracle,
182 VectorPtr<Index_> indices_ptr,
183 const Options& opt
184 ) :
185 my_remapping(remap)
186 {
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);
190 }
191
192private:
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;
197
198 // The conditionals here mirror those in 'fetch',
199 // to self-document the case where each of the temporaries are needed.
200 if (!my_needs_sort) {
201 if (my_needs_index) {
202 ; // no 'my_holding_ibuffer' required as a user-provided 'index_buffer' should be available.
203 }
204
205 } else if (my_needs_value) {
206 opt.sparse_extract_index = true;
207 my_sortspace.reserve(sorted.size());
208 if (my_needs_index) {
209 // no 'my_holding_ibuffer' required as a user-provided 'index_buffer' should be available.
210 } else {
211 // Needs 'my_holding_ibuffer' as user-provided 'index_buffer' may be NULL.
212 resize_container_to_Index_size(my_holding_ibuffer, sorted.size()); // sorted.size() should fit in an Index_, hence the cast is safe.
213 }
214
215 } else if (my_needs_index) {
216 ; // no 'my_holding_ibuffer' required as a user-provided 'index_buffer' should be available.
217 }
218
219 my_ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), subset_utils::as_VectorPtr(std::move(sorted)), opt);
220 }
221
222public:
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()));
225
226 // Pointers in 'input' and the 'buffer' pointers may point to the same array,
227 // as we're either just modifiying in place or we're copying to 'my_sortspace'.
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]];
232 }
233 input.index = index_buffer;
234 }
235
236 } else if (my_needs_value) {
237 // We assume that the indices have already been extracted for sorting
238 // purposes, even if they weren't actually requested.
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]);
242 }
243 std::sort(my_sortspace.begin(), my_sortspace.end());
244
245 auto vcopy = value_buffer;
246 for (const auto& ss : my_sortspace) {
247 *vcopy = ss.second;
248 ++vcopy;
249 }
250 input.value = value_buffer;
251
252 if (my_needs_index) {
253 auto icopy = index_buffer;
254 for (const auto& ss : my_sortspace) {
255 *icopy = ss.first;
256 ++icopy;
257 }
258 input.index = index_buffer;
259 } else {
260 input.index = NULL;
261 }
262
263 } else if (my_needs_index) {
264 for (Index_ i = 0; i < input.number; ++i) {
265 index_buffer[i] = my_remapping[input.index[i]];
266 }
267 std::sort(index_buffer, index_buffer + input.number);
268 input.index = index_buffer;
269 }
270
271 return input;
272 }
273
274private:
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;
280};
281
282}
298template<typename Value_, typename Index_, class SubsetStorage_>
299class DelayedSubsetUnique final : public Matrix<Value_, Index_> {
300public:
310 std::shared_ptr<const Matrix<Value_, Index_> > matrix,
311 SubsetStorage_ subset,
312 const bool by_row,
313 const bool check = true
314 ) :
315 my_matrix(std::move(matrix)),
316 my_subset(std::move(subset)),
317 my_by_row(by_row)
318 {
319 const Index_ fulldim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
320 const auto nsub = my_subset.size();
321
322 if (check) {
324 for (I<decltype(nsub)> i = 0; i < nsub; ++i) {
325 auto& found = checks[my_subset[i]];
326 if (found) {
327 throw std::runtime_error("my_subset should be unique");
328 }
329 found = 1;
330 }
331 }
332
333 resize_container_to_Index_size(my_mapping_single, fulldim);
334 for (I<decltype(nsub)> i = 0; i < nsub; ++i) {
335 my_mapping_single[my_subset[i]] = i;
336 }
337 }
338
339private:
340 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
341 SubsetStorage_ my_subset;
342 bool my_by_row;
343 std::vector<Index_> my_mapping_single;
344
345public:
346 Index_ nrow() const {
347 if (my_by_row) {
348 return my_subset.size();
349 } else {
350 return my_matrix->nrow();
351 }
352 }
353
354 Index_ ncol() const {
355 if (my_by_row) {
356 return my_matrix->ncol();
357 } else {
358 return my_subset.size();
359 }
360 }
361
362 bool is_sparse() const {
363 return my_matrix->is_sparse();
364 }
365
366 double is_sparse_proportion() const {
367 return my_matrix->is_sparse_proportion();
368 }
369
370 bool prefer_rows() const {
371 return my_matrix->prefer_rows();
372 }
373
374 double prefer_rows_proportion() const {
375 return my_matrix->prefer_rows_proportion();
376 }
377
378 bool uses_oracle(const bool row) const {
379 return my_matrix->uses_oracle(row);
380 }
381
382 using Matrix<Value_, Index_>::dense_column;
383
384 using Matrix<Value_, Index_>::dense_row;
385
386 using Matrix<Value_, Index_>::sparse_column;
387
388 using Matrix<Value_, Index_>::sparse_row;
389
390 /********************
391 *** Myopic dense ***
392 ********************/
393private:
394 template<typename ... Args_>
395 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
396 const bool row,
397 Args_&& ... args
398 ) const {
399 if (row == my_by_row) {
400 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
401 *my_matrix,
402 my_subset,
403 row,
404 std::forward<Args_>(args)...
405 );
406 } else {
407 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<false, Value_, Index_> >(
408 *my_matrix,
409 my_subset,
410 row,
411 false,
412 std::forward<Args_>(args)...
413 );
414 }
415 }
416
417public:
418 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
419 const bool row,
420 const Options& opt
421 ) const {
422 return populate_myopic_dense(row, opt);
423 }
424
425 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
426 const bool row,
427 const Index_ block_start,
428 const Index_ block_length,
429 const Options& opt
430 ) const {
431 return populate_myopic_dense(row, block_start, block_length, opt);
432 }
433
434 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
435 const bool row,
436 VectorPtr<Index_> indices_ptr,
437 const Options& opt
438 ) const {
439 return populate_myopic_dense(row, std::move(indices_ptr), opt);
440 }
441
442 /*********************
443 *** Myopic sparse ***
444 *********************/
445private:
446 template<typename ... Args_>
447 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
448 const bool row,
449 Args_&& ... args
450 ) const {
451 if (row == my_by_row) {
452 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
453 *my_matrix,
454 my_subset,
455 row,
456 std::forward<Args_>(args)...
457 );
458 } else {
459 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<false, Value_, Index_> >(
460 *my_matrix,
461 my_subset,
462 my_mapping_single,
463 row,
464 false,
465 std::forward<Args_>(args)...
466 );
467 }
468 }
469
470public:
471 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
472 const bool row,
473 const Options& opt
474 ) const {
475 return populate_myopic_sparse(row, opt);
476 }
477
478 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
479 const bool row,
480 const Index_ block_start,
481 const Index_ block_length,
482 const Options& opt
483 ) const {
484 return populate_myopic_sparse(row, block_start, block_length, opt);
485 }
486
487 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
488 const bool row,
489 VectorPtr<Index_> indices_ptr,
490 const Options& opt
491 ) const {
492 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
493 }
494
495 /**********************
496 *** Oracular dense ***
497 **********************/
498private:
499 template<typename ... Args_>
500 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
501 const bool row,
502 std::shared_ptr<const Oracle<Index_> > oracle,
503 Args_&& ... args
504 ) const {
505 if (row == my_by_row) {
506 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
507 *my_matrix,
508 my_subset,
509 row,
510 std::move(oracle),
511 std::forward<Args_>(args)...
512 );
513 } else {
514 return std::make_unique<DelayedSubsetUnique_internal::ParallelDense<true, Value_, Index_> >(
515 *my_matrix,
516 my_subset,
517 row,
518 std::move(oracle),
519 std::forward<Args_>(args)...
520 );
521 }
522 }
523
524public:
525 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
526 const bool row,
527 std::shared_ptr<const Oracle<Index_> > oracle,
528 const Options& opt
529 ) const {
530 return populate_oracular_dense(row, std::move(oracle), opt);
531 }
532
533 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
534 const bool row,
535 std::shared_ptr<const Oracle<Index_> > oracle,
536 const Index_ block_start,
537 const Index_ block_length,
538 const Options& opt
539 ) const {
540 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
541 }
542
543 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
544 const bool row,
545 std::shared_ptr<const Oracle<Index_> > oracle,
546 VectorPtr<Index_> indices_ptr,
547 const Options& opt
548 ) const {
549 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
550 }
551
552 /***********************
553 *** Oracular sparse ***
554 ***********************/
555private:
556 template<typename ... Args_>
557 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
558 const bool row,
559 std::shared_ptr<const Oracle<Index_> > oracle,
560 Args_&& ... args
561 ) const {
562 if (row == my_by_row) {
563 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
564 *my_matrix,
565 my_subset,
566 row,
567 std::move(oracle),
568 std::forward<Args_>(args)...
569 );
570 } else {
571 return std::make_unique<DelayedSubsetUnique_internal::ParallelSparse<true, Value_, Index_> >(
572 *my_matrix,
573 my_subset,
574 my_mapping_single,
575 row,
576 std::move(oracle),
577 std::forward<Args_>(args)...
578 );
579 }
580 }
581
582public:
583 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
584 const bool row,
585 std::shared_ptr<const Oracle<Index_> > oracle,
586 const Options& opt
587 ) const {
588 return populate_oracular_sparse(row, std::move(oracle), opt);
589 }
590
591 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
592 const bool row,
593 std::shared_ptr<const Oracle<Index_> > oracle,
594 const Index_ block_start,
595 const Index_ block_length,
596 const Options& opt
597 ) const {
598 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
599 }
600
601 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
602 const bool row,
603 std::shared_ptr<const Oracle<Index_> > oracle,
604 VectorPtr<Index_> indices_ptr,
605 const Options& opt
606 ) const {
607 return populate_oracular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
608 }
609};
610
611}
612
613#endif
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.
Templated construction of a new extractor.
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