tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
OracularSubsettedSlabCache.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_SUBSETTED_ORACLE_SLAB_CACHE_HPP
2#define TATAMI_CHUNKED_SUBSETTED_ORACLE_SLAB_CACHE_HPP
3
4#include "utils.hpp"
5
6#include <unordered_map>
7#include <vector>
8#include <list>
9#include <cstddef>
10
11#include "tatami/tatami.hpp"
12#include "sanisizer/sanisizer.hpp"
13
19namespace tatami_chunked {
20
29enum class OracularSubsettedSlabCacheSelectionType : char { FULL, BLOCK, INDEX };
30
35template<typename Index_>
41
50
56
62 Index_ block_end;
63
72 std::vector<Index_> indices;
73
79 std::unordered_map<Index_, Index_> mapping;
80};
81
85namespace OracularSubsettedSlabCache_internals {
86
87// We put these functions in here as struct{} usage policy forbids methods
88// (outside of the constructor). The Details class should be a passive data
89// carrier only.
90
91template<typename Index_>
92void fill_mapping_in_details(OracularSubsettedSlabCacheSelectionDetails<Index_>& details) {
93 auto num = details.indices.size();
94 for (I<decltype(num)> i = 0; i < num; ++i) {
95 details.mapping[details.indices[i]] = i;
96 }
97}
98
99template<typename Index_>
100void set_details(OracularSubsettedSlabCacheSelectionDetails<Index_>& details, Index_ i) {
101 details.selection = OracularSubsettedSlabCacheSelectionType::BLOCK;
102 details.block_start = i;
103 details.block_end = i + 1;
104 details.indices.clear();
105 details.mapping.clear();
106}
107
108template<typename Index_>
109void add_to_details(OracularSubsettedSlabCacheSelectionDetails<Index_>& details, Index_ i) {
110 if (details.selection == OracularSubsettedSlabCacheSelectionType::FULL) {
111 return;
112 }
113
114 if (details.selection == OracularSubsettedSlabCacheSelectionType::BLOCK) {
115 if (i == details.block_end) {
116 details.block_end = i + 1;
117 return;
118
119 } else if (i + 1 == details.block_start) {
120 details.block_start = i;
121 return;
122
123 } else if (i >= details.block_start && i < details.block_end) {
124 return;
125 }
126
127 details.selection = OracularSubsettedSlabCacheSelectionType::INDEX;
128
129 // Don't replace this with tatami::resize_container_to_Index_size as this class might be used outside of the tatami::Matrix contract (i.e., Index_ might store values beyond std::size_t).
130 details.indices.resize(sanisizer::cast<I<decltype(details.indices.size())> >(details.block_end - details.block_start));
131 std::iota(details.indices.begin(), details.indices.end(), details.block_start);
132 fill_mapping_in_details(details);
133 }
134
135 if (details.mapping.find(i) == details.mapping.end()) {
136 details.mapping[i] = details.indices.size();
137 details.indices.push_back(i);
138 }
139}
140
141template<typename Index_>
142void finalize_details(OracularSubsettedSlabCacheSelectionDetails<Index_>& details) {
143 if (details.selection == OracularSubsettedSlabCacheSelectionType::BLOCK) {
144 details.block_length = details.block_end - details.block_start;
145 } else if (details.selection == OracularSubsettedSlabCacheSelectionType::INDEX) {
146 if (!std::is_sorted(details.indices.begin(), details.indices.end())) {
147 std::sort(details.indices.begin(), details.indices.end());
148 fill_mapping_in_details(details);
149 }
150 }
151}
152
153}
171template<typename Id_, typename Index_, class Slab_>
173private:
174 std::shared_ptr<const tatami::Oracle<Index_> > my_oracle;
176 tatami::PredictionIndex my_counter = 0;
177
178 Index_ my_last_slab_id = 0;
179 Slab_* my_last_slab = NULL;
180
181 typedef std::vector<Slab_> SlabPool;
182 typename SlabPool::size_type my_max_slabs;
183 SlabPool my_all_slabs;
184 std::unordered_map<Id_, Slab_*> my_current_cache, my_future_cache;
185
186 std::vector<OracularSubsettedSlabCacheSelectionDetails<Index_> > my_all_subset_details;
187 std::vector<OracularSubsettedSlabCacheSelectionDetails<Index_>*> my_free_subset_details;
188 std::unordered_map<Id_, OracularSubsettedSlabCacheSelectionDetails<Index_>*> my_close_future_subset_cache, my_far_future_subset_cache;
189
190 tatami::PredictionIndex my_close_refresh_point = 0;
191 tatami::PredictionIndex my_far_refresh_point = 0;
192 Id_ my_far_slab_id;
193 Index_ my_far_slab_offset;
194
195 std::vector<std::pair<Id_, OracularSubsettedSlabCacheSelectionDetails<Index_>*> > my_to_reassign;
196 std::vector<std::tuple<Id_, Slab_*, const OracularSubsettedSlabCacheSelectionDetails<Index_>*> > my_to_populate;
197
198public:
203 OracularSubsettedSlabCache(std::shared_ptr<const tatami::Oracle<Index_> > oracle, Index_ max_slabs) :
204 my_oracle(std::move(oracle)),
205 my_total(my_oracle->total()),
206 my_max_slabs(sanisizer::cast<I<decltype(my_max_slabs)> >(max_slabs))
207 {
208 my_all_slabs.reserve(max_slabs);
209 my_current_cache.reserve(max_slabs);
210 my_future_cache.reserve(max_slabs);
211 my_close_future_subset_cache.reserve(max_slabs);
212 my_far_future_subset_cache.reserve(max_slabs);
213
214 my_all_subset_details.resize(sanisizer::product<I<decltype(my_all_subset_details.size())> >(2, max_slabs));
215 for (auto& as : my_all_subset_details) {
216 my_free_subset_details.push_back(&as);
217 }
218 }
219
224
229
233 // Move operators are still okay as pointers still point to the moved vectors,
234 // see https://stackoverflow.com/questions/43988553/stdvector-stdmove-and-pointer-invalidation.
237
238 // Might as well define this.
239 ~OracularSubsettedSlabCache() = default;
244public:
251 Index_ next() {
252 return my_oracle->get(my_counter++);
253 }
254
255public:
282 template<class Ifunction_, class Cfunction_, class Pfunction_>
283 std::pair<const Slab_*, Index_> next(Ifunction_ identify, Cfunction_ create, Pfunction_ populate) {
284 Index_ index = this->next();
285 auto slab_info = identify(index);
286 if (slab_info.first == my_last_slab_id && my_last_slab) {
287 return std::make_pair(my_last_slab, slab_info.second);
288 }
289 my_last_slab_id = slab_info.first;
290
291 // Updating the cache if we hit the refresh point.
292 if (my_counter - 1 == my_close_refresh_point) {
293 if (my_all_slabs.empty()) {
294 // This section only runs once, at the start, to populate the my_close_future_subset_cache.
295 requisition_subset_close(slab_info.first, slab_info.second);
296 I<decltype(my_max_slabs)> used_slabs = 1;
297
298 while (++my_close_refresh_point < my_total) {
299 auto future_index = my_oracle->get(my_close_refresh_point);
300 auto future_slab_info = identify(future_index);
301 auto cfcIt = my_close_future_subset_cache.find(future_slab_info.first);
302 if (cfcIt != my_close_future_subset_cache.end()) {
303 OracularSubsettedSlabCache_internals::add_to_details(*(cfcIt->second), future_slab_info.second);
304 } else if (used_slabs < my_max_slabs) {
305 requisition_subset_close(future_slab_info.first, future_slab_info.second);
306 ++used_slabs;
307 } else {
308 my_far_slab_id = future_slab_info.first;
309 my_far_slab_offset = future_slab_info.second;
310 break;
311 }
312 }
313
314 my_far_refresh_point = my_close_refresh_point;
315 } else {
316 my_close_refresh_point = my_far_refresh_point;
317 }
318
319 // Populating the far future cache.
320 if (my_far_refresh_point < my_total) {
321 requisition_subset_far(my_far_slab_id, my_far_slab_offset);
322 I<decltype(my_max_slabs)> used_slabs = 1;
323
324 while (++my_far_refresh_point < my_total) {
325 auto future_index = my_oracle->get(my_far_refresh_point);
326 auto future_slab_info = identify(future_index);
327 auto ffcIt = my_far_future_subset_cache.find(future_slab_info.first);
328 if (ffcIt != my_far_future_subset_cache.end()) {
329 OracularSubsettedSlabCache_internals::add_to_details(*(ffcIt->second), future_slab_info.second);
330 } else if (used_slabs < my_max_slabs) {
331 requisition_subset_far(future_slab_info.first, future_slab_info.second);
332 ++used_slabs;
333 } else {
334 my_far_slab_id = future_slab_info.first;
335 my_far_slab_offset = future_slab_info.second;
336 break;
337 }
338 }
339 }
340
341 // Reusing slabs from my_current_cache; these should all have FULL selections already.
342 for (auto& cf : my_close_future_subset_cache) {
343 auto cIt = my_current_cache.find(cf.first);
344 if (cIt == my_current_cache.end()) {
345 my_to_reassign.emplace_back(cf.first, cf.second);
346 } else {
347 my_future_cache[cf.first] = cIt->second;
348 my_current_cache.erase(cIt);
349 }
350 }
351
352 // Creating new slabs for everything that's left.
353 auto cIt = my_current_cache.begin();
354 for (auto a : my_to_reassign) {
355 Slab_* slab_ptr;
356 if (cIt == my_current_cache.end()) {
357 my_all_slabs.emplace_back(create());
358 slab_ptr = &(my_all_slabs.back());
359 } else {
360 slab_ptr = cIt->second;
361 ++cIt;
362 }
363 my_future_cache[a.first] = slab_ptr;
364 OracularSubsettedSlabCache_internals::finalize_details(*(a.second));
365 my_to_populate.emplace_back(a.first, slab_ptr, a.second);
366 }
367 my_to_reassign.clear();
368
369 populate(my_to_populate);
370 my_to_populate.clear();
371
372 // We always fill my_future_cache to the brim so every entry of
373 // my_all_slabs should be referenced by a pointer in
374 // my_future_cache. There shouldn't be any free cache entries
375 // remaining in my_current_cache i.e., at this point, cIt should
376 // equal my_current_cache.end(), as we transferred everything to
377 // my_future_cache. Thus it is safe to clear my_current_cache
378 // without worrying about leaking memory. The only exception is if
379 // we run out of predictions, in which case it doesn't matter.
380 my_current_cache.clear();
381 my_current_cache.swap(my_future_cache);
382
383 // Putting the no-longer-used subset pointers back in the free pool
384 // before we swap the close and far futures.
385 for (auto& cfc : my_close_future_subset_cache) {
386 my_free_subset_details.push_back(cfc.second);
387 }
388 my_close_future_subset_cache.clear();
389 my_close_future_subset_cache.swap(my_far_future_subset_cache);
390 }
391
392 // We know it must exist, so no need to check ccIt's validity.
393 auto ccIt = my_current_cache.find(slab_info.first);
394 my_last_slab = ccIt->second;
395 return std::make_pair(my_last_slab, slab_info.second);
396 }
397
398private:
399 void requisition_subset_close(Id_ slab_id, Index_ slab_offset) {
400 auto selected = my_free_subset_details.back();
401 OracularSubsettedSlabCache_internals::set_details(*selected, slab_offset);
402 my_close_future_subset_cache[slab_id] = selected;
403 my_free_subset_details.pop_back();
404 }
405
406 void requisition_subset_far(Id_ slab_id, Index_ slab_offset) {
407 auto selected = my_free_subset_details.back();
408 OracularSubsettedSlabCache_internals::set_details(*selected, slab_offset);
409 my_far_future_subset_cache[slab_id] = selected;
410 my_free_subset_details.pop_back();
411
412 // If a slab is still being used in the far future, it might continue
413 // to be used in an even further future, in which case we need to do a
414 // FULL extraction just to be safe.
415 auto cfcIt = my_close_future_subset_cache.find(slab_id);
416 if (cfcIt != my_close_future_subset_cache.end()) {
417 selected->selection = OracularSubsettedSlabCacheSelectionType::FULL;
418 cfcIt->second->selection = OracularSubsettedSlabCacheSelectionType::FULL;
419 }
420 }
421
422public:
427 auto get_max_slabs() const {
428 return my_max_slabs;
429 }
430
435 auto get_num_slabs() const {
436 return my_current_cache.size();
437 }
438};
439
440}
441
442#endif
Oracle-aware cache for slabs, plus subsets.
Definition OracularSubsettedSlabCache.hpp:172
auto get_max_slabs() const
Definition OracularSubsettedSlabCache.hpp:427
auto get_num_slabs() const
Definition OracularSubsettedSlabCache.hpp:435
OracularSubsettedSlabCache & operator=(const OracularSubsettedSlabCache &)=delete
std::pair< const Slab_ *, Index_ > next(Ifunction_ identify, Cfunction_ create, Pfunction_ populate)
Definition OracularSubsettedSlabCache.hpp:283
OracularSubsettedSlabCache(const OracularSubsettedSlabCache &)=delete
Index_ next()
Definition OracularSubsettedSlabCache.hpp:251
OracularSubsettedSlabCache(std::shared_ptr< const tatami::Oracle< Index_ > > oracle, Index_ max_slabs)
Definition OracularSubsettedSlabCache.hpp:203
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
OracularSubsettedSlabCacheSelectionType
Definition OracularSubsettedSlabCache.hpp:29
std::size_t PredictionIndex
Details on the subset to extract in OracularSubsettedSlabCache.
Definition OracularSubsettedSlabCache.hpp:36
Index_ block_end
Definition OracularSubsettedSlabCache.hpp:62
Index_ block_length
Definition OracularSubsettedSlabCache.hpp:55
std::vector< Index_ > indices
Definition OracularSubsettedSlabCache.hpp:72
OracularSubsettedSlabCacheSelectionType selection
Definition OracularSubsettedSlabCache.hpp:40
Index_ block_start
Definition OracularSubsettedSlabCache.hpp:49
std::unordered_map< Index_, Index_ > mapping
Definition OracularSubsettedSlabCache.hpp:79