1#ifndef TATAMI_CONVERT_TO_DENSE_H
2#define TATAMI_CONVERT_TO_DENSE_H
15#include "sanisizer/sanisizer.hpp"
46template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
48 const InputIndex_ NR = matrix.
nrow();
49 const InputIndex_ NC = matrix.
ncol();
51 const auto primary = (pref_rows ? NR : NC);
52 const auto secondary = (pref_rows ? NC : NR);
57 if (row_major == pref_rows) {
58 constexpr bool same_type = std::is_same<InputValue_, StoredValue_>::value;
59 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
62 if constexpr(same_type) {
69 for (InputIndex_ x = 0; x < length; ++x) {
70 const auto store_copy = store + sanisizer::product_unsafe<std::size_t>(secondary, start + x);
71 if constexpr(same_type) {
72 auto ptr = wrk->fetch(store_copy);
73 copy_n(ptr, secondary, store_copy);
75 auto ptr = wrk->fetch(temp.data());
76 std::copy_n(ptr, secondary, store_copy);
82 std::fill_n(store, sanisizer::product_unsafe<std::size_t>(primary, secondary), 0);
89 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
97 for (InputIndex_ x = 0; x < primary; ++x) {
98 const auto range = wrk->fetch(vtemp.data(), itemp.data());
99 for (InputIndex_ i = 0; i < range.number; ++i) {
100 store[sanisizer::nd_offset<std::size_t>(x, primary, range.index[i])] = range.value[i];
109 parallelize([&](
const int,
const InputIndex_ start,
const InputIndex_ length) ->
void {
116 constexpr InputIndex_ block_size = 16;
117 const InputIndex_ alloc = std::min(primary, block_size);
118 std::vector<InputValue_> bigbuffer(sanisizer::product_unsafe<
typename std::vector<InputValue_>::size_type>(length, alloc));
119 std::vector<const InputValue_*> ptrs(alloc);
120 std::vector<InputValue_*> buf_ptrs(alloc);
121 for (InputIndex_ i = 0; i < alloc; ++i) {
122 buf_ptrs[i] = bigbuffer.data() + sanisizer::product_unsafe<std::size_t>(length, i);
125 InputIndex_ prim_i = 0;
126 while (prim_i < primary) {
127 const InputIndex_ prim_to_process = std::min(
static_cast<InputIndex_
>(primary - prim_i), block_size);
128 for (InputIndex_ c = 0; c < prim_to_process; ++c) {
129 ptrs[c] = wrk->fetch(buf_ptrs[c]);
132 InputIndex_ sec_i = 0;
133 while (sec_i < length) {
134 const InputIndex_ sec_end = sec_i + std::min(
static_cast<InputIndex_
>(length - sec_i), block_size);
135 for (InputIndex_ c = 0; c < prim_to_process; ++c) {
136 const auto input = ptrs[c];
137 for (InputIndex_ r = sec_i; r < sec_end; ++r) {
138 store[sanisizer::nd_offset<std::size_t>(c + prim_i, primary, r + start)] = input[r];
144 prim_i += prim_to_process;
169 typename StoredValue_ = Value_,
170 typename InputValue_,
174 const auto NR = matrix.
nrow();
175 const auto NC = matrix.
ncol();
176 const auto buffer_size = sanisizer::product<std::size_t>(NR, NC);
177 auto buffer = sanisizer::create<std::vector<StoredValue_> >(buffer_size);
179 return std::shared_ptr<Matrix<Value_, Index_> >(
new DenseMatrix<Value_, Index_, I<
decltype(buffer)> >(NR, NC, std::move(buffer), row_major));
186template <
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
187void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major, StoredValue_* store,
int threads = 1) {
193 ConvertToDenseOptions options;
194 options.num_threads = threads;
200template <
typename Value_ =
double,
typename Index_ =
int,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
201inline std::shared_ptr<Matrix<Value_, Index_> >
convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix,
bool row_major,
int threads = 1) {
202 ConvertToDenseOptions options;
203 options.num_threads = threads;
208 ConvertToDenseOptions options;
209 options.num_threads = threads;
215template<
bool row_,
typename StoredValue_,
typename InputValue_,
typename InputIndex_>
216void convert_to_dense(
const Matrix<InputValue_, InputIndex_>* matrix, StoredValue_* store,
int threads = 1) {
220template<
bool row_,
typename Value_,
typename Index_,
typename StoredValue_ = Value_,
typename InputValue_,
typename InputIndex_>
221inline 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:172
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
virtual bool is_sparse() const =0
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
void parallelize(Function_ fun, const Index_ tasks, const int threads)
Definition parallelize.hpp:42
void convert_to_dense(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row_major, StoredValue_ *const store, const ConvertToDenseOptions &options)
Definition convert_to_dense.hpp:47
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:78
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:28
int num_threads
Definition convert_to_dense.hpp:32