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
25template<typename MaxSlabs_>
31
37
51 template<typename Index_>
52 SlabCacheStats(Index_ target_length, Index_ non_target_length, MaxSlabs_ target_num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache) :
53 // Don't be tempted to do unsafe casts of target_length to size_t,
54 // as this class might be used outside of the tatami::Matrix contract (i.e., Index_ might store values beyond std::size_t).
55 slab_size_in_elements(sanisizer::product<std::size_t>(target_length, non_target_length)),
56 max_slabs_in_cache(compute_max_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_elements, require_minimum_cache))
57 {}
58
74 template<typename Index_>
75 SlabCacheStats(Index_ target_length, Index_ non_target_length, MaxSlabs_ target_num_slabs, std::size_t cache_size_in_bytes, std::size_t element_size, bool require_minimum_cache) :
76 slab_size_in_elements(sanisizer::product<std::size_t>(target_length, non_target_length)),
78 if (element_size == 0) {
79 return target_num_slabs;
80 } else {
81 return compute_max_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_bytes / element_size, require_minimum_cache);
82 }
83 }())
84 {}
85
86private:
87 static MaxSlabs_ compute_max_slabs_in_cache(std::size_t slab_size_in_elements, MaxSlabs_ num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache) {
88 if (slab_size_in_elements == 0) {
89 return num_slabs;
90 }
91
92 auto tmp = cache_size_in_elements / slab_size_in_elements;
93 if (tmp == 0 && require_minimum_cache) {
94 return 1;
95 }
96
97 return sanisizer::min(tmp, num_slabs);
98 }
99};
100
101}
102
103#endif
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
Statistics for slab caching.
Definition SlabCacheStats.hpp:26
MaxSlabs_ max_slabs_in_cache
Definition SlabCacheStats.hpp:36
std::size_t slab_size_in_elements
Definition SlabCacheStats.hpp:30
SlabCacheStats(Index_ target_length, Index_ non_target_length, MaxSlabs_ target_num_slabs, std::size_t cache_size_in_elements, bool require_minimum_cache)
Definition SlabCacheStats.hpp:52
SlabCacheStats(Index_ target_length, Index_ non_target_length, MaxSlabs_ target_num_slabs, std::size_t cache_size_in_bytes, std::size_t element_size, bool require_minimum_cache)
Definition SlabCacheStats.hpp:75