tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
OracularSlabCache.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_ORACULAR_SLAB_CACHE_HPP
2#define TATAMI_CHUNKED_ORACULAR_SLAB_CACHE_HPP
3
4#include "utils.hpp"
5
6#include <unordered_map>
7#include <vector>
8#include <list>
9#include <type_traits>
10#include <memory>
11#include <cstddef>
12
13#include "tatami/tatami.hpp"
14#include "sanisizer/sanisizer.hpp"
15
21namespace tatami_chunked {
22
40template<typename Id_, typename Index_, class Slab_, bool track_reuse_ = false>
42private:
43 std::shared_ptr<const tatami::Oracle<Index_> > my_oracle;
45 tatami::PredictionIndex my_counter = 0;
46
47 Id_ my_last_slab_id = 0;
48 Slab_* my_last_slab = NULL;
49
50 typedef std::vector<Slab_> SlabPool;
51 typename SlabPool::size_type my_max_slabs;
52 SlabPool my_all_slabs;
53
54 std::unordered_map<Id_, Slab_*> my_current_cache, my_future_cache;
55 std::vector<std::pair<Id_, Slab_*> > my_to_populate;
56 std::vector<Id_> my_in_need;
57 tatami::PredictionIndex my_refresh_point = 0;
58
59 typename std::conditional<track_reuse_, std::vector<std::pair<Id_, Slab_*> >, bool>::type my_to_reuse;
60
61public:
66 OracularSlabCache(std::shared_ptr<const tatami::Oracle<Index_> > oracle, Index_ max_slabs) :
67 my_oracle(std::move(oracle)),
68 my_total(my_oracle->total()),
69 my_max_slabs(sanisizer::cast<I<decltype(my_max_slabs)> >(max_slabs))
70 {
71 my_all_slabs.reserve(max_slabs);
72 my_current_cache.reserve(max_slabs);
73 my_future_cache.reserve(max_slabs);
74 }
75
80
85
89 // Move operators are still okay as pointers still point to the moved vectors.
90 // see https://stackoverflow.com/questions/43988553/stdvector-stdmove-and-pointer-invalidation.
93
94 // Might as well define this.
95 ~OracularSlabCache() = default;
100public:
107 Index_ next() {
108 return my_oracle->get(my_counter++);
109 }
110
111public:
146 template<class Ifunction_, class Cfunction_, class Pfunction_>
147 std::pair<const Slab_*, Index_> next(Ifunction_ identify, Cfunction_ create, Pfunction_ populate) {
148 Index_ index = this->next();
149 auto slab_info = identify(index);
150 if (slab_info.first == my_last_slab_id && my_last_slab) {
151 return std::make_pair(my_last_slab, slab_info.second);
152 }
153 my_last_slab_id = slab_info.first;
154
155 // Updating the cache if we hit the refresh point.
156 if (my_counter - 1 == my_refresh_point) {
157 // Note that, for any given populate cycle, the first prediction's
158 // slab cannot already be in the cache, otherwise it would have
159 // incorporated into the previous cycle. So we can skip some code.
160 my_future_cache[slab_info.first] = NULL;
161 my_in_need.push_back(slab_info.first);
162 I<decltype(my_max_slabs)> used_slabs = 1;
163 auto last_future_slab_id = slab_info.first;
164
165 while (++my_refresh_point < my_total) {
166 auto future_index = my_oracle->get(my_refresh_point);
167 auto future_slab_info = identify(future_index);
168 if (last_future_slab_id == future_slab_info.first) {
169 continue;
170 }
171
172 last_future_slab_id = future_slab_info.first;
173 if (my_future_cache.find(future_slab_info.first) != my_future_cache.end()) {
174 continue;
175 }
176
177 if (used_slabs == my_max_slabs) {
178 break;
179 }
180 ++used_slabs;
181
182 auto ccIt = my_current_cache.find(future_slab_info.first);
183 if (ccIt == my_current_cache.end()) {
184 my_future_cache[future_slab_info.first] = NULL;
185 my_in_need.push_back(future_slab_info.first);
186
187 } else {
188 auto slab_ptr = ccIt->second;
189 my_future_cache[future_slab_info.first] = slab_ptr;
190 my_current_cache.erase(ccIt);
191 if constexpr(track_reuse_) {
192 my_to_reuse.emplace_back(future_slab_info.first, slab_ptr);
193 }
194 }
195 }
196
197 auto cIt = my_current_cache.begin();
198 for (auto a : my_in_need) {
199 if (cIt != my_current_cache.end()) {
200 my_to_populate.emplace_back(a, cIt->second);
201 my_future_cache[a] = cIt->second;
202 ++cIt;
203 } else {
204 // We reserved my_all_slabs so further push_backs() should not
205 // trigger any reallocation or invalidation of the pointers.
206 my_all_slabs.push_back(create());
207 auto slab_ptr = &(my_all_slabs.back());
208 my_to_populate.emplace_back(a, slab_ptr);
209 my_future_cache[a] = slab_ptr;
210 }
211 }
212 my_in_need.clear();
213
214 if constexpr(track_reuse_) {
215 populate(my_to_populate, my_to_reuse);
216 } else {
217 populate(my_to_populate);
218 }
219
220 my_to_populate.clear();
221 if constexpr(track_reuse_) {
222 my_to_reuse.clear();
223 }
224
225 // We always fill my_future_cache to the brim so every entry of
226 // my_all_slabs should be referenced by a pointer in
227 // my_future_cache. There shouldn't be any free cache entries
228 // remaining in my_current_cache i.e., at this point, cIt should
229 // equal my_current_cache.end(), as we transferred everything to
230 // my_future_cache. Thus it is safe to clear my_current_cache
231 // without worrying about leaking memory. The only exception is if
232 // we run out of predictions, in which case it doesn't matter.
233 my_current_cache.clear();
234 my_current_cache.swap(my_future_cache);
235 }
236
237 // We know it must exist, so no need to check ccIt's validity.
238 auto ccIt = my_current_cache.find(slab_info.first);
239 my_last_slab = ccIt->second;
240 return std::make_pair(my_last_slab, slab_info.second);
241 }
242
243public:
248 auto get_max_slabs() const {
249 return my_max_slabs;
250 }
251
256 auto get_num_slabs() const {
257 return my_current_cache.size();
258 }
259};
260
261}
262
263#endif
Oracular-aware cache for slabs.
Definition OracularSlabCache.hpp:41
Index_ next()
Definition OracularSlabCache.hpp:107
auto get_num_slabs() const
Definition OracularSlabCache.hpp:256
OracularSlabCache & operator=(const OracularSlabCache &)=delete
OracularSlabCache(const OracularSlabCache &)=delete
OracularSlabCache(std::shared_ptr< const tatami::Oracle< Index_ > > oracle, Index_ max_slabs)
Definition OracularSlabCache.hpp:66
std::pair< const Slab_ *, Index_ > next(Ifunction_ identify, Cfunction_ create, Pfunction_ populate)
Definition OracularSlabCache.hpp:147
auto get_max_slabs() const
Definition OracularSlabCache.hpp:248
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
std::size_t PredictionIndex