tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
SlabCacheStats.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_SLAB_CACHE_STATS_HPP
2#define TATAMI_CHUNKED_SLAB_CACHE_STATS_HPP
3
4#include <algorithm>
5#include <cstddef>
6
7#include "sanisizer/sanisizer.hpp"
8
14namespace tatami_chunked {
15
19template<typename Index_>
20Index_ compute_max_slabs_in_cache(std::size_t slab_size_in_elements, Index_ num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache) {
21 if (slab_size_in_elements == 0) {
22 return num_slabs;
23 }
24
25 auto tmp = cache_size_in_elements / slab_size_in_elements;
26 if (tmp == 0 && require_minimum_cache) {
27 return 1;
28 }
29
30 return sanisizer::min(tmp, num_slabs);
31}
46template<typename Index_>
52
58
70 SlabCacheStats(Index_ target_length, Index_ non_target_length, Index_ target_num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache) :
71 // Don't be tempted to do unsafe casts of target_length to size_t, as a smaller type could allow us to sip the overflow check for the product.
72 // Also, this class might be used outside of the tatami::Matrix contract (i.e., Index_ might store values beyond std::size_t).
73 slab_size_in_elements(sanisizer::product<std::size_t>(target_length, non_target_length)),
74 max_slabs_in_cache(compute_max_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_elements, require_minimum_cache))
75 {}
76
90 SlabCacheStats(Index_ target_length, Index_ non_target_length, Index_ target_num_slabs, std::size_t cache_size_in_bytes, std::size_t element_size, bool require_minimum_cache) :
91 slab_size_in_elements(sanisizer::product<std::size_t>(target_length, non_target_length)),
93 if (element_size == 0) {
94 return target_num_slabs;
95 } else {
96 return compute_max_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_bytes / element_size, require_minimum_cache);
97 }
98 }())
99 {}
100};
101
102}
103
104#endif
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
Statistics for slab caching.
Definition SlabCacheStats.hpp:47
SlabCacheStats(Index_ target_length, Index_ non_target_length, Index_ target_num_slabs, std::size_t cache_size_in_bytes, std::size_t element_size, bool require_minimum_cache)
Definition SlabCacheStats.hpp:90
std::size_t slab_size_in_elements
Definition SlabCacheStats.hpp:51
Index_ max_slabs_in_cache
Definition SlabCacheStats.hpp:57
SlabCacheStats(Index_ target_length, Index_ non_target_length, Index_ target_num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache)
Definition SlabCacheStats.hpp:70