tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
SparseSlabFactory.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_SPARSE_SLAB_FACTORY_HPP
2#define TATAMI_CHUNKED_SPARSE_SLAB_FACTORY_HPP
3
4#include "SlabCacheStats.hpp"
5#include "utils.hpp"
6
7#include <vector>
8#include <cstddef>
9
10#include "sanisizer/sanisizer.hpp"
11
17namespace tatami_chunked {
18
33template<typename Value_, typename Index_, typename Count_ = Index_>
35private:
36 Index_ my_target_dim, my_non_target_dim;
37 bool my_needs_value, my_needs_index;
38
39 // Might as well use size_t here, as we'll be doing pointer arithmetic in create().
40 std::size_t my_slab_size;
41 std::size_t my_offset_number = 0;
42 std::size_t my_offset_slab = 0;
43
44 std::vector<Value_> my_value_pool;
45 std::vector<Index_> my_index_pool;
46 std::vector<Count_> my_number_pool;
47
48public:
61 template<typename MaxSlabs_>
62 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, MaxSlabs_ max_slabs, bool needs_value, bool needs_index) :
63 my_target_dim(target_dim),
64 my_non_target_dim(non_target_dim),
65 my_needs_value(needs_value),
66 my_needs_index(needs_index),
67 my_slab_size(slab_size),
68 my_number_pool(sanisizer::product<I<decltype(my_number_pool.size())> >(max_slabs, target_dim))
69 {
70 if (needs_value) {
71 my_value_pool.resize(sanisizer::product<I<decltype(my_value_pool.size())> >(max_slabs, slab_size));
72 }
73 if (needs_index) {
74 my_index_pool.resize(sanisizer::product<I<decltype(my_index_pool.size())> >(max_slabs, slab_size));
75 }
76 }
77
89 template<typename MaxSlabs_>
90 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, MaxSlabs_ max_slabs, bool needs_value, bool needs_index) :
91 SparseSlabFactory(target_dim, non_target_dim, sanisizer::product<std::size_t>(target_dim, non_target_dim), max_slabs, needs_value, needs_index) {}
92
104 template<typename MaxSlabs_>
105 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats<MaxSlabs_>& stats, bool needs_value, bool needs_index) :
106 SparseSlabFactory(target_dim, non_target_dim, stats.slab_size_in_elements, stats.max_slabs_in_cache, needs_value, needs_index) {}
107
111 // Delete the copy constructors as we're passing out pointers.
112 SparseSlabFactory(const SparseSlabFactory&) = delete;
113 SparseSlabFactory& operator=(const SparseSlabFactory&) = delete;
114
115 // Move constructors are okay though.
117 SparseSlabFactory& operator=(SparseSlabFactory&&) = default;
122public:
126 struct Slab {
135 std::vector<Value_*> values;
136
145 std::vector<Index_*> indices;
146
153 Count_* number = NULL;
154 };
155
163 Slab output;
164 output.number = my_number_pool.data() + my_offset_number;
165 my_offset_number += my_target_dim;
166
167 if (my_needs_value) {
168 output.values.reserve(my_target_dim);
169 auto vptr = my_value_pool.data() + my_offset_slab;
170 for (I<decltype(my_target_dim)> p = 0; p < my_target_dim; ++p, vptr += my_non_target_dim) {
171 output.values.push_back(vptr);
172 }
173 }
174
175 if (my_needs_index) {
176 output.indices.reserve(my_target_dim);
177 auto iptr = my_index_pool.data() + my_offset_slab;
178 for (I<decltype(my_target_dim)> p = 0; p < my_target_dim; ++p, iptr += my_non_target_dim) {
179 output.indices.push_back(iptr);
180 }
181 }
182
183 my_offset_slab += my_slab_size;
184 return output;
185 }
186};
187
188}
189
190#endif
Slab cache statistics.
Factory for sparse slabs.
Definition SparseSlabFactory.hpp:34
Slab create()
Definition SparseSlabFactory.hpp:162
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats< MaxSlabs_ > &stats, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:105
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, MaxSlabs_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:90
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, MaxSlabs_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:62
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
Statistics for slab caching.
Definition SlabCacheStats.hpp:26
Sparse slab.
Definition SparseSlabFactory.hpp:126
std::vector< Index_ * > indices
Definition SparseSlabFactory.hpp:145
std::vector< Value_ * > values
Definition SparseSlabFactory.hpp:135
Count_ * number
Definition SparseSlabFactory.hpp:153