tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
convert_to_fragmented_sparse.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
2#define TATAMI_CONVERT_TO_FRAGMENTED_SPARSE_H
3
4#include <memory>
5#include <vector>
6#include <cstddef>
7#include <optional>
8#include <cassert>
9
11#include "convert_to_sparse_utils.hpp"
12
14#include "../utils/copy.hpp"
17
24namespace tatami {
25
36template<typename Value_, typename Index_>
41 FragmentedSparseContents(Index_ n) :
42 value(cast_Index_to_container_size<I<decltype(value)> >(n)),
44 {}
53 std::vector<std::vector<Value_> > value;
54
60 std::vector<std::vector<Index_> > index;
61};
62
79
83template<typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
84FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_contents_consistent(
86 const bool row,
88) {
89 const InputIndex_ NR = matrix.nrow();
90 const InputIndex_ NC = matrix.ncol();
91 const InputIndex_ primary = (row ? NR : NC);
92 const InputIndex_ secondary = (row ? NC : NR);
93
95 auto& store_v = output.value;
96 auto& store_i = output.index;
97
98 if (matrix.is_sparse()) {
99 parallelize([&](const int, const InputIndex_ start, const InputIndex_ length) -> void {
100 auto wrk = make_Extractor<true, true>(matrix, row, make_ConsecutiveOracle(start, length), Options());
103
104 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
105 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
106 auto& sv = store_v[p];
107 auto& si = store_i[p];
108 // We don't filter out structural non-zeros that have values of zero, for consistency with convert_to_compressed_sparse().
109 sv.insert(sv.end(), range.value, range.value + range.number);
110 si.insert(si.end(), range.index, range.index + range.number);
111 }
112 }, primary, options.num_threads);
113
114 } else {
115 parallelize([&](const int, const InputIndex_ start, const InputIndex_ length) -> void {
116 auto wrk = make_Extractor<false, true>(matrix, row, make_ConsecutiveOracle(start, length), Options());
118
119 for (InputIndex_ p = start, pe = start + length; p < pe; ++p) {
120 const auto ptr = wrk->fetch(buffer_v.data());
121 auto& sv = store_v[p];
122 auto& si = store_i[p];
123
124 // For dense, we do treat zero values as structural zeros and remove them, otherwise the output wouldn't actually be sparse.
125 for (InputIndex_ s = 0; s < secondary; ++s) {
126 const auto val = ptr[s];
127
128 if (val) {
129 sv.push_back(val);
130 si.push_back(s);
131 }
132 }
133 }
134 }, primary, options.num_threads);
135 }
136
137 return output;
138}
139
140template<typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
141FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_inconsistent_one_pass(
143 const bool row,
144 const int num_threads
145) {
146 const InputIndex_ NR = matrix.nrow();
147 const InputIndex_ NC = matrix.ncol();
148 const InputIndex_ primary = (row ? NR : NC);
149 const InputIndex_ secondary = (row ? NC : NR);
150
151 // In the one-pass strategy, we load everything in a nice format first, then we transpose it in serial.
152 // This avoids messy reallocations when trying to expand vectors on an inconsistent dimension.
153 std::vector<std::vector<InputValue_> > store_v;
154 std::vector<std::vector<InputIndex_> > store_i;
155 auto original_ranges = extract_sparse_matrix(matrix, store_v, store_i, num_threads);
156
157 auto primary_counts = create_container_of_Index_size<std::vector<InputIndex_> >(primary);
158 for (I<decltype(secondary)> s = 0; s < secondary; ++s) {
159 const auto& sec_indices = original_ranges[s].index;
160 const auto num = original_ranges[s].number;
161 for (I<decltype(num)> n = 0; n < num; ++n) {
162 primary_counts[sec_indices[n]] += 1; // addition must be safe as this cannot exceed dimension extents.
163 }
164 }
165
166 FragmentedSparseContents<StoredValue_, StoredIndex_> output(primary);
167 for (InputIndex_ p = 0; p < primary; ++p) {
168 output.index[p].reserve(primary_counts[p]);
169 output.value[p].reserve(primary_counts[p]);
170 }
171
172 for (I<decltype(secondary)> s = 0; s < secondary; ++s) {
173 const auto& sec_values = original_ranges[s].value;
174 const auto& sec_indices = original_ranges[s].index;
175 const auto num = original_ranges[s].number;
176 for (I<decltype(num)> n = 0; n < num; ++n) {
177 const auto curp = sec_indices[n];
178 output.value[curp].push_back(sec_values[n]);
179 output.index[curp].push_back(s);
180 }
181 }
182
183 return output;
184}
185
186template<typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
187FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_inconsistent_two_pass(
189 const bool row,
190 const int num_threads
191) {
192 const InputIndex_ NR = matrix.nrow();
193 const InputIndex_ NC = matrix.ncol();
194 const InputIndex_ primary = (row ? NR : NC);
195 const InputIndex_ secondary = (row ? NC : NR);
196
197 // In the two-pass strategy, we count the number of non-zeros first, then we fill it up in the second pass.
198 auto nnz_inconsistent = create_container_of_Index_size<std::vector<InputIndex_> >(primary);
199 auto per_thread = count_sparse_non_zeros_inconsistent(matrix, primary, secondary, row, nnz_inconsistent.data(), num_threads);
200
201 FragmentedSparseContents<StoredValue_, StoredIndex_> output(primary);
204 for (InputIndex_ p = 0; p < primary; ++p) {
205 assert(nnz_inconsistent[p] <= secondary);
206 output.index[p].resize(nnz_inconsistent[p]);
207 output.value[p].resize(nnz_inconsistent[p]);
208 }
209
210 const bool is_sparse = matrix.is_sparse();
211 if (per_thread.has_value()) {
212 // Transforming the per-thread counts into per-thread starting offsets within each vector.
213 auto& offsets = per_thread->counts;
214 for (InputIndex_ i = 0; i < primary; ++i) {
215 InputIndex_ accumulant = 0;
216 static_assert(std::is_same<I<decltype(per_thread->counts[0][0])>, InputIndex_>::value); // confirm that the accumulant assignment won't overflow.
217 for (auto& pt : offsets) {
218 const auto count = pt[i];
219 pt[i] = accumulant;
220 accumulant += count;
221 }
222 }
223
224 parallelize([&](const int, const int th_start, const int th_length) -> void {
225 for (int t = 0; t < th_length; ++t) {
226 auto& offsets = (per_thread->counts)[t + th_start];
227 const auto actual_start = (per_thread->starts)[t + th_start];
228 const auto actual_length = (per_thread->lengths)[t + th_start];
229
230 // We're going to completely ignore false sharing here, see reasoning in convert_to_compressed_sparse.hpp.
231 if (is_sparse) {
232 Options opt;
233 opt.sparse_ordered_index = false;
234 auto wrk = make_Extractor<true, true>(matrix, !row, make_ConsecutiveOracle(actual_start, actual_length), opt);
237 for (InputIndex_ x = 0; x < actual_length; ++x) {
238 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
239 for (InputIndex_ i = 0; i < range.number; ++i) {
240 const auto prim = range.index[i];
241 auto& pos = offsets[prim];
242 output.value[prim][pos] = range.value[i];
243 output.index[prim][pos] = x + actual_start;
244 ++pos;
245 }
246 }
247
248 } else {
249 auto wrk = make_Extractor<false, true>(matrix, !row, make_ConsecutiveOracle(actual_start, actual_length), Options());
251 for (InputIndex_ x = 0; x < actual_length; ++x) {
252 const auto ptr = wrk->fetch(buffer_v.data());
253 for (InputIndex_ p = 0; p < primary; ++p) {
254 const auto val = ptr[p];
255 if (val != 0) {
256 auto& pos = offsets[p];
257 output.value[p][pos] = val;
258 output.index[p][pos] = x + actual_start;
259 ++pos;
260 }
261 }
262 }
263 }
264 }
265 }, per_thread->counts.size(), per_thread->counts.size());
266
267 } else {
269
270 if (is_sparse){
271 Options opt;
272 opt.sparse_ordered_index = false;
273 auto wrk = make_Extractor<true, true>(matrix, !row, make_ConsecutiveOracle<InputIndex_>(0, secondary), opt);
276 for (InputIndex_ s = 0; s < secondary; ++s) {
277 const auto range = wrk->fetch(buffer_v.data(), buffer_i.data());
278 for (InputIndex_ i = 0; i < range.number; ++i) {
279 const auto prim = range.index[i];
280 auto& pos = offsets[prim];
281 output.value[prim][pos] = range.value[i];
282 output.index[prim][pos] = s;
283 ++pos;
284 }
285 }
286
287 } else {
288 auto wrk = make_Extractor<false, true>(matrix, !row, make_ConsecutiveOracle<InputIndex_>(0, secondary), Options());
290 for (InputIndex_ s = 0; s < secondary; ++s) {
291 const auto ptr = wrk->fetch(buffer_v.data());
292 for (InputIndex_ p = 0; p < primary; ++p) {
293 const auto val = ptr[p];
294 if (val != 0) {
295 auto& pos = offsets[p];
296 output.value[p][pos] = val;
297 output.index[p][pos] = s;
298 ++pos;
299 }
300 }
301 }
302 }
303 }
304
305 return output;
306}
323template<typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
326 const bool row,
328) {
329 if (row == matrix.prefer_rows()) {
330 return retrieve_fragmented_sparse_contents_consistent<StoredValue_, StoredIndex_>(matrix, row, options);
331 }
332
333 if (!options.two_pass) {
334 return retrieve_fragmented_sparse_inconsistent_one_pass<StoredValue_, StoredIndex_>(matrix, row, options.num_threads);
335 }
336
337 return retrieve_fragmented_sparse_inconsistent_two_pass<StoredValue_, StoredIndex_>(matrix, row, options.num_threads);
338}
339
349 bool two_pass = false;
350
354 int num_threads = 1;
355};
356
372template<
373 typename Value_,
374 typename Index_,
375 typename StoredValue_ = Value_,
376 typename StoredIndex_ = Index_,
377 typename InputValue_,
378 typename InputIndex_
379>
380std::shared_ptr<Matrix<Value_, Index_> > convert_to_fragmented_sparse(
382 const bool row,
384{
386 matrix,
387 row,
388 [&]{
390 ropt.two_pass = options.two_pass;
391 ropt.num_threads = options.num_threads;
392 return ropt;
393 }()
394 );
395 return std::shared_ptr<Matrix<Value_, Index_> >(
397 Value_,
398 Index_,
399 std::vector<std::vector<StoredValue_> >,
400 std::vector<std::vector<StoredIndex_> >
401 >(
402 matrix.nrow(),
403 matrix.ncol(),
404 std::move(frag.value),
405 std::move(frag.index),
406 row,
407 []{
408 FragmentedSparseMatrixOptions fopt;
409 fopt.check = false; // no need for checks, as we guarantee correctness.
410 return fopt;
411 }()
412 )
413 );
414}
415
419// Backwards compatbility.
420template<typename Value_, typename Index_, typename StoredValue_ = Value_, typename StoredIndex_ = Index_, typename InputValue_, typename InputIndex_>
421std::shared_ptr<Matrix<Value_, Index_> > convert_to_fragmented_sparse(const Matrix<InputValue_, InputIndex_>* matrix, bool row, int threads = 1) {
423 *matrix,
424 row,
425 [&]{
426 ConvertToFragmentedSparseOptions opt;
427 opt.num_threads = threads;
428 return opt;
429 }()
430 );
431}
432
433template<typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
434FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_contents(const Matrix<InputValue_, InputIndex_>* matrix, bool row, int threads = 1) {
436 *matrix,
437 row,
438 [&]{
439 RetrieveFragmentedSparseContentsOptions opt;
440 opt.num_threads = threads;
441 return opt;
442 }()
443 );
444}
445
446template <bool row_, typename StoredValue_, typename StoredIndex_, typename InputValue_, typename InputIndex_>
447FragmentedSparseContents<StoredValue_, StoredIndex_> retrieve_fragmented_sparse_contents(const Matrix<InputValue_, InputIndex_>* matrix, int threads = 1) {
449}
450
451template <bool row_, typename Value_, typename Index_, typename StoredValue_ = Value_, typename StoredIndex_ = Index_, typename InputValue_, typename InputIndex_>
452std::shared_ptr<Matrix<Value_, Index_> > convert_to_fragmented_sparse(const Matrix<InputValue_, InputIndex_>* matrix, int threads = 1) {
454}
459}
460
461#endif
Iterate across consecutive elements of the target dimension.
Fragmented sparse matrix representation.
Convert index type to container size.
Fragmented sparse matrix representation.
Definition FragmentedSparseMatrix.hpp:627
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
auto cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:51
std::shared_ptr< ConsecutiveOracle< Index_ > > make_ConsecutiveOracle(Index_ start, Index_ length)
Definition ConsecutiveOracle.hpp:56
FragmentedSparseContents< StoredValue_, StoredIndex_ > retrieve_fragmented_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const RetrieveFragmentedSparseContentsOptions &options)
Definition convert_to_fragmented_sparse.hpp:324
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
std::shared_ptr< Matrix< Value_, Index_ > > convert_to_fragmented_sparse(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const ConvertToFragmentedSparseOptions &options)
Definition convert_to_fragmented_sparse.hpp:380
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_fragmented_sparse().
Definition convert_to_fragmented_sparse.hpp:343
bool two_pass
Definition convert_to_fragmented_sparse.hpp:349
int num_threads
Definition convert_to_fragmented_sparse.hpp:354
Fragmented sparse contents.
Definition convert_to_fragmented_sparse.hpp:37
std::vector< std::vector< Value_ > > value
Definition convert_to_fragmented_sparse.hpp:53
std::vector< std::vector< Index_ > > index
Definition convert_to_fragmented_sparse.hpp:60
Options for accessing data from a Matrix instance.
Definition Options.hpp:30
Options for retrieve_fragmented_sparse_contents().
Definition convert_to_fragmented_sparse.hpp:66
int num_threads
Definition convert_to_fragmented_sparse.hpp:77
bool two_pass
Definition convert_to_fragmented_sparse.hpp:72