tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DelayedBind.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DELAYED_BIND_HPP
2#define TATAMI_DELAYED_BIND_HPP
3
4#include "../base/Matrix.hpp"
7#include "../utils/copy.hpp"
9
10#include "CompressedMapping.hpp"
11#include "split_oracles.hpp"
12
13#include <numeric>
14#include <algorithm>
15#include <memory>
16#include <array>
17#include <type_traits>
18#include <cstddef>
19#include <limits>
20
29namespace tatami {
30
34namespace DelayedBind_internal {
35
36template<typename Index_, class Initialize_>
37Index_ initialize_parallel_block(
38 const std::vector<Index_>& cumulative,
39 const CompressedMapping<Index_>& mapping,
40 const Index_ block_start,
41 const Index_ block_length,
42 const Initialize_ init
43) {
44 const Index_ nmats = cumulative.size() - 1; // Number of matrices is guaranteed to fit in Index_, see reasoning in the DelayedBind constructor.
45 if (nmats == 0) {
46 return 0;
47 }
48
49 const Index_ start_index = mapping[block_start];
50 Index_ actual_start = block_start - cumulative[start_index];
51 const Index_ block_end = block_start + block_length;
52
53 for (Index_ index = start_index; index < nmats; ++index) {
54 const Index_ submat_end = cumulative[index + 1];
55 const bool not_final = (block_end > submat_end);
56 const Index_ actual_end = (not_final ? submat_end : block_end) - cumulative[index];
57 init(index, actual_start, actual_end - actual_start);
58 if (!not_final) {
59 break;
60 }
61 actual_start = 0;
62 }
63
64 return start_index;
65}
66
67template<typename Index_, class Initialize_>
68void initialize_parallel_index(
69 const std::vector<Index_>& cumulative,
70 const CompressedMapping<Index_>& mapping,
71 const std::vector<Index_>& indices,
72 const Initialize_ init
73) {
74 Index_ counter = 0;
75 const Index_ il = indices.size();
76 while (counter < il) {
77 const Index_ first_index = indices[counter];
78 const Index_ bind_index = mapping[first_index];
79 const Index_ lower = cumulative[bind_index];
80 const Index_ upper = cumulative[bind_index + 1];
81
82 // Creating the slice with one element already.
83 auto slice_ptr = std::make_shared<std::vector<Index_> >(1, first_index - lower);
84 ++counter;
85
86 while (counter < il && indices[counter] < upper) {
87 slice_ptr->push_back(indices[counter] - lower);
88 ++counter;
89 }
90
91 init(bind_index, std::move(slice_ptr));
92 }
93}
94
95/**********************
96 *** Dense parallel ***
97 **********************/
98
99template<bool oracle_, typename Value_, typename Index_>
100class ParallelDense final : public DenseExtractor<oracle_, Value_, Index_> {
101public:
102 ParallelDense(
103 const std::vector<Index_>&, // Not used, just provided for consistency with other constructors.
104 const CompressedMapping<Index_>&, // Ditto.
105 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
106 const bool row,
107 MaybeOracle<oracle_, Index_> oracle,
108 const Options& opt
109 ) {
110 my_exts.reserve(matrices.size());
111 my_count.reserve(matrices.size());
112 for (const auto& m : matrices) {
113 my_count.emplace_back(row ? m->ncol() : m->nrow());
114 my_exts.emplace_back(make_Extractor<false, oracle_>(*m, row, oracle, opt));
115 }
116 }
117
118 ParallelDense(
119 const std::vector<Index_>& cumulative,
120 const CompressedMapping<Index_>& mapping,
121 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
122 const bool row,
123 MaybeOracle<oracle_, Index_> oracle,
124 const Index_ block_start,
125 const Index_ block_length,
126 const Options& opt
127 ) {
128 my_exts.reserve(matrices.size());
129 my_count.reserve(matrices.size());
130 initialize_parallel_block(
131 cumulative,
132 mapping,
133 block_start,
134 block_length,
135 [&](const Index_ i, const Index_ sub_block_start, const Index_ sub_block_length) -> void {
136 my_count.emplace_back(sub_block_length);
137 my_exts.emplace_back(make_Extractor<false, oracle_>(*matrices[i], row, oracle, sub_block_start, sub_block_length, opt));
138 }
139 );
140 }
141
142 ParallelDense(
143 const std::vector<Index_>& cumulative,
144 const CompressedMapping<Index_>& mapping,
145 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
146 const bool row,
147 MaybeOracle<oracle_, Index_> oracle,
148 VectorPtr<Index_> indices_ptr,
149 const Options& opt
150 ) {
151 my_exts.reserve(matrices.size());
152 my_count.reserve(matrices.size());
153 initialize_parallel_index(
154 cumulative,
155 mapping,
156 *indices_ptr,
157 [&](const Index_ i, VectorPtr<Index_> sub_indices_ptr) -> void {
158 my_count.emplace_back(sub_indices_ptr->size());
159 my_exts.emplace_back(make_Extractor<false, oracle_>(*matrices[i], row, oracle, std::move(sub_indices_ptr), opt));
160 }
161 );
162 }
163
164public:
165 const Value_* fetch(const Index_ i, Value_* const buffer) {
166 auto copy = buffer;
167 const Index_ nmats = my_count.size();
168 for (Index_ x = 0; x < nmats; ++x) {
169 const auto ptr = my_exts[x]->fetch(i, copy);
170 const auto num = my_count[x];
171 copy_n(ptr, num, copy);
172 copy += num;
173 }
174 return buffer;
175 }
176
177private:
178 std::vector<std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > > my_exts;
179 std::vector<Index_> my_count;
180};
181
182/***********************
183 *** Sparse parallel ***
184 ***********************/
185
186template<bool oracle_, typename Value_, typename Index_>
187class SparseNonFirstExtractor final : public SparseExtractor<oracle_, Value_, Index_> {
188public:
189 SparseNonFirstExtractor(std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > ext, Index_ offset) : my_ext(std::move(ext)), my_offset(offset) {}
190
191 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
192 auto output = my_ext->fetch(i, value_buffer, index_buffer);
193 assert(output.index);
194 for (Index_ i = 0; i < output.number; ++i) {
195 index_buffer[i] = output.index[i] + my_offset;
196 }
197 output.index = index_buffer;
198 return output;
199 }
200private:
201 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
202 Index_ my_offset;
203};
204
205template<bool oracle_, typename Value_, typename Index_>
206class ParallelFullSparse final : public SparseExtractor<oracle_, Value_, Index_> {
207public:
208 ParallelFullSparse(
209 const std::vector<Index_>& cumulative,
210 const CompressedMapping<Index_>&, // not actually used, just provided for consistency with the other constructors.
211 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
212 const bool row,
213 MaybeOracle<oracle_, Index_> oracle,
214 const Options& opt
215 ) :
216 my_cumulative(cumulative),
217 my_needs_value(opt.sparse_extract_value),
218 my_needs_index(opt.sparse_extract_index)
219 {
220 my_exts.reserve(matrices.size());
221 for (const auto& m : matrices) {
222 my_exts.emplace_back(make_Extractor<true, oracle_>(*m, row, oracle, opt));
223 }
224 }
225
226 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
227 auto vcopy = value_buffer;
228 auto icopy = index_buffer;
229 Index_ accumulated = 0;
230
231 const Index_ nmats = my_exts.size();
232 for (Index_ x = 0; x < nmats; ++x) {
233 const auto range = my_exts[x]->fetch(i, vcopy, icopy);
234 accumulated += range.number;
235 if (my_needs_value) {
236 copy_n(range.value, range.number, vcopy);
237 vcopy += range.number;
238 }
239 if (my_needs_index) {
240 const auto offset = my_cumulative[x];
241
242 for (Index_ y = 0; y < range.number; ++y) {
243 icopy[y] = range.index[y] + offset;
244 }
245 icopy += range.number;
246 }
247 }
248
249 return SparseRange<Value_, Index_>(accumulated, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
250 }
251
252private:
253 const std::vector<Index_>& my_cumulative;
254 bool my_needs_value, my_needs_index;
255 std::vector<std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > > my_exts;
256};
257
258template<bool oracle_, typename Value_, typename Index_>
259class ParallelBlockSparse final : public SparseExtractor<oracle_, Value_, Index_> {
260public:
261 ParallelBlockSparse(
262 const std::vector<Index_>& cumulative,
263 const CompressedMapping<Index_>& mapping,
264 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
265 const bool row,
266 MaybeOracle<oracle_, Index_> oracle,
267 const Index_ block_start,
268 const Index_ block_length,
269 const Options& opt
270 ) :
271 my_cumulative(cumulative),
272 my_needs_value(opt.sparse_extract_value),
273 my_needs_index(opt.sparse_extract_index)
274 {
275 my_exts.reserve(matrices.size());
276 my_start_matrix = initialize_parallel_block(
277 my_cumulative,
278 mapping,
279 block_start,
280 block_length,
281 [&](const Index_ i, const Index_ sub_block_start, const Index_ sub_block_length) -> void {
282 my_exts.emplace_back(make_Extractor<true, oracle_>(*matrices[i], row, oracle, sub_block_start, sub_block_length, opt));
283 }
284 );
285 }
286
287 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
288 auto vcopy = value_buffer;
289 auto icopy = index_buffer;
290 Index_ count = 0;
291
292 const Index_ nmats = my_exts.size();
293 for (Index_ x = 0; x < nmats; ++x) {
294 const auto range = my_exts[x]->fetch(i, vcopy, icopy);
295 count += range.number;
296 if (my_needs_value) {
297 copy_n(range.value, range.number, vcopy);
298 vcopy += range.number;
299 }
300 if (my_needs_index) {
301 const Index_ offset = my_cumulative[x + my_start_matrix];
302 for (Index_ y = 0; y < range.number; ++y) {
303 icopy[y] = range.index[y] + offset;
304 }
305 icopy += range.number;
306 }
307 }
308
309 return SparseRange<Value_, Index_>(count, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
310 }
311
312private:
313 const std::vector<Index_>& my_cumulative;
314 bool my_needs_value, my_needs_index;
315 std::vector<std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > > my_exts;
316 Index_ my_start_matrix;
317};
318
319template<bool oracle_, typename Value_, typename Index_>
320class ParallelIndexSparse final : public SparseExtractor<oracle_, Value_, Index_> {
321public:
322 ParallelIndexSparse(
323 const std::vector<Index_>& cumulative,
324 const CompressedMapping<Index_>& mapping,
325 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
326 const bool row,
327 MaybeOracle<oracle_, Index_> oracle,
328 VectorPtr<Index_> indices_ptr,
329 const Options& opt
330 ) :
331 my_cumulative(cumulative),
332 my_needs_value(opt.sparse_extract_value),
333 my_needs_index(opt.sparse_extract_index)
334 {
335 my_exts.reserve(matrices.size());
336 my_which_matrix.reserve(matrices.size());
337 initialize_parallel_index(
338 my_cumulative,
339 mapping,
340 *indices_ptr,
341 [&](const Index_ i, VectorPtr<Index_> sub_indices_ptr) -> void {
342 my_which_matrix.emplace_back(i);
343 my_exts.emplace_back(make_Extractor<true, oracle_>(*matrices[i], row, oracle, std::move(sub_indices_ptr), opt));
344 }
345 );
346 }
347
348 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
349 auto vcopy = value_buffer;
350 auto icopy = index_buffer;
351 Index_ count = 0;
352
353 const Index_ nmats = my_which_matrix.size();
354 for (Index_ x = 0; x < nmats; ++x) {
355 const auto range = my_exts[x]->fetch(i, vcopy, icopy);
356 count += range.number;
357 if (my_needs_value) {
358 copy_n(range.value, range.number, vcopy);
359 vcopy += range.number;
360 }
361
362 if (my_needs_index) {
363 const Index_ offset = my_cumulative[my_which_matrix[x]];
364 for (Index_ y = 0; y < range.number; ++y) {
365 icopy[y] = range.index[y] + offset;
366 }
367 icopy += range.number;
368 }
369 }
370
371 return SparseRange<Value_, Index_>(count, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
372 }
373
374private:
375 const std::vector<Index_>& my_cumulative;
376 bool my_needs_value, my_needs_index;
377 std::vector<std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > > my_exts;
378 std::vector<Index_> my_which_matrix;
379};
380
381/****************************
382 *** Myopic Perpendicular ***
383 ****************************/
384
385template<typename Value_, typename Index_>
386class MyopicPerpendicularDense final : public MyopicDenseExtractor<Value_, Index_> {
387public:
388 template<typename ... Args_>
389 MyopicPerpendicularDense(
390 const std::vector<Index_>& cumulative,
391 const CompressedMapping<Index_>& mapping,
392 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
393 const bool row,
394 const Args_& ... args
395 ) :
396 my_cumulative(cumulative),
397 my_mapping(mapping)
398 {
399 my_exts.reserve(matrices.size());
400 for (const auto& m : matrices) {
401 my_exts.emplace_back(m->dense(row, args...));
402 }
403 }
404
405 const Value_* fetch(const Index_ i, Value_* const buffer) {
406 const Index_ chosen = my_mapping[i];
407 return my_exts[chosen]->fetch(i - my_cumulative[chosen], buffer);
408 }
409
410private:
411 const std::vector<Index_>& my_cumulative;
412 const CompressedMapping<Index_>& my_mapping;
413 std::vector<std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > > my_exts;
414};
415
416template<typename Value_, typename Index_>
417class MyopicPerpendicularSparse final : public MyopicSparseExtractor<Value_, Index_> {
418public:
419 template<typename ... Args_>
420 MyopicPerpendicularSparse(
421 const std::vector<Index_>& cumulative,
422 const CompressedMapping<Index_>& mapping,
423 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
424 const bool row,
425 const Args_& ... args
426 ) :
427 my_cumulative(cumulative),
428 my_mapping(mapping)
429 {
430 my_exts.reserve(matrices.size());
431 for (const auto& m : matrices) {
432 my_exts.emplace_back(m->sparse(row, args...));
433 }
434 }
435
436 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const vbuffer, Index_* const ibuffer) {
437 const Index_ chosen = my_mapping[i];
438 return my_exts[chosen]->fetch(i - my_cumulative[chosen], vbuffer, ibuffer);
439 }
440
441private:
442 const std::vector<Index_>& my_cumulative;
443 const CompressedMapping<Index_>& my_mapping;
444 std::vector<std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > > my_exts;
445};
446
447/******************************
448 *** Oracular Perpendicular ***
449 ******************************/
450
451template<typename Value_, typename Index_>
452class OracularPerpendicularDense final : public OracularDenseExtractor<Value_, Index_> {
453public:
454 template<typename ... Args_>
455 OracularPerpendicularDense(
456 const std::vector<Index_>& cumulative,
457 const CompressedMapping<Index_>& mapping,
458 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
459 const bool row,
460 std::shared_ptr<const Oracle<Index_> > ora,
461 std::vector<SplitPredictions<Index_> > split_pred,
462 const Args_& ... args
463 ) :
464 my_mapping(mapping),
465 my_oracle(std::move(ora))
466 {
467 resize_container_to_Index_size(my_exts, matrices.size()); // number of matrices should fit in an Index_, so this call is allowed.
468 split_oracles(
469 cumulative,
470 my_oracle,
471 std::move(split_pred),
472 [&](const Index_ x, std::shared_ptr<const Oracle<Index_> > subora) -> void {
473 my_exts[x] = matrices[x]->dense(row, std::move(subora), args...);
474 }
475 );
476 }
477
478 const Value_* fetch(const Index_ i, Value_* const buffer) {
479 const auto chosen = my_mapping[my_oracle->get(my_used++)];
480 const auto output = my_exts[chosen]->fetch(i, buffer);
481 return output;
482 }
483
484private:
485 const CompressedMapping<Index_>& my_mapping;
486 std::shared_ptr<const Oracle<Index_> > my_oracle;
487 PredictionIndex my_used = 0;
488 std::vector<std::unique_ptr<OracularDenseExtractor<Value_, Index_> > > my_exts;
489};
490
491template<typename Value_, typename Index_>
492class OracularPerpendicularSparse final : public OracularSparseExtractor<Value_, Index_> {
493public:
494 template<typename ... Args_>
495 OracularPerpendicularSparse(
496 const std::vector<Index_>& cumulative,
497 const CompressedMapping<Index_>& mapping,
498 const std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >& matrices,
499 const bool row,
500 std::shared_ptr<const Oracle<Index_> > ora,
501 std::vector<SplitPredictions<Index_> > split_pred,
502 const Args_& ... args
503 ) :
504 my_mapping(mapping),
505 my_oracle(std::move(ora))
506 {
507 resize_container_to_Index_size(my_exts, matrices.size()); // number of matrices should fit in an Index_, so this call is legal.
508 split_oracles(
509 cumulative,
510 my_oracle,
511 std::move(split_pred),
512 [&](const Index_ x, std::shared_ptr<const Oracle<Index_> > subora) -> void {
513 my_exts[x] = matrices[x]->sparse(row, std::move(subora), args...);
514 }
515 );
516 }
517
518 SparseRange<Value_, Index_> fetch(Index_ i, Value_* vbuffer, Index_* ibuffer) {
519 const auto chosen = my_mapping[my_oracle->get(my_used++)];
520 const auto output = my_exts[chosen]->fetch(i, vbuffer, ibuffer);
521 return output;
522 }
523
524private:
525 const CompressedMapping<Index_>& my_mapping;
526 std::shared_ptr<const Oracle<Index_> > my_oracle;
527 PredictionIndex my_used = 0;
528 std::vector<std::unique_ptr<OracularSparseExtractor<Value_, Index_> > > my_exts;
529};
530
531}
545template<typename Value_, typename Index_>
546class DelayedBind final : public Matrix<Value_, Index_> {
547public:
554 DelayedBind(std::vector<std::shared_ptr<const Matrix<Value_, Index_> > > matrices, const bool by_row) :
555 my_matrices(std::move(matrices)), my_by_row(by_row)
556 {
557 auto nmats = my_matrices.size();
558 my_cumulative.reserve(sanisizer::sum<I<decltype(my_cumulative.size())> >(nmats, 1));
559 I<decltype(nmats)> sofar = 0;
560 my_cumulative.push_back(0);
561 Index_ min_extent = std::numeric_limits<Index_>::max();
562
563 for (I<decltype(nmats)> i = 0; i < nmats; ++i) {
564 auto& current = my_matrices[i];
565 Index_ primary, secondary;
566 if (my_by_row) {
567 primary = current->nrow();
568 secondary = current->ncol();
569 } else {
570 primary = current->ncol();
571 secondary = current->nrow();
572 }
573
574 if (i == 0) {
575 my_otherdim = secondary;
576 } else if (my_otherdim != secondary) {
577 throw std::runtime_error("all 'matrices' should have the same number of " + (my_by_row ? std::string("columns") : std::string("rows")));
578 }
579
580 // Removing the matrices that don't contribute anything,
581 // so we don't have to deal with their overhead.
582 if (primary > 0) {
583 if (sofar != i) {
584 my_matrices[sofar] = std::move(current);
585 }
586 my_cumulative.push_back(sanisizer::sum<Index_>(attest_for_Index(my_cumulative.back()), attest_for_Index(primary)));
587 min_extent = std::min(min_extent, primary);
588 ++sofar;
589 }
590 }
591
592 my_matrices.resize(sofar);
593 nmats = sofar;
594
595 if (nmats) {
596 my_mapping = DelayedBind_internal::CompressedMapping(min_extent, my_cumulative);
597 }
598
599 double denom = 0;
600 for (const auto& x : my_matrices) {
601 const double total = static_cast<double>(x->nrow()) * static_cast<double>(x->ncol());
602 denom += total;
603 my_sparse_prop += total * x->is_sparse_proportion();
604 my_by_row_prop += total * x->prefer_rows_proportion();
605 }
606 if (denom) {
607 my_sparse_prop /= denom;
608 my_by_row_prop /= denom;
609 }
610
611 for (int d = 0; d < 2; ++d) {
612 my_uses_oracle[d] = false;
613 for (const auto& x : my_matrices) {
614 if (x->uses_oracle(d)) {
615 my_uses_oracle[d] = true;
616 break;
617 }
618 }
619 }
620 }
621
625 // Soft-deprecated.
626 DelayedBind(const std::vector<std::shared_ptr<Matrix<Value_, Index_> > >& matrices, const bool by_row) :
627 DelayedBind(std::vector<std::shared_ptr<const Matrix<Value_, Index_> > >(matrices.begin(), matrices.end()), by_row) {}
632private:
633 std::vector<std::shared_ptr<const Matrix<Value_, Index_> > > my_matrices;
634 bool my_by_row;
635
636 Index_ my_otherdim = 0;
637 std::vector<Index_> my_cumulative;
638 DelayedBind_internal::CompressedMapping<Index_> my_mapping;
639
640 double my_sparse_prop = 0, my_by_row_prop = 0;
641 std::array<bool, 2> my_uses_oracle;
642
643public:
644 Index_ nrow() const {
645 if (my_by_row) {
646 return my_cumulative.back();
647 } else {
648 return my_otherdim;
649 }
650 }
651
652 Index_ ncol() const {
653 if (my_by_row) {
654 return my_otherdim;
655 } else {
656 return my_cumulative.back();
657 }
658 }
659
660 bool is_sparse() const {
661 return my_sparse_prop > 0.5;
662 }
663
664 double is_sparse_proportion() const {
665 return my_sparse_prop;
666 }
667
668 bool prefer_rows() const {
669 return my_by_row_prop > 0.5;
670 }
671
672 double prefer_rows_proportion() const {
673 return my_by_row_prop;
674 }
675
676 bool uses_oracle(const bool row) const {
677 return my_uses_oracle[row];
678 }
679
680 using Matrix<Value_, Index_>::dense;
681
682 using Matrix<Value_, Index_>::sparse;
683
684 /**********************************
685 ******* Parallel internals *******
686 **********************************/
687private:
688 template<bool sparse_, bool oracle_>
689 std::unique_ptr<Extractor<sparse_, oracle_, Value_, Index_> > create_parallel_block_extractor(
690 const bool row,
692 const Index_ block_start,
693 const Index_ block_length,
694 const Options& opt
695 ) const {
696 if (block_length) {
697 const auto chosen = my_mapping[block_start];
698 if (chosen == my_mapping[block_start + block_length - 1]) {
699 const auto offset = my_cumulative[chosen];
700
702 *my_matrices[chosen],
703 row,
704 std::move(ora),
705 static_cast<Index_>(block_start - offset),
706 block_length,
707 opt
708 );
709
710 if constexpr(sparse_) {
711 if (offset && opt.sparse_extract_index) {
712 return std::make_unique<DelayedBind_internal::SparseNonFirstExtractor<oracle_, Value_, Index_> >(std::move(ext), offset);
713 }
714 }
715 return ext;
716 }
717 }
718
719 return std::make_unique<
720 typename std::conditional<
721 sparse_,
722 DelayedBind_internal::ParallelBlockSparse<oracle_, Value_, Index_>,
723 DelayedBind_internal::ParallelDense<oracle_, Value_, Index_>
724 >::type
725 >(
726 my_cumulative,
727 my_mapping,
728 my_matrices,
729 row,
730 std::move(ora),
731 block_start,
732 block_length,
733 opt
734 );
735 }
736
737 template<bool sparse_, bool oracle_>
738 std::unique_ptr<Extractor<sparse_, oracle_, Value_, Index_> > create_parallel_index_extractor(
739 const bool row,
741 VectorPtr<Index_> indices_ptr,
742 const Options& opt
743 ) const {
744 if (indices_ptr->size()) {
745 const auto chosen = my_mapping[indices_ptr->front()];
746 if (chosen == my_mapping[indices_ptr->back()]) {
747 const auto offset = my_cumulative[chosen];
748
749 if (offset) {
750 const auto ptr2 = new std::vector<Index_>(*indices_ptr);
751 indices_ptr.reset(ptr2);
752 for (auto& i : *ptr2) {
753 i -= offset;
754 }
755 }
756
758 *my_matrices[chosen],
759 row,
760 std::move(ora),
761 std::move(indices_ptr),
762 opt
763 );
764
765 if constexpr(sparse_) {
766 if (offset && opt.sparse_extract_index) {
767 return std::make_unique<DelayedBind_internal::SparseNonFirstExtractor<oracle_, Value_, Index_> >(std::move(ext), offset);
768 }
769 }
770 return ext;
771 }
772 }
773
774 return std::make_unique<
775 typename std::conditional<
776 sparse_,
777 DelayedBind_internal::ParallelIndexSparse<oracle_, Value_, Index_>,
778 DelayedBind_internal::ParallelDense<oracle_, Value_, Index_>
779 >::type
780 >(
781 my_cumulative,
782 my_mapping,
783 my_matrices,
784 row,
785 std::move(ora),
786 std::move(indices_ptr),
787 opt
788 );
789 }
790
791 /**********************************
792 ********** Myopic dense **********
793 **********************************/
794public:
795 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
796 const bool row,
797 const Options& opt
798 ) const {
799 if (my_matrices.size() == 1) {
800 return my_matrices[0]->dense(row, opt);
801
802 } else if (row == my_by_row) {
803 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
804 my_cumulative,
805 my_mapping,
806 my_matrices,
807 row,
808 opt
809 );
810
811 } else {
812 return std::make_unique<DelayedBind_internal::ParallelDense<false, Value_, Index_> >(
813 my_cumulative,
814 my_mapping,
815 my_matrices,
816 row,
817 false,
818 opt
819 );
820 }
821 }
822
823 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
824 const bool row,
825 const Index_ block_start,
826 const Index_ block_length,
827 const Options& opt
828 ) const {
829 if (my_matrices.size() == 1) {
830 return my_matrices[0]->dense(row, block_start, block_length, opt);
831
832 } else if (row == my_by_row) {
833 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
834 my_cumulative,
835 my_mapping,
836 my_matrices,
837 row,
838 block_start,
839 block_length,
840 opt
841 );
842
843 } else {
844 return create_parallel_block_extractor<false, false>(row, false, block_start, block_length, opt);
845 }
846 }
847
848 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
849 const bool row,
850 VectorPtr<Index_> indices_ptr,
851 const Options& opt
852 ) const {
853 if (my_matrices.size() == 1) {
854 return my_matrices[0]->dense(row, std::move(indices_ptr), opt);
855
856 } else if (row == my_by_row) {
857 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
858 my_cumulative,
859 my_mapping,
860 my_matrices,
861 row,
862 std::move(indices_ptr),
863 opt
864 );
865
866 } else {
867 return create_parallel_index_extractor<false, false>(row, false, std::move(indices_ptr), opt);
868 }
869 }
870
871 /***********************************
872 ********** Myopic sparse **********
873 ***********************************/
874private:
875 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
876 const bool row,
877 const Options& opt
878 ) const {
879 if (my_matrices.size() == 1) {
880 return my_matrices[0]->sparse(row, opt);
881
882 } else if (row == my_by_row) {
883 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
884 my_cumulative,
885 my_mapping,
886 my_matrices,
887 row,
888 opt
889 );
890
891 } else {
892 return std::make_unique<DelayedBind_internal::ParallelFullSparse<false, Value_, Index_> >(
893 my_cumulative,
894 my_mapping,
895 my_matrices,
896 row,
897 false,
898 opt
899 );
900 }
901 }
902
903 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
904 const bool row,
905 const Index_ block_start,
906 const Index_ block_length,
907 const Options& opt
908 ) const {
909 if (my_matrices.size() == 1) {
910 return my_matrices[0]->sparse(row, block_start, block_length, opt);
911
912 } else if (row == my_by_row) {
913 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
914 my_cumulative,
915 my_mapping,
916 my_matrices,
917 row,
918 block_start,
919 block_length,
920 opt
921 );
922
923 } else {
924 return create_parallel_block_extractor<true, false>(row, false, block_start, block_length, opt);
925 }
926 }
927
928 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
929 const bool row,
930 VectorPtr<Index_> indices_ptr,
931 const Options& opt
932 ) const {
933 if (my_matrices.size() == 1) {
934 return my_matrices[0]->sparse(row, std::move(indices_ptr), opt);
935
936 } else if (row == my_by_row) {
937 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
938 my_cumulative,
939 my_mapping,
940 my_matrices,
941 row,
942 std::move(indices_ptr),
943 opt
944 );
945
946 } else {
947 return create_parallel_index_extractor<true, false>(row, false, std::move(indices_ptr), opt);
948 }
949 }
950
951 /************************************
952 ********** Oracular dense **********
953 ************************************/
954private:
955 template<typename ... Args_>
956 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > create_oracular_perpendicular_dense(
957 bool row,
958 std::shared_ptr<const Oracle<Index_> > oracle,
959 Args_&&... args
960 ) const {
961 auto split_pred = DelayedBind_internal::split_predictions(my_cumulative, my_mapping, *oracle);
962 const auto lone_comp = DelayedBind_internal::uses_lone_component(split_pred);
963
964 if (!lone_comp.has_value()) {
965 return std::make_unique<DelayedBind_internal::OracularPerpendicularDense<Value_, Index_> >(
966 my_cumulative,
967 my_mapping,
968 my_matrices,
969 row,
970 std::move(oracle),
971 std::move(split_pred),
972 std::forward<Args_>(args)...
973 );
974 }
975
976 // If there's only a single component matrix that's used in the predictions,
977 // we return its extractor directly, possibly with a wrapper around the oracle.
978 if (*lone_comp == 0) {
979 return my_matrices[0]->dense(row, std::move(oracle), std::forward<Args_>(args)...);
980 } else {
981 const auto num_preds = split_pred[*lone_comp].number_of_calls;
982 return my_matrices[*lone_comp]->dense(
983 row,
984 std::make_shared<DelayedBind_internal::OffsetOracleWrapper<Index_> >(std::move(oracle), 0, num_preds, my_cumulative[*lone_comp]),
985 std::forward<Args_>(args)...
986 );
987 }
988 }
989
990public:
991 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
992 const bool row,
993 std::shared_ptr<const Oracle<Index_> > oracle,
994 const Options& opt
995 ) const {
996 if (my_matrices.size() == 1) {
997 return my_matrices[0]->dense(row, std::move(oracle), opt);
998
999 } else if (!my_uses_oracle[row]) {
1000 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1001 std::move(oracle),
1002 dense(row, opt)
1003 );
1004
1005 } else if (row == my_by_row) {
1006 return create_oracular_perpendicular_dense(row, std::move(oracle), opt);
1007
1008 } else {
1009 return std::make_unique<DelayedBind_internal::ParallelDense<true, Value_, Index_> >(
1010 my_cumulative,
1011 my_mapping,
1012 my_matrices,
1013 row,
1014 std::move(oracle),
1015 opt
1016 );
1017 }
1018 }
1019
1020 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
1021 const bool row,
1022 std::shared_ptr<const Oracle<Index_> > oracle,
1023 const Index_ block_start,
1024 const Index_ block_length,
1025 const Options& opt
1026 ) const {
1027 if (my_matrices.size() == 1) {
1028 return my_matrices[0]->dense(row, std::move(oracle), block_start, block_length, opt);
1029
1030 } else if (!my_uses_oracle[row]) {
1031 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1032 std::move(oracle),
1033 dense(row, block_start, block_length, opt)
1034 );
1035
1036 } else if (row == my_by_row) {
1037 return create_oracular_perpendicular_dense(row, std::move(oracle), block_start, block_length, opt);
1038
1039 } else {
1040 return create_parallel_block_extractor<false, true>(row, std::move(oracle), block_start, block_length, opt);
1041 }
1042 }
1043
1044 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
1045 const bool row,
1046 std::shared_ptr<const Oracle<Index_> > oracle,
1047 VectorPtr<Index_> indices_ptr,
1048 const Options& opt
1049 ) const {
1050 if (my_matrices.size() == 1) {
1051 return my_matrices[0]->dense(row, std::move(oracle), std::move(indices_ptr), opt);
1052
1053 } else if (!my_uses_oracle[row]) {
1054 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1055 std::move(oracle),
1056 dense(row, std::move(indices_ptr), opt)
1057 );
1058
1059 } else if (row == my_by_row) {
1060 return create_oracular_perpendicular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
1061
1062 } else {
1063 return create_parallel_index_extractor<false, true>(row, std::move(oracle), std::move(indices_ptr), opt);
1064 }
1065 }
1066
1067 /*************************************
1068 ********** Oracular sparse **********
1069 *************************************/
1070private:
1071 template<typename ... Args_>
1072 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > create_oracular_perpendicular_sparse(
1073 bool row,
1074 std::shared_ptr<const Oracle<Index_> > oracle,
1075 Args_&&... args
1076 ) const {
1077 auto split_pred = DelayedBind_internal::split_predictions(my_cumulative, my_mapping, *oracle);
1078 const auto lone_comp = DelayedBind_internal::uses_lone_component(split_pred);
1079
1080 if (!lone_comp.has_value()) {
1081 return std::make_unique<DelayedBind_internal::OracularPerpendicularSparse<Value_, Index_> >(
1082 my_cumulative,
1083 my_mapping,
1084 my_matrices,
1085 row,
1086 std::move(oracle),
1087 std::move(split_pred),
1088 std::forward<Args_>(args)...
1089 );
1090 }
1091
1092 // If there's only a single component matrix that's used in the predictions,
1093 // we return its extractor directly, possibly with a wrapper around the oracle.
1094 if (*lone_comp == 0) {
1095 return my_matrices[0]->sparse(row, std::move(oracle), std::forward<Args_>(args)...);
1096 } else {
1097 const auto num_preds = split_pred[*lone_comp].number_of_calls;
1098 return my_matrices[*lone_comp]->sparse(
1099 row,
1100 std::make_shared<DelayedBind_internal::OffsetOracleWrapper<Index_> >(std::move(oracle), 0, num_preds, my_cumulative[*lone_comp]),
1101 std::forward<Args_>(args)...
1102 );
1103 }
1104 }
1105
1106public:
1107 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
1108 const bool row,
1109 std::shared_ptr<const Oracle<Index_> > oracle,
1110 const Options& opt
1111 ) const {
1112 if (my_matrices.size() == 1) {
1113 return my_matrices[0]->sparse(row, std::move(oracle), opt);
1114
1115 } else if (!my_uses_oracle[row]) {
1116 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1117 std::move(oracle),
1118 sparse(row, opt)
1119 );
1120
1121 } else if (row == my_by_row) {
1122 return create_oracular_perpendicular_sparse(row, std::move(oracle), opt);
1123
1124 } else {
1125 return std::make_unique<DelayedBind_internal::ParallelFullSparse<true, Value_, Index_> >(
1126 my_cumulative,
1127 my_mapping,
1128 my_matrices,
1129 row,
1130 std::move(oracle),
1131 opt
1132 );
1133 }
1134 }
1135
1136 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
1137 const bool row,
1138 std::shared_ptr<const Oracle<Index_> > oracle,
1139 const Index_ block_start,
1140 const Index_ block_length,
1141 const Options& opt
1142 ) const {
1143 if (my_matrices.size() == 1) {
1144 return my_matrices[0]->sparse(row, std::move(oracle), block_start, block_length, opt);
1145
1146 } else if (!my_uses_oracle[row]) {
1147 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1148 std::move(oracle),
1149 sparse(row, block_start, block_length, opt)
1150 );
1151
1152 } else if (row == my_by_row) {
1153 return create_oracular_perpendicular_sparse(row, std::move(oracle), block_start, block_length, opt);
1154
1155 } else {
1156 return create_parallel_block_extractor<true, true>(row, std::move(oracle), block_start, block_length, opt);
1157 }
1158 }
1159
1160 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
1161 const bool row,
1162 std::shared_ptr<const Oracle<Index_> > oracle,
1163 VectorPtr<Index_> indices_ptr,
1164 const Options& opt
1165 ) const {
1166 if (my_matrices.size() == 1) {
1167 return my_matrices[0]->sparse(row, std::move(oracle), std::move(indices_ptr), opt);
1168
1169 } else if (!my_uses_oracle[row]) {
1170 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1171 std::move(oracle),
1172 sparse(row, std::move(indices_ptr), opt)
1173 );
1174
1175 } else if (row == my_by_row) {
1176 return create_oracular_perpendicular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
1177
1178 } else {
1179 return create_parallel_index_extractor<true, true>(row, std::move(oracle), std::move(indices_ptr), opt);
1180 }
1181 }
1182};
1183
1187// These methods are soft-deprecated: kept around for back-compatibility only.
1188template<typename Value_, typename Index_>
1189std::shared_ptr<Matrix<Value_, Index_> > make_DelayedBind(std::vector<std::shared_ptr<const Matrix<Value_, Index_> > > matrices, bool row) {
1190 return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedBind<Value_, Index_>(std::move(matrices), row));
1191}
1192
1193template<typename Value_, typename Index_>
1194std::shared_ptr<Matrix<Value_, Index_> > make_DelayedBind(std::vector<std::shared_ptr<Matrix<Value_, Index_> > > matrices, bool row) {
1195 return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedBind<Value_, Index_>(std::move(matrices), row));
1196}
1197
1198template<int margin_, typename Value_, typename Index_>
1199std::shared_ptr<Matrix<Value_, Index_> > make_DelayedBind(std::vector<std::shared_ptr<const Matrix<Value_, Index_> > > matrices) {
1200 return make_DelayedBind(std::move(matrices), margin_ == 0);
1201}
1202
1203template<int margin_, typename Value_, typename Index_>
1204std::shared_ptr<Matrix<Value_, Index_> > make_DelayedBind(std::vector<std::shared_ptr<Matrix<Value_, Index_> > > matrices) {
1205 return make_DelayedBind(std::move(matrices), margin_ == 0);
1206}
1211}
1212
1213#endif
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Mimic the oracle-aware extractor interface.
Delayed combining of a matrix.
Definition DelayedBind.hpp:546
bool uses_oracle(const bool row) const
Definition DelayedBind.hpp:676
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 DelayedBind.hpp:1044
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 DelayedBind.hpp:1160
DelayedBind(std::vector< std::shared_ptr< const Matrix< Value_, Index_ > > > matrices, const bool by_row)
Definition DelayedBind.hpp:554
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedBind.hpp:1107
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedBind.hpp:795
double is_sparse_proportion() const
Definition DelayedBind.hpp:664
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedBind.hpp:823
Index_ nrow() const
Definition DelayedBind.hpp:644
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedBind.hpp:848
bool is_sparse() const
Definition DelayedBind.hpp:660
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedBind.hpp:991
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 DelayedBind.hpp:1136
Index_ ncol() const
Definition DelayedBind.hpp:652
bool prefer_rows() const
Definition DelayedBind.hpp:668
double prefer_rows_proportion() const
Definition DelayedBind.hpp:672
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 DelayedBind.hpp:1020
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_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Definition make_Extractor.hpp:23
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:284
std::unique_ptr< Extractor< sparse_, oracle_, Value_, Index_ > > make_Extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, const tatami::Options &opt)
Definition make_Extractor.hpp:69
Value_ * copy_n(const Value_ *const input, const Size_ n, Value_ *const output)
Definition copy.hpp:37
std::size_t PredictionIndex
Definition Oracle.hpp:18
Options for accessing data from a Matrix instance.
Definition Options.hpp:30
bool sparse_extract_index
Definition Options.hpp:40