1#ifndef TATAMI_CONVERT_TO_DENSE_H
2#define TATAMI_CONVERT_TO_DENSE_H
16#include "sanisizer/sanisizer.hpp"
39template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
41 const InputIndex_ NR = matrix.
nrow();
42 const InputIndex_ NC = matrix.
ncol();
43 const auto primary = (row ? NR : NC);
44 const auto secondary = (row ? NC : NR);
46 constexpr bool same_type = std::is_same<InputValue_, StoredValue_>::value;
49 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
52 if constexpr(same_type) {
55 return std::vector<InputValue_>(secondary);
59 for (InputIndex_ x = 0; x < length; ++x) {
60 const auto store_copy = store + sanisizer::product_unsafe<std::size_t>(secondary, start + x);
61 if constexpr(same_type) {
62 auto ptr = wrk->fetch(store_copy);
63 copy_n(ptr, secondary, store_copy);
65 auto ptr = wrk->fetch(temp.data());
66 std::copy_n(ptr, secondary, store_copy);
72template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
73void convert_to_dense_running(
const Matrix<InputValue_, InputIndex_>& matrix,
const bool row, StoredValue_*
const store,
const ConvertToDenseOptions& options) {
74 const InputIndex_ NR = matrix.nrow();
75 const InputIndex_ NC = matrix.ncol();
76 const auto primary = (row ? NR : NC);
77 const auto secondary = (row ? NC : NR);
85 if (matrix.is_sparse()) {
88 std::fill_n(store, sanisizer::product_unsafe<std::size_t>(primary, secondary), 0);
90 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
94 for (InputIndex_ x = 0; x < length; ++x) {
95 const auto range = wrk->fetch(vtemp.data(), itemp.data());
96 for (InputIndex_ i = 0; i < range.number; ++i) {
97 store[sanisizer::nd_offset<std::size_t>(start + x, secondary, range.index[i])] = range.value[i];
100 }, secondary, options.num_threads);
103 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
108 constexpr InputIndex_ block_size = 16;
109 const InputIndex_ alloc = std::min(length, block_size);
110 std::vector<InputValue_> bigbuffer(sanisizer::product_unsafe<
typename std::vector<InputValue_>::size_type>(primary, alloc));
111 std::vector<const InputValue_*> ptrs(alloc);
113 InputIndex_ sec_i = 0;
114 while (sec_i < length) {
115 const InputIndex_ sec_to_process = std::min(
static_cast<InputIndex_
>(length - sec_i), block_size);
116 for (InputIndex_ x = 0; x < sec_to_process; ++x) {
117 ptrs[x] = wrk->fetch(bigbuffer.data() + sanisizer::product_unsafe<std::size_t>(primary, x));
120 InputIndex_ prim_i = 0;
121 while (prim_i < primary) {
122 const InputIndex_ prim_end = prim_i + std::min(
static_cast<InputIndex_
>(primary - prim_i), block_size);
123 for (InputIndex_ x = 0; x < sec_to_process; ++x) {
124 const auto input = ptrs[x];
125 for (InputIndex_ p = prim_i; p < prim_end; ++p) {
126 store[sanisizer::nd_offset<std::size_t>(start + sec_i + x, secondary, p)] = input[p];
131 sec_i += sec_to_process;
133 }, secondary, options.num_threads);
152template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
155 convert_to_dense_direct(matrix, row_major, store, options);
157 convert_to_dense_running(matrix, row_major, store, options);
178 typename StoredValue_ = Value_,
179 typename InputValue_,
183 const auto NR = matrix.
nrow();
184 const auto NC = matrix.
ncol();
185 const auto buffer_size = sanisizer::product<typename std::vector<StoredValue_>::size_type>(attest_for_Index(NR), attest_for_Index(NC));
186 std::vector<StoredValue_> buffer(buffer_size);
189 return std::shared_ptr<Matrix<Value_, Index_> >(
190 new DenseMatrix<Value_, Index_, I<
decltype(buffer)> >(
191 sanisizer::cast<Index_>(attest_for_Index(NR)),
192 sanisizer::cast<Index_>(attest_for_Index(NC)),
203template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
204void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major, StoredValue_* store,
int threads = 1) {
210 ConvertToDenseOptions options;
211 options.num_threads = threads;
217template <
typename Value_ =
double,
typename Index_ =
int,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
218inline std::shared_ptr<Matrix<Value_, Index_> >
convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major,
int threads = 1) {
219 ConvertToDenseOptions options;
220 options.num_threads = threads;
225 ConvertToDenseOptions options;
226 options.num_threads = threads;
232template<
bool row_,
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
233void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix, StoredValue_* store,
int threads = 1) {
237template<
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
238inline std::shared_ptr<Matrix<Value_, Index_> >
convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
int threads = 1) {
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
Index_ can_cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:49
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Definition parallelize.hpp:58
void convert_to_dense(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row_major, StoredValue_ *const store, const ConvertToDenseOptions &options)
Definition convert_to_dense.hpp:153
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:82
auto consecutive_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, const Index_ iter_start, const Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Parallelized iteration over a tatami::Matrix.
Options for convert_to_dense().
Definition convert_to_dense.hpp:29
int num_threads
Definition convert_to_dense.hpp:33