tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
convert_to_dense.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CONVERT_TO_DENSE_H
2#define TATAMI_CONVERT_TO_DENSE_H
3
4#include "./DenseMatrix.hpp"
5#include "./transpose.hpp"
6
10#include "../utils/copy.hpp"
12
13#include <memory>
14#include <vector>
15#include <cstddef>
16
17#include "sanisizer/sanisizer.hpp"
18
25namespace tatami {
26
36
40template <typename StoredValue_, typename InputValue_, typename InputIndex_>
41void convert_to_dense_direct(const Matrix<InputValue_, InputIndex_>& matrix, const bool row, StoredValue_* const store, const ConvertToDenseOptions& options) {
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);
46
47 constexpr bool same_type = std::is_same<InputValue_, StoredValue_>::value;
48 can_cast_Index_to_container_size<std::vector<InputValue_> >(secondary);
49
50 parallelize([&](const int, const InputIndex_ start, const InputIndex_ length) -> void {
51 auto wrk = make_Extractor<false, true>(matrix, row, make_ConsecutiveOracle(start, length), Options());
52 auto temp = [&]{
53 if constexpr(same_type) {
54 return false;
55 } else {
56 return std::vector<InputValue_>(secondary);
57 }
58 }();
59
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);
65 } else {
66 auto ptr = wrk->fetch(temp.data());
67 std::copy_n(ptr, secondary, store_copy);
68 }
69 }
70 }, primary, options.num_threads);
71}
72
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);
79
80 // We're going to completely ignore the potential for false sharing here.
81 // False sharing would only be a risk for very fat/thin matrices (depending on row= and num_threads=),
82 // and if the input matrix is sparse, this further lowers the chance of contention between threads.
83 // The alternative would be to allocate a per-thread buffer to store all of the values,
84 // but then we need to do lots of interleaved copies and at that point the cure is worse than the disease.
85
86 if (matrix.is_sparse()) {
87 // We assume that 'store' was allocated correctly, in which case the product of 'primary' and 'secondary' is known to fit inside a std::size_t.
88 // This saves us from various checks when computing related products.
89 std::fill_n(store, sanisizer::product_unsafe<std::size_t>(primary, secondary), 0);
90
91 parallelize([&](const int, const InputIndex_ start, const InputIndex_ length) -> void {
92 auto wrk = make_Extractor<true, true>(matrix, !row, make_ConsecutiveOracle(start, length), Options());
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];
99 }
100 }
101 }, secondary, options.num_threads);
102
103 } else {
104 parallelize([&](const int, const InputIndex_ start, const InputIndex_ length) -> void {
105 auto wrk = make_Extractor<false, true>(matrix, !row, make_ConsecutiveOracle(start, length), Options());
106
107 // Performing a blocked transposition to be more cache-friendly.
108 // This involves collecting several consecutive primary dimension elements so that we can transpose by blocks along the secondary dimension.
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); // no need for protection here, we know that alloc <= 16.
113
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));
119 }
120
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];
128 }
129 }
130 prim_i = prim_end;
131 }
132 sec_i += sec_to_process;
133 }
134 }, secondary, options.num_threads);
135 }
136}
153template <typename StoredValue_, typename InputValue_, typename InputIndex_>
154void convert_to_dense(const Matrix<InputValue_, InputIndex_>& matrix, const bool row_major, StoredValue_* const store, const ConvertToDenseOptions& options) {
155 if (row_major == matrix.prefer_rows()) {
156 convert_to_dense_direct(matrix, row_major, store, options);
157 } else {
158 convert_to_dense_running(matrix, row_major, store, options);
159 }
160}
161
176template <
177 typename Value_,
178 typename Index_,
179 typename StoredValue_ = Value_,
180 typename InputValue_,
181 typename InputIndex_
182>
183std::shared_ptr<Matrix<Value_, Index_> > convert_to_dense(const Matrix<InputValue_, InputIndex_>& matrix, const bool row_major, const ConvertToDenseOptions& options) {
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);
188 convert_to_dense(matrix, row_major, buffer.data(), options);
189
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)),
194 std::move(buffer),
195 row_major
196 )
197 );
198}
199
203// Backwards compatbility.
204template <typename StoredValue_, typename InputValue_, typename InputIndex_>
205void convert_to_dense(const Matrix<InputValue_, InputIndex_>* matrix, bool row_major, StoredValue_* store, int threads = 1) {
207 *matrix,
208 row_major,
209 store,
210 [&]{
211 ConvertToDenseOptions options;
212 options.num_threads = threads;
213 return options;
214 }()
215 );
216}
217
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;
223 *matrix,
224 row_major,
225 [&]{
226 ConvertToDenseOptions options;
227 options.num_threads = threads;
228 return options;
229 }()
230 );
231}
232
233template<bool row_, typename StoredValue_, typename InputValue_, typename InputIndex_>
234void convert_to_dense(const Matrix<InputValue_, InputIndex_>* matrix, StoredValue_* store, int threads = 1) {
235 convert_to_dense(matrix, row_, store, threads);
236}
237
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) {
240 return convert_to_dense<Value_, Index_, StoredValue_>(matrix, row_, threads);
241}
246}
247
248#endif
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.
Templated construction of a new extractor.
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
Transpose a dense array.