1#ifndef TATAMI_CONVERT_TO_DENSE_H
2#define TATAMI_CONVERT_TO_DENSE_H
17#include "sanisizer/sanisizer.hpp"
40template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
42 const InputIndex_ NR = matrix.
nrow();
43 const InputIndex_ NC = matrix.
ncol();
44 const auto primary = (row ? NR : NC);
45 const auto secondary = (row ? NC : NR);
47 constexpr bool same_type = std::is_same<InputValue_, StoredValue_>::value;
48 can_cast_Index_to_container_size<std::vector<InputValue_> >(secondary);
50 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
53 if constexpr(same_type) {
56 return std::vector<InputValue_>(secondary);
60 for (InputIndex_ x = 0; x < length; ++x) {
61 const auto store_copy = store + sanisizer::product_unsafe<std::size_t>(secondary, start + x);
62 if constexpr(same_type) {
63 auto ptr = wrk->fetch(store_copy);
64 copy_n(ptr, secondary, store_copy);
66 auto ptr = wrk->fetch(temp.data());
67 std::copy_n(ptr, secondary, store_copy);
73template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
74void convert_to_dense_running(
const Matrix<InputValue_, InputIndex_>& matrix,
const bool row, StoredValue_*
const store,
const ConvertToDenseOptions& options) {
75 const InputIndex_ NR = matrix.nrow();
76 const InputIndex_ NC = matrix.ncol();
77 const auto primary = (row ? NR : NC);
78 const auto secondary = (row ? NC : NR);
86 if (matrix.is_sparse()) {
89 std::fill_n(store, sanisizer::product_unsafe<std::size_t>(primary, secondary), 0);
91 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
95 for (InputIndex_ x = 0; x < length; ++x) {
96 const auto range = wrk->fetch(vtemp.data(), itemp.data());
97 for (InputIndex_ i = 0; i < range.number; ++i) {
98 store[sanisizer::nd_offset<std::size_t>(start + x, secondary, range.index[i])] = range.value[i];
101 }, secondary, options.num_threads);
104 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
109 constexpr InputIndex_ block_size = 16;
110 const InputIndex_ alloc = std::min(length, block_size);
111 std::vector<InputValue_> bigbuffer(sanisizer::product_unsafe<
typename std::vector<InputValue_>::size_type>(primary, alloc));
112 std::vector<const InputValue_*> ptrs(alloc);
114 InputIndex_ sec_i = 0;
115 while (sec_i < length) {
116 const InputIndex_ sec_to_process = std::min(
static_cast<InputIndex_
>(length - sec_i), block_size);
117 for (InputIndex_ x = 0; x < sec_to_process; ++x) {
118 ptrs[x] = wrk->fetch(bigbuffer.data() + sanisizer::product_unsafe<std::size_t>(primary, x));
121 InputIndex_ prim_i = 0;
122 while (prim_i < primary) {
123 const InputIndex_ prim_end = prim_i + std::min(
static_cast<InputIndex_
>(primary - prim_i), block_size);
124 for (InputIndex_ x = 0; x < sec_to_process; ++x) {
125 const auto input = ptrs[x];
126 for (InputIndex_ p = prim_i; p < prim_end; ++p) {
127 store[sanisizer::nd_offset<std::size_t>(start + sec_i + x, secondary, p)] = input[p];
132 sec_i += sec_to_process;
134 }, secondary, options.num_threads);
153template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
156 convert_to_dense_direct(matrix, row_major, store, options);
158 convert_to_dense_running(matrix, row_major, store, options);
179 typename StoredValue_ = Value_,
180 typename InputValue_,
184 const auto NR = matrix.
nrow();
185 const auto NC = matrix.
ncol();
186 const auto buffer_size = sanisizer::product<typename std::vector<StoredValue_>::size_type>(attest_for_Index(NR), attest_for_Index(NC));
187 std::vector<StoredValue_> buffer(buffer_size);
190 return std::shared_ptr<Matrix<Value_, Index_> >(
191 new DenseMatrix<Value_, Index_, I<
decltype(buffer)> >(
192 sanisizer::cast<Index_>(attest_for_Index(NR)),
193 sanisizer::cast<Index_>(attest_for_Index(NC)),
204template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
205void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major, StoredValue_* store,
int threads = 1) {
211 ConvertToDenseOptions options;
212 options.num_threads = threads;
218template <
typename Value_ =
double,
typename Index_ =
int,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
219inline std::shared_ptr<Matrix<Value_, Index_> >
convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major,
int threads = 1) {
220 ConvertToDenseOptions options;
221 options.num_threads = threads;
226 ConvertToDenseOptions options;
227 options.num_threads = threads;
233template<
bool row_,
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
234void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix, StoredValue_* store,
int threads = 1) {
238template<
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
239inline std::shared_ptr<Matrix<Value_, Index_> >
convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
int threads = 1) {
Iterate across consecutive elements of the target dimension.
Dense matrix representation.
Convert index type to container size.
Dense matrix representation.
Definition DenseMatrix.hpp:180
Virtual class for a matrix.
Definition Matrix.hpp:59
virtual Index_ ncol() const =0
virtual Index_ nrow() const =0
virtual bool prefer_rows() const =0
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< ConsecutiveOracle< Index_ > > make_ConsecutiveOracle(Index_ start, Index_ length)
Definition ConsecutiveOracle.hpp:56
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Definition parallelize.hpp:58
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
void convert_to_dense(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row_major, StoredValue_ *const store, const ConvertToDenseOptions &options)
Definition convert_to_dense.hpp:154
Value_ * copy_n(const Value_ *const input, const Size_ n, Value_ *const output)
Definition copy.hpp:37
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:68
Parallelized iteration over a tatami::Matrix.
Options for convert_to_dense().
Definition convert_to_dense.hpp:30
int num_threads
Definition convert_to_dense.hpp:34
Options for accessing data from a Matrix instance.
Definition Options.hpp:30