1#ifndef TATAMI_DELAYED_BIND_HPP
2#define TATAMI_DELAYED_BIND_HPP
10#include "CompressedMapping.hpp"
11#include "split_oracles.hpp"
34namespace DelayedBind_internal {
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
44 const Index_ nmats = cumulative.size() - 1;
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;
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);
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
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];
83 auto slice_ptr = std::make_shared<std::vector<Index_> >(1, first_index - lower);
86 while (counter < il && indices[counter] < upper) {
87 slice_ptr->push_back(indices[counter] - lower);
91 init(bind_index, std::move(slice_ptr));
99template<
bool oracle_,
typename Value_,
typename Index_>
100class ParallelDense final :
public DenseExtractor<oracle_, Value_, Index_> {
103 const std::vector<Index_>&,
104 const CompressedMapping<Index_>&,
105 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
107 MaybeOracle<oracle_, Index_> oracle,
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));
119 const std::vector<Index_>& cumulative,
120 const CompressedMapping<Index_>& mapping,
121 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
123 MaybeOracle<oracle_, Index_> oracle,
124 const Index_ block_start,
125 const Index_ block_length,
128 my_exts.reserve(matrices.size());
129 my_count.reserve(matrices.size());
130 initialize_parallel_block(
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));
143 const std::vector<Index_>& cumulative,
144 const CompressedMapping<Index_>& mapping,
145 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
147 MaybeOracle<oracle_, Index_> oracle,
148 VectorPtr<Index_> indices_ptr,
151 my_exts.reserve(matrices.size());
152 my_count.reserve(matrices.size());
153 initialize_parallel_index(
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));
165 const Value_* fetch(
const Index_ i, Value_*
const 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];
178 std::vector<std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > > my_exts;
179 std::vector<Index_> my_count;
186template<
bool oracle_,
typename Value_,
typename Index_>
187class SparseNonFirstExtractor final :
public SparseExtractor<oracle_, Value_, Index_> {
189 SparseNonFirstExtractor(std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > ext, Index_ offset) : my_ext(std::move(ext)), my_offset(offset) {}
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;
197 output.index = index_buffer;
201 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
205template<
bool oracle_,
typename Value_,
typename Index_>
206class ParallelFullSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
209 const std::vector<Index_>& cumulative,
210 const CompressedMapping<Index_>&,
211 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
213 MaybeOracle<oracle_, Index_> oracle,
216 my_cumulative(cumulative),
217 my_needs_value(opt.sparse_extract_value),
218 my_needs_index(opt.sparse_extract_index)
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));
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;
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;
239 if (my_needs_index) {
240 const auto offset = my_cumulative[x];
242 for (Index_ y = 0; y < range.number; ++y) {
243 icopy[y] = range.index[y] + offset;
245 icopy += range.number;
249 return SparseRange<Value_, Index_>(accumulated, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
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;
258template<
bool oracle_,
typename Value_,
typename Index_>
259class ParallelBlockSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
262 const std::vector<Index_>& cumulative,
263 const CompressedMapping<Index_>& mapping,
264 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
266 MaybeOracle<oracle_, Index_> oracle,
267 const Index_ block_start,
268 const Index_ block_length,
271 my_cumulative(cumulative),
272 my_needs_value(opt.sparse_extract_value),
273 my_needs_index(opt.sparse_extract_index)
275 my_exts.reserve(matrices.size());
276 my_start_matrix = initialize_parallel_block(
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));
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;
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;
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;
305 icopy += range.number;
309 return SparseRange<Value_, Index_>(count, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
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;
319template<
bool oracle_,
typename Value_,
typename Index_>
320class ParallelIndexSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
323 const std::vector<Index_>& cumulative,
324 const CompressedMapping<Index_>& mapping,
325 const std::vector<std::shared_ptr<
const Matrix<Value_, Index_> > >& matrices,
327 MaybeOracle<oracle_, Index_> oracle,
328 VectorPtr<Index_> indices_ptr,
331 my_cumulative(cumulative),
332 my_needs_value(opt.sparse_extract_value),
333 my_needs_index(opt.sparse_extract_index)
335 my_exts.reserve(matrices.size());
336 my_which_matrix.reserve(matrices.size());
337 initialize_parallel_index(
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));
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;
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;
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;
367 icopy += range.number;
371 return SparseRange<Value_, Index_>(count, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
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;
385template<
typename Value_,
typename Index_>
386class MyopicPerpendicularDense final :
public MyopicDenseExtractor<Value_, Index_> {
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,
394 const Args_& ... args
396 my_cumulative(cumulative),
399 my_exts.reserve(matrices.size());
400 for (
const auto& m : matrices) {
401 my_exts.emplace_back(m->dense(row, args...));
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);
411 const std::vector<Index_>& my_cumulative;
412 const CompressedMapping<Index_>& my_mapping;
413 std::vector<std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > > my_exts;
416template<
typename Value_,
typename Index_>
417class MyopicPerpendicularSparse final :
public MyopicSparseExtractor<Value_, Index_> {
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,
425 const Args_& ... args
427 my_cumulative(cumulative),
430 my_exts.reserve(matrices.size());
431 for (
const auto& m : matrices) {
432 my_exts.emplace_back(m->sparse(row, args...));
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);
442 const std::vector<Index_>& my_cumulative;
443 const CompressedMapping<Index_>& my_mapping;
444 std::vector<std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > > my_exts;
451template<
typename Value_,
typename Index_>
452class OracularPerpendicularDense final :
public OracularDenseExtractor<Value_, Index_> {
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,
460 std::shared_ptr<
const Oracle<Index_> > ora,
461 std::vector<SplitPredictions<Index_> > split_pred,
462 const Args_& ... args
465 my_oracle(std::move(ora))
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...);
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);
485 const CompressedMapping<Index_>& my_mapping;
486 std::shared_ptr<const Oracle<Index_> > my_oracle;
488 std::vector<std::unique_ptr<OracularDenseExtractor<Value_, Index_> > > my_exts;
491template<
typename Value_,
typename Index_>
492class OracularPerpendicularSparse final :
public OracularSparseExtractor<Value_, Index_> {
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,
500 std::shared_ptr<
const Oracle<Index_> > ora,
501 std::vector<SplitPredictions<Index_> > split_pred,
502 const Args_& ... args
505 my_oracle(std::move(ora))
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...);
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);
525 const CompressedMapping<Index_>& my_mapping;
526 std::shared_ptr<const Oracle<Index_> > my_oracle;
528 std::vector<std::unique_ptr<OracularSparseExtractor<Value_, Index_> > > my_exts;
545template<
typename Value_,
typename Index_>
555 my_matrices(std::move(matrices)), my_by_row(by_row)
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();
563 for (I<
decltype(nmats)> i = 0; i < nmats; ++i) {
564 auto& current = my_matrices[i];
565 Index_ primary, secondary;
567 primary = current->nrow();
568 secondary = current->ncol();
570 primary = current->ncol();
571 secondary = current->nrow();
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")));
584 my_matrices[sofar] = std::move(current);
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);
592 my_matrices.resize(sofar);
596 my_mapping = DelayedBind_internal::CompressedMapping(min_extent, my_cumulative);
600 for (
const auto& x : my_matrices) {
601 const double total =
static_cast<double>(x->nrow()) *
static_cast<double>(x->ncol());
603 my_sparse_prop += total * x->is_sparse_proportion();
604 my_by_row_prop += total * x->prefer_rows_proportion();
607 my_sparse_prop /= denom;
608 my_by_row_prop /= denom;
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;
627 DelayedBind(std::vector<std::shared_ptr<const
Matrix<Value_, Index_> > >(matrices.begin(), matrices.end()), by_row) {}
633 std::vector<std::shared_ptr<const Matrix<Value_, Index_> > > my_matrices;
636 Index_ my_otherdim = 0;
637 std::vector<Index_> my_cumulative;
638 DelayedBind_internal::CompressedMapping<Index_> my_mapping;
640 double my_sparse_prop = 0, my_by_row_prop = 0;
641 std::array<bool, 2> my_uses_oracle;
646 return my_cumulative.back();
656 return my_cumulative.back();
661 return my_sparse_prop > 0.5;
665 return my_sparse_prop;
669 return my_by_row_prop > 0.5;
673 return my_by_row_prop;
677 return my_uses_oracle[row];
682 using Matrix<Value_, Index_>::sparse;
688 template<
bool sparse_,
bool oracle_>
689 std::unique_ptr<Extractor<sparse_, oracle_, Value_, Index_> > create_parallel_block_extractor(
692 const Index_ block_start,
693 const Index_ 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];
702 *my_matrices[chosen],
705 static_cast<Index_
>(block_start - offset),
710 if constexpr(sparse_) {
712 return std::make_unique<DelayedBind_internal::SparseNonFirstExtractor<oracle_, Value_, Index_> >(std::move(ext), offset);
719 return std::make_unique<
720 typename std::conditional<
722 DelayedBind_internal::ParallelBlockSparse<oracle_, Value_, Index_>,
723 DelayedBind_internal::ParallelDense<oracle_, Value_, Index_>
737 template<
bool sparse_,
bool oracle_>
738 std::unique_ptr<Extractor<sparse_, oracle_, Value_, Index_> > create_parallel_index_extractor(
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];
750 const auto ptr2 =
new std::vector<Index_>(*indices_ptr);
751 indices_ptr.reset(ptr2);
752 for (
auto& i : *ptr2) {
758 *my_matrices[chosen],
761 std::move(indices_ptr),
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);
774 return std::make_unique<
775 typename std::conditional<
777 DelayedBind_internal::ParallelIndexSparse<oracle_, Value_, Index_>,
778 DelayedBind_internal::ParallelDense<oracle_, Value_, Index_>
786 std::move(indices_ptr),
795 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
799 if (my_matrices.size() == 1) {
800 return my_matrices[0]->dense(row, opt);
802 }
else if (row == my_by_row) {
803 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
812 return std::make_unique<DelayedBind_internal::ParallelDense<false, Value_, Index_> >(
823 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
825 const Index_ block_start,
826 const Index_ block_length,
829 if (my_matrices.size() == 1) {
830 return my_matrices[0]->dense(row, block_start, block_length, opt);
832 }
else if (row == my_by_row) {
833 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
844 return create_parallel_block_extractor<false, false>(row,
false, block_start, block_length, opt);
848 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
853 if (my_matrices.size() == 1) {
854 return my_matrices[0]->dense(row, std::move(indices_ptr), opt);
856 }
else if (row == my_by_row) {
857 return std::make_unique<DelayedBind_internal::MyopicPerpendicularDense<Value_, Index_> >(
862 std::move(indices_ptr),
867 return create_parallel_index_extractor<false, false>(row,
false, std::move(indices_ptr), opt);
875 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
879 if (my_matrices.size() == 1) {
880 return my_matrices[0]->sparse(row, opt);
882 }
else if (row == my_by_row) {
883 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
892 return std::make_unique<DelayedBind_internal::ParallelFullSparse<false, Value_, Index_> >(
903 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
905 const Index_ block_start,
906 const Index_ block_length,
909 if (my_matrices.size() == 1) {
910 return my_matrices[0]->sparse(row, block_start, block_length, opt);
912 }
else if (row == my_by_row) {
913 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
924 return create_parallel_block_extractor<true, false>(row,
false, block_start, block_length, opt);
928 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
933 if (my_matrices.size() == 1) {
934 return my_matrices[0]->sparse(row, std::move(indices_ptr), opt);
936 }
else if (row == my_by_row) {
937 return std::make_unique<DelayedBind_internal::MyopicPerpendicularSparse<Value_, Index_> >(
942 std::move(indices_ptr),
947 return create_parallel_index_extractor<true, false>(row,
false, std::move(indices_ptr), opt);
955 template<
typename ... Args_>
956 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > create_oracular_perpendicular_dense(
958 std::shared_ptr<
const Oracle<Index_> > oracle,
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);
964 if (!lone_comp.has_value()) {
965 return std::make_unique<DelayedBind_internal::OracularPerpendicularDense<Value_, Index_> >(
971 std::move(split_pred),
972 std::forward<Args_>(args)...
978 if (*lone_comp == 0) {
979 return my_matrices[0]->dense(row, std::move(oracle), std::forward<Args_>(args)...);
981 const auto num_preds = split_pred[*lone_comp].number_of_calls;
982 return my_matrices[*lone_comp]->dense(
984 std::make_shared<DelayedBind_internal::OffsetOracleWrapper<Index_> >(std::move(oracle), 0, num_preds, my_cumulative[*lone_comp]),
985 std::forward<Args_>(args)...
991 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
996 if (my_matrices.size() == 1) {
997 return my_matrices[0]->dense(row, std::move(oracle), opt);
999 }
else if (!my_uses_oracle[row]) {
1000 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1005 }
else if (row == my_by_row) {
1006 return create_oracular_perpendicular_dense(row, std::move(oracle), opt);
1009 return std::make_unique<DelayedBind_internal::ParallelDense<true, Value_, Index_> >(
1020 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
1023 const Index_ block_start,
1024 const Index_ block_length,
1027 if (my_matrices.size() == 1) {
1028 return my_matrices[0]->dense(row, std::move(oracle), block_start, block_length, opt);
1030 }
else if (!my_uses_oracle[row]) {
1031 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1033 dense(row, block_start, block_length, opt)
1036 }
else if (row == my_by_row) {
1037 return create_oracular_perpendicular_dense(row, std::move(oracle), block_start, block_length, opt);
1040 return create_parallel_block_extractor<false, true>(row, std::move(oracle), block_start, block_length, opt);
1044 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
1050 if (my_matrices.size() == 1) {
1051 return my_matrices[0]->dense(row, std::move(oracle), std::move(indices_ptr), opt);
1053 }
else if (!my_uses_oracle[row]) {
1054 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(
1056 dense(row, std::move(indices_ptr), opt)
1059 }
else if (row == my_by_row) {
1060 return create_oracular_perpendicular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
1063 return create_parallel_index_extractor<false, true>(row, std::move(oracle), std::move(indices_ptr), opt);
1071 template<
typename ... Args_>
1072 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > create_oracular_perpendicular_sparse(
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);
1080 if (!lone_comp.has_value()) {
1081 return std::make_unique<DelayedBind_internal::OracularPerpendicularSparse<Value_, Index_> >(
1087 std::move(split_pred),
1088 std::forward<Args_>(args)...
1094 if (*lone_comp == 0) {
1095 return my_matrices[0]->sparse(row, std::move(oracle), std::forward<Args_>(args)...);
1097 const auto num_preds = split_pred[*lone_comp].number_of_calls;
1098 return my_matrices[*lone_comp]->sparse(
1100 std::make_shared<DelayedBind_internal::OffsetOracleWrapper<Index_> >(std::move(oracle), 0, num_preds, my_cumulative[*lone_comp]),
1101 std::forward<Args_>(args)...
1107 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
1112 if (my_matrices.size() == 1) {
1113 return my_matrices[0]->sparse(row, std::move(oracle), opt);
1115 }
else if (!my_uses_oracle[row]) {
1116 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1121 }
else if (row == my_by_row) {
1122 return create_oracular_perpendicular_sparse(row, std::move(oracle), opt);
1125 return std::make_unique<DelayedBind_internal::ParallelFullSparse<true, Value_, Index_> >(
1136 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
1139 const Index_ block_start,
1140 const Index_ block_length,
1143 if (my_matrices.size() == 1) {
1144 return my_matrices[0]->sparse(row, std::move(oracle), block_start, block_length, opt);
1146 }
else if (!my_uses_oracle[row]) {
1147 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1149 sparse(row, block_start, block_length, opt)
1152 }
else if (row == my_by_row) {
1153 return create_oracular_perpendicular_sparse(row, std::move(oracle), block_start, block_length, opt);
1156 return create_parallel_block_extractor<true, true>(row, std::move(oracle), block_start, block_length, opt);
1160 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
1166 if (my_matrices.size() == 1) {
1167 return my_matrices[0]->sparse(row, std::move(oracle), std::move(indices_ptr), opt);
1169 }
else if (!my_uses_oracle[row]) {
1170 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(
1172 sparse(row, std::move(indices_ptr), opt)
1175 }
else if (row == my_by_row) {
1176 return create_oracular_perpendicular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
1179 return create_parallel_index_extractor<true, true>(row, std::move(oracle), std::move(indices_ptr), opt);
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));
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));
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);
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);
Convert index type to container size.
Virtual class for a matrix of some numeric type.
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.
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