1#ifndef TATAMI_COMPRESSED_SPARSE_MATRIX_H
2#define TATAMI_COMPRESSED_SPARSE_MATRIX_H
4#include "../base/Matrix.hpp"
5#include "primary_extraction.hpp"
6#include "secondary_extraction.hpp"
8#include "../utils/ElementType.hpp"
9#include "../utils/has_data.hpp"
10#include "../utils/PseudoOracularExtractor.hpp"
35template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
38 PrimaryMyopicFullDense(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary) :
41 my_pointers(pointers),
42 my_secondary(secondary)
45 const Value_* fetch(Index_ i, Value_* buffer) {
46 auto offset = my_pointers[i];
47 size_t delta = my_pointers[i+1] - my_pointers[i];
48 std::fill_n(buffer, my_secondary,
static_cast<Value_
>(0));
49 for (
size_t x = 0; x < delta; ++x) {
50 auto cur_offset = offset + x;
51 buffer[my_indices[cur_offset]] = my_values[cur_offset];
57 const ValueStorage_& my_values;
58 const IndexStorage_& my_indices;
59 const PointerStorage_& my_pointers;
63template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
66 PrimaryMyopicFullSparse(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary,
const Options& opt) :
69 my_pointers(pointers),
70 my_secondary(secondary),
71 my_needs_value(opt.sparse_extract_value),
72 my_needs_index(opt.sparse_extract_index)
75 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
76 auto offset = my_pointers[i];
77 auto delta = my_pointers[i+1] - my_pointers[i];
79 SparseRange<Value_, Index_> output(delta, NULL, NULL);
81 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
84 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
90 const ValueStorage_& my_values;
91 const IndexStorage_& my_indices;
92 const PointerStorage_& my_pointers;
94 bool my_needs_value, my_needs_index;
101template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
104 PrimaryMyopicBlockDense(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length) :
107 my_pointers(pointers),
108 my_secondary(secondary),
109 my_block_start(block_start),
110 my_block_length(block_length)
113 const Value_* fetch(Index_ i, Value_* buffer) {
114 auto iStart = my_indices.begin() + my_pointers[i];
115 auto iEnd = my_indices.begin() + my_pointers[i + 1];
116 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
117 size_t offset = (iStart - my_indices.begin());
118 size_t number = iEnd - iStart;
120 std::fill_n(buffer, my_block_length,
static_cast<Value_
>(0));
121 for (
size_t i = 0; i < number; ++i) {
122 auto cur_offset = offset + i;
123 buffer[my_indices[cur_offset] - my_block_start] = my_values[cur_offset];
129 const ValueStorage_& my_values;
130 const IndexStorage_& my_indices;
131 const PointerStorage_& my_pointers;
133 Index_ my_block_start, my_block_length;
136template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
139 PrimaryMyopicBlockSparse(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length,
const Options& opt) :
142 my_pointers(pointers),
143 my_secondary(secondary),
144 my_block_start(block_start),
145 my_block_length(block_length),
146 my_needs_value(opt.sparse_extract_value),
147 my_needs_index(opt.sparse_extract_index)
150 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
151 auto iStart = my_indices.begin() + my_pointers[i];
152 auto iEnd = my_indices.begin() + my_pointers[i + 1];
153 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
154 size_t offset = iStart - my_indices.begin();
155 size_t delta = iEnd - iStart;
157 SparseRange<Value_, Index_> output(delta, NULL, NULL);
158 if (my_needs_value) {
159 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
161 if (my_needs_index) {
162 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
168 const ValueStorage_& my_values;
169 const IndexStorage_& my_indices;
170 const PointerStorage_& my_pointers;
172 Index_ my_block_start, my_block_length;
173 bool my_needs_value, my_needs_index;
180template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
183 PrimaryMyopicIndexDense(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary,
const VectorPtr<Index_>& indices_ptr) :
186 my_pointers(pointers),
187 my_retriever(*indices_ptr, secondary),
188 my_num_indices(indices_ptr->size())
191 const Value_* fetch(Index_ i, Value_* buffer) {
192 std::fill_n(buffer, my_num_indices,
static_cast<Value_
>(0));
193 auto vIt = my_values.begin() + my_pointers[i];
194 my_retriever.populate(
195 my_indices.begin() + my_pointers[i],
196 my_indices.begin() + my_pointers[i+1],
197 [&](
size_t s,
size_t offset) {
198 buffer[s] = *(vIt + offset);
205 const ValueStorage_& my_values;
206 const IndexStorage_& my_indices;
207 const PointerStorage_& my_pointers;
208 sparse_utils::RetrievePrimarySubsetDense<Index_> my_retriever;
209 size_t my_num_indices;
212template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
215 PrimaryMyopicIndexSparse(
const ValueStorage_& values,
const IndexStorage_& indices,
const PointerStorage_& pointers, Index_ secondary,
const VectorPtr<Index_>& indices_ptr,
const Options& opt) :
218 my_pointers(pointers),
219 my_retriever(*indices_ptr, secondary),
220 my_needs_value(opt.sparse_extract_value),
221 my_needs_index(opt.sparse_extract_index) {}
223 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
225 auto vcopy = value_buffer;
226 auto icopy = index_buffer;
228 auto vIt = my_values.begin() + my_pointers[i];
229 my_retriever.populate(
230 my_indices.begin() + my_pointers[i],
231 my_indices.begin() + my_pointers[i+1],
232 [&](
size_t offset, Index_ ix) {
234 if (my_needs_value) {
235 *vcopy = *(vIt + offset);
238 if (my_needs_index) {
245 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
252 sparse_utils::RetrievePrimarySubsetSparse<Index_>
my_retriever;
253 bool my_needs_value, my_needs_index;
260template<
typename Index_,
class IndexStorage_,
class Po
interStorage_>
273 return my_pointers[primary];
277 return my_pointers[primary + 1];
281 return my_indices.begin();
285template<
typename Index_,
class IndexStorage_,
class Po
interStorage_>
290template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
308 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
311template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
317 my_needs_value(
opt.sparse_extract_value),
318 my_needs_index(
opt.sparse_extract_index)
324 if (my_needs_value) {
327 if (my_needs_index) {
337 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
338 bool my_needs_value, my_needs_index;
345template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
363 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
366template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
372 my_needs_value(
opt.sparse_extract_value),
373 my_needs_index(
opt.sparse_extract_index)
379 if (my_needs_value) {
382 if (my_needs_index) {
392 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
393 bool my_needs_value, my_needs_index;
400template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
416 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
419template<
typename Value_,
typename Index_,
class ValueStorage_,
class IndexStorage_,
class Po
interStorage_>
425 my_needs_value(
opt.sparse_extract_value),
426 my_needs_index(
opt.sparse_extract_index)
432 if (my_needs_value) {
435 if (my_needs_index) {
445 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> >
my_cache;
446 bool my_needs_value, my_needs_index;
473 class ValueStorage_ = std::vector<Value_>,
474 class IndexStorage_ = std::vector<Index_>,
475 class PointerStorage_ = std::vector<size_t>
494 CompressedSparseMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers,
bool csr,
bool check =
true) :
495 my_nrow(nrow), my_ncols(ncol), my_values(std::move(values)), my_indices(std::move(indices)), my_pointers(std::move(pointers)), my_csr(csr)
498 size_t nnzero = my_values.size();
499 if (nnzero != my_indices.size()) {
500 throw std::runtime_error(
"'my_values' and 'my_indices' should be of the same length");
503 size_t npointers = my_pointers.size();
505 if (npointers !=
static_cast<size_t>(my_nrow) + 1){
506 throw std::runtime_error(
"length of 'pointers' should be equal to 'nrow + 1'");
509 if (npointers !=
static_cast<size_t>(my_ncols) + 1){
510 throw std::runtime_error(
"length of 'pointers' should be equal to 'ncols + 1'");
514 if (my_pointers[0] != 0) {
515 throw std::runtime_error(
"first element of 'pointers' should be zero");
518 auto last = my_pointers[npointers - 1];
519 if (
static_cast<size_t>(last) != nnzero) {
520 throw std::runtime_error(
"last element of 'pointers' should be equal to length of 'indices'");
524 for (
size_t i = 1; i < npointers; ++i) {
525 auto start = my_pointers[i- 1], end = my_pointers[i];
526 if (end < start || end > last) {
527 throw std::runtime_error(
"'pointers' should be in non-decreasing order");
530 for (
auto x = start; x < end; ++x) {
531 if (my_indices[x] < 0 || my_indices[x] >= max_index) {
532 throw std::runtime_error(
"'indices' should contain non-negative integers less than the number of " + (my_csr ? std::string(
"columns") : std::string(
"rows")));
536 for (
decltype(start) j = start + 1; j < end; ++j) {
537 if (my_indices[j] <= my_indices[j - 1]) {
538 throw std::runtime_error(
"'indices' should be strictly increasing within each " + (my_csr ? std::string(
"row") : std::string(
"column")));
546 Index_ my_nrow, my_ncols;
547 ValueStorage_ my_values;
548 IndexStorage_ my_indices;
549 PointerStorage_ my_pointers;
553 Index_
nrow()
const {
return my_nrow; }
555 Index_
ncol()
const {
return my_ncols; }
567 using Matrix<Value_, Index_>::dense_row;
569 using Matrix<Value_, Index_>::dense_column;
571 using Matrix<Value_, Index_>::sparse_row;
573 using Matrix<Value_, Index_>::sparse_column;
576 Index_ secondary()
const {
588 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options&)
const {
590 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
591 my_values, my_indices, my_pointers, secondary()
594 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
595 my_values, my_indices, my_pointers, secondary()
600 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_end,
const Options&)
const {
602 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
603 my_values, my_indices, my_pointers, secondary(), block_start, block_end
606 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
607 my_values, my_indices, my_pointers, secondary(), block_start, block_end
614 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
615 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
618 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
619 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
628 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
630 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
631 my_values, my_indices, my_pointers, secondary(), opt
634 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
635 my_values, my_indices, my_pointers, secondary(), opt
640 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_end,
const Options& opt)
const {
642 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
643 my_values, my_indices, my_pointers, secondary(), block_start, block_end, opt
646 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
647 my_values, my_indices, my_pointers, secondary(), block_start, block_end, opt
654 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
655 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
658 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
659 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
668 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
669 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
672 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end,
const Options& opt)
const {
673 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_end, opt));
677 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(my_indices_ptr), opt));
684 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
685 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
688 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end,
const Options& opt)
const {
689 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_end, opt));
693 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(my_indices_ptr), opt));
702template<
typename Value_,
typename Index_,
class ValueStorage_ = std::vector<Value_>,
class IndexStorage_ = std::vector<Index_>,
class Po
interStorage_ = std::vector<
size_t> >
714 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), false, check) {}
722template<
typename Value_,
typename Index_,
class ValueStorage_ = std::vector<Value_>,
class IndexStorage_ = std::vector<Index_>,
class Po
interStorage_ = std::vector<
size_t> >
733 CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers,
bool check =
true) :
734 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), true, check) {}
Compressed sparse column matrix.
Definition CompressedSparseMatrix.hpp:703
CompressedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:713
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:477
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:684
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition CompressedSparseMatrix.hpp:628
Index_ nrow() const
Definition CompressedSparseMatrix.hpp:553
bool prefer_rows() const
Definition CompressedSparseMatrix.hpp:561
double prefer_rows_proportion() const
Definition CompressedSparseMatrix.hpp:563
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:640
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:692
bool uses_oracle(bool) const
Definition CompressedSparseMatrix.hpp:565
CompressedSparseMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool csr, bool check=true)
Definition CompressedSparseMatrix.hpp:494
double is_sparse_proportion() const
Definition CompressedSparseMatrix.hpp:559
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_end, const Options &) const
Definition CompressedSparseMatrix.hpp:600
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:668
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:688
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:672
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:676
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition CompressedSparseMatrix.hpp:612
bool is_sparse() const
Definition CompressedSparseMatrix.hpp:557
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:652
Index_ ncol() const
Definition CompressedSparseMatrix.hpp:555
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition CompressedSparseMatrix.hpp:588
Compressed sparse row matrix.
Definition CompressedSparseMatrix.hpp:723
CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:733
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:21
Flexible representations for matrix data.
Definition Extractor.hpp:15
typename std::remove_cv< typename std::remove_reference< decltype(std::declval< Array_ >()[0])>::type >::type ElementType
Definition ElementType.hpp:17
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Options for accessing data from a Matrix instance.
Definition Options.hpp:30