1#ifndef TATAMI_HDF5_SPARSE_UTILS_HPP
2#define TATAMI_HDF5_SPARSE_UTILS_HPP
13#include "sanisizer/sanisizer.hpp"
18namespace CompressedSparseMatrix_internal {
20template<
typename CachedValue_,
typename CachedIndex_>
21std::size_t size_of_cached_element(
bool needs_cached_value,
bool needs_cached_index) {
22 return (needs_cached_index ?
sizeof(CachedIndex_) : 0) + (needs_cached_value ?
sizeof(CachedValue_) : 0);
27 H5::DataSet data_dataset;
28 H5::DataSet index_dataset;
29 H5::DataSpace dataspace;
30 H5::DataSpace memspace;
33struct ChunkCacheSizes {
34 ChunkCacheSizes() =
default;
35 ChunkCacheSizes(hsize_t value, hsize_t index) : value(value), index(index) {}
53inline hsize_t compute_chunk_cache_size(hsize_t nonzeros, hsize_t chunk_length, std::size_t element_size) {
54 if (chunk_length == 0) {
57 hsize_t num_chunks = std::min(nonzeros / chunk_length + (nonzeros % chunk_length > 0),
static_cast<hsize_t
>(3));
58 hsize_t cache_size = sanisizer::product<hsize_t>(num_chunks, chunk_length);
59 return std::max(sanisizer::product<hsize_t>(cache_size, element_size), sanisizer::cap<hsize_t>(1000000));
67template<
typename Index_>
70 const std::string& file_name,
71 const std::string& value_name,
72 const std::string& index_name,
75 const std::vector<hsize_t>& pointers,
76 std::size_t slab_cache_size,
78 ChunkCacheSizes chunk_cache_sizes
81 value_name(value_name),
82 index_name(index_name),
83 primary_dim(primary_dim),
84 secondary_dim(secondary_dim),
86 slab_cache_size(slab_cache_size),
87 max_non_zeros(max_non_zeros),
88 chunk_cache_sizes(std::move(chunk_cache_sizes))
91 const std::string& file_name;
92 const std::string& value_name;
93 const std::string& index_name;
97 const std::vector<hsize_t>& pointers;
99 std::size_t slab_cache_size;
100 Index_ max_non_zeros;
101 ChunkCacheSizes chunk_cache_sizes;
105template<
typename Index_>
106void initialize(
const MatrixDetails<Index_>& details, std::unique_ptr<Components>& h5comp) {
108 h5comp.reset(
new Components);
110 auto create_dapl = [&](hsize_t cache_size) -> H5::DSetAccPropList {
112 H5::DSetAccPropList dapl(H5::DSetAccPropList::DEFAULT.getId());
113 dapl.setChunkCache(H5D_CHUNK_CACHE_NSLOTS_DEFAULT, cache_size, H5D_CHUNK_CACHE_W0_DEFAULT);
117 h5comp->file.openFile(details.file_name, H5F_ACC_RDONLY);
118 h5comp->data_dataset = h5comp->file.openDataSet(details.value_name, create_dapl(details.chunk_cache_sizes.value));
119 h5comp->index_dataset = h5comp->file.openDataSet(details.index_name, create_dapl(details.chunk_cache_sizes.index));
120 h5comp->dataspace = h5comp->data_dataset.getSpace();
124inline void destroy(std::unique_ptr<Components>& h5comp) {
134template<
typename Po
inter_,
typename Index_>
135std::pair<Pointer_, Pointer_> narrow_primary_extraction_range(
136 const Pointer_ ptr_start,
137 const Pointer_ ptr_end,
138 const Index_ block_start,
139 const Index_ block_end,
140 const Index_ secondary_dim
142 const Pointer_ num_nonzeros = ptr_end - ptr_start;
143 const Pointer_ new_ptr_end = ptr_start + sanisizer::min(block_end, num_nonzeros);
144 const Pointer_ new_ptr_start = ptr_end - sanisizer::min(secondary_dim - block_start, num_nonzeros);
155 return std::make_pair(new_ptr_start, new_ptr_end);
158template<
class IndexIt_,
typename Index_>
159void refine_primary_limits(IndexIt_& indices_start, IndexIt_& indices_end, Index_ extent, Index_ smallest, Index_ largest_plus_one) {
162 indices_start = std::lower_bound(indices_start, indices_end, smallest, [](Index_ a, Index_ b) ->
bool {
return a < b; });
165 if (largest_plus_one != extent) {
166 indices_end = std::lower_bound(indices_start, indices_end, largest_plus_one, [](Index_ a, Index_ b) ->
bool {
return a < b; });
170template<
typename Slab_,
typename Value_,
typename Index_>
174 std::copy_n(slab.value, slab.number, value_buffer);
175 output.value = value_buffer;
178 std::copy_n(slab.index, slab.number, index_buffer);
179 output.index = index_buffer;
184template<
typename Slab_,
typename Value_,
typename Index_>
185Value_* slab_to_dense(
const Slab_& slab, Value_* buffer, Index_ extract_length) {
186 std::fill_n(buffer, extract_length, 0);
187 for (Index_ i = 0; i < slab.number; ++i) {
188 buffer[slab.index[i]] = slab.value[i];
Representations for matrix data in HDF5 files.
Definition CompressedSparseMatrix.hpp:24
void serialize(Function_ f)
Definition serialize.hpp:53
Default locking for serial access.