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