tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
OracularVariableSlabCache.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_ORACULAR_VARIABLE_SLAB_CACHE_HPP
2#define TATAMI_CHUNKED_ORACULAR_VARIABLE_SLAB_CACHE_HPP
3
4#include <unordered_map>
5#include <vector>
6#include <type_traits>
7#include <optional>
8#include <cstddef>
9
10#include "tatami/tatami.hpp"
11
17namespace tatami_chunked {
18
44template<typename Id_, typename Index_, class Slab_, typename Size_>
46private:
47 std::shared_ptr<const tatami::Oracle<Index_> > my_oracle;
49 tatami::PredictionIndex my_counter = 0;
50
51 typedef std::vector<Slab_> SlabPool;
52 typedef typename SlabPool::size_type SlabIndex;
53
54 Index_ my_last_slab_id = 0;
55 std::optional<SlabIndex> my_last_slab_num;
56
57 Size_ my_max_size, my_used_size = 0;
58 SlabPool my_all_slabs;
59
60 // We need to hold an offset into 'my_all_slabs' rather than a pointer, as
61 // 'my_all_slabs' might be reallocated upon addition of new slabs, given that
62 // we don't know the maximum number of slabs ahead of time.
63 std::unordered_map<Id_, SlabIndex> my_current_cache, my_future_cache;
64 std::vector<std::pair<Id_, SlabIndex> > my_to_populate, my_to_reuse;
65 std::vector<Id_> my_in_need;
66 std::vector<SlabIndex> my_free_pool;
67 tatami::PredictionIndex my_refresh_point = 0;
68
69public:
75 OracularVariableSlabCache(std::shared_ptr<const tatami::Oracle<Index_> > oracle, Size_ max_size) :
76 my_oracle(std::move(oracle)),
77 my_total(my_oracle->total()),
78 my_max_size(max_size)
79 {}
80
85
90
94 // Move operators are still okay as pointers still point to the moved vectors.
95 // see https://stackoverflow.com/questions/43988553/stdvector-stdmove-and-pointer-invalidation.
98
99 // Might as well define this.
100 ~OracularVariableSlabCache() = default;
105public:
112 Index_ next() {
113 return my_oracle->get(my_counter++);
114 }
115
116public:
161 template<class Ifunction_, class Ufunction_, class Afunction_, class Cfunction_, class Pfunction_>
162 std::pair<const Slab_*, Index_> next(Ifunction_ identify, Ufunction_ upper_size, Afunction_ actual_size, Cfunction_ create, Pfunction_ populate) {
163 Index_ index = this->next();
164 auto slab_info = identify(index);
165 if (slab_info.first == my_last_slab_id && my_last_slab_num.has_value()) {
166 return std::make_pair(my_all_slabs.data() + *my_last_slab_num, slab_info.second);
167 }
168 my_last_slab_id = slab_info.first;
169
170 // Updating the cache if we hit the refresh point.
171 if (my_counter - 1 == my_refresh_point) {
172 // Note that, for any given populate cycle, the first prediction's
173 // slab cannot already be in the cache, otherwise it would have
174 // incorporated into the previous cycle. So we can skip some code.
175 my_used_size = upper_size(slab_info.first);
176 requisition_new_slab(slab_info.first);
177
178 auto last_future_slab_id = slab_info.first;
179 while (++my_refresh_point < my_total) {
180 auto future_index = my_oracle->get(my_refresh_point);
181 auto future_slab_info = identify(future_index);
182 if (last_future_slab_id == future_slab_info.first) {
183 continue;
184 }
185
186 last_future_slab_id = future_slab_info.first;
187 if (my_future_cache.find(future_slab_info.first) != my_future_cache.end()) {
188 continue;
189 }
190
191 auto ccIt = my_current_cache.find(future_slab_info.first);
192 if (ccIt != my_current_cache.end()) {
193 auto slab_num = ccIt->second;
194 auto candidate = my_used_size + actual_size(future_slab_info.first, my_all_slabs[slab_num]);
195 if (candidate > my_max_size) {
196 break;
197 }
198 my_used_size = candidate;
199 my_future_cache[future_slab_info.first] = slab_num;
200 my_to_reuse.emplace_back(future_slab_info.first, slab_num);
201 my_current_cache.erase(ccIt);
202 } else {
203 auto candidate = my_used_size + upper_size(future_slab_info.first);
204 if (candidate > my_max_size) {
205 break;
206 }
207 my_used_size = candidate;
208 requisition_new_slab(future_slab_info.first);
209 }
210 }
211
212 auto cIt = my_current_cache.begin();
213 for (auto a : my_in_need) {
214 if (cIt != my_current_cache.end()) {
215 auto slab_num = cIt->second;
216 my_to_populate.emplace_back(a, slab_num);
217 my_future_cache[a] = slab_num;
218 ++cIt;
219 } else {
220 auto slab_num = my_all_slabs.size();
221 my_all_slabs.push_back(create());
222 my_to_populate.emplace_back(a, slab_num);
223 my_future_cache[a] = slab_num;
224 }
225 }
226 my_in_need.clear();
227
228 for (; cIt != my_current_cache.end(); ++cIt) {
229 my_free_pool.emplace_back(cIt->second);
230 }
231
232 populate(my_to_populate, my_to_reuse, my_all_slabs);
233 my_to_populate.clear();
234 my_to_reuse.clear();
235
236 my_current_cache.clear();
237 my_current_cache.swap(my_future_cache);
238 }
239
240 // We know it must exist, so no need to check ccIt's validity.
241 auto ccIt = my_current_cache.find(slab_info.first);
242 my_last_slab_num = ccIt->second;
243 return std::make_pair(my_all_slabs.data() + *my_last_slab_num, slab_info.second);
244 }
245
246private:
247 void requisition_new_slab(Id_ slab_id) {
248 if (!my_free_pool.empty()) {
249 auto slab_num = my_free_pool.back();
250 my_future_cache[slab_id] = slab_num;
251 my_free_pool.pop_back();
252 my_to_populate.emplace_back(slab_id, slab_num);
253 } else {
254 my_future_cache[slab_id] = 0;
255 my_in_need.push_back(slab_id);
256 }
257 }
258
259public:
264 Size_ get_max_size() const {
265 return my_max_size;
266 }
267
272 Size_ get_used_size() const {
273 return my_used_size;
274 }
275
280 auto get_num_slabs() const {
281 return my_current_cache.size();
282 }
283};
284
285}
286
287#endif
Oracle-aware cache for variable-size slabs.
Definition OracularVariableSlabCache.hpp:45
auto get_num_slabs() const
Definition OracularVariableSlabCache.hpp:280
std::pair< const Slab_ *, Index_ > next(Ifunction_ identify, Ufunction_ upper_size, Afunction_ actual_size, Cfunction_ create, Pfunction_ populate)
Definition OracularVariableSlabCache.hpp:162
OracularVariableSlabCache(const OracularVariableSlabCache &)=delete
OracularVariableSlabCache & operator=(const OracularVariableSlabCache &)=delete
Index_ next()
Definition OracularVariableSlabCache.hpp:112
Size_ get_used_size() const
Definition OracularVariableSlabCache.hpp:272
OracularVariableSlabCache(std::shared_ptr< const tatami::Oracle< Index_ > > oracle, Size_ max_size)
Definition OracularVariableSlabCache.hpp:75
Size_ get_max_size() const
Definition OracularVariableSlabCache.hpp:264
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
std::size_t PredictionIndex