BioCMAMC-ST
data_exporter.hpp
1#ifndef __CORE_DATA_EXPORTER_HPP__
2#define __CORE_DATA_EXPORTER_HPP__
3
4#include "common/logger.hpp"
5#include <common/common.hpp>
6#include <common/execinfo.hpp>
7#include <core/simulation_parameters.hpp>
8#include <cstddef>
9#include <cstdint>
10#include <memory>
11#include <optional>
12#include <span>
13#include <string>
14#include <string_view>
15#include <unordered_map>
16#include <variant>
17#include <vector>
18
19namespace
20{
21 template <typename MapType>
22 auto&
23 get_descriptor_impl(MapType& descriptors, std::string_view name)
24 {
25 try
26 {
27 return descriptors.at(std::string(name));
28 }
29 catch (const std::out_of_range&)
30 {
31 throw std::runtime_error(
32 IO::format("DataExporter: unknown matrix: ", name));
33 }
34 }
35}
36
37namespace Core
38{
39
40 // static inline std::string get_filename(std::string_view filename,
41 // std::size_t current_rank)
42 // {
43 // return std::string(filename) + "_partial_" + std::to_string(current_rank)
44 // + ".h5";
45 // }
46 bool
47 fill_and_check_result_file_path(const std::shared_ptr<IO::Logger>& logger,
66 {
67 public:
68 DataExporter(const DataExporter&) = delete;
72 DataExporter() = default;
85 void do_link(std::string_view filename,
86 std::string_view link_name,
87 std::string_view groupname);
88
89 void
90 set_logger(std::shared_ptr<IO::Logger> _logger)
91 {
92 logger = std::move(_logger);
93 }
94
95 protected:
96 std::shared_ptr<IO::Logger> logger;
97
109 {
110 std::string name;
111 std::vector<size_t> dims;
112 std::vector<size_t> max_dims;
113 std::optional<std::vector<unsigned long long>>
117 };
118
119 // Using type definitions with aligned comments
121 = std::variant<uint64_t, int, std::string>;
124 = std::unordered_map<std::string,
128 = Kokkos::View<double**,
129 Kokkos::LayoutRight,
130 HostSpace>;
131 using simple_export_t = std::variant<size_t,
132 std::string,
133 std::vector<size_t>,
134 std::vector<std::string>,
135 std::vector<double>,
136 double,
137 uint32_t>;
140 = std::unordered_map<std::string,
143
144 using matrix_variant_t
145 = std::variant<std::span<const double>,
146 std::span<const std::size_t>,
147 double>;
148
149 // Methods
151 explicit DataExporter(const ExecInfo& info,
152 std::string_view _filename,
153 std::optional<export_metadata_t> user_description
154 = std::nullopt);
156
157 void write_matrix(std::string_view name,
158 std::span<const double> values,
159 bool compress = false);
160
161 void write_matrix(std::string_view name,
162 std::span<const double> values,
163 size_t n_row,
164 size_t n_col,
165 bool compress = false);
166
167 void prepare_matrix(MultiMatrixDescription description);
168
169 void append_matrix(std::string_view name, matrix_variant_t data);
170
171 void append_array(std::string_view name,
172 std::span<const double> data,
173 uint64_t last_size = 0);
174
175 void write_properties(std::optional<std::string> specific_dataspace,
176 const export_metadata_kv& values);
177
178 void write_simple(const export_initial_kv& values, std::string_view root);
180 void write_simple(std::string specific_dataspace,
181 const simple_export_t& values);
184 uint64_t export_counter = 0;
185
186 const auto&
187 get_descriptor(std::string_view name) const
189 return get_descriptor_impl(m_descriptors, name);
190 };
191
192 auto&
193 get_descriptor_mut(std::string_view name)
195 return get_descriptor_impl(m_descriptors, name);
196 };
197
198 std::span<const std::size_t>
199 get_dim(std::string_view name) const
200 {
201 const auto& dims = get_descriptor(name).dims;
202 return dims;
204
205 private:
206 std::unordered_map<std::string, MultiMatrixDescription> m_descriptors;
207 class impl;
208 std::unique_ptr<impl> pimpl;
209 };
210
211} // namespace Core
212#endif
void append_matrix(std::string_view name, matrix_variant_t data)
Definition impl_default_dataexporter.cpp:41
void append_array(std::string_view name, std::span< const double > data, uint64_t last_size=0)
Definition impl_default_dataexporter.cpp:46
void do_link(std::string_view filename, std::string_view link_name, std::string_view groupname)
Creates a link to a specified file.
Definition impl_default_dataexporter.cpp:11
std::variant< uint64_t, int, std::string > export_metadata_t
Definition data_exporter.hpp:120
std::variant< std::span< const double >, std::span< const std::size_t >, double > matrix_variant_t
Variant for matrix data types.
Definition data_exporter.hpp:140
void write_simple(const export_initial_kv &values, std::string_view root)
Definition impl_default_dataexporter.cpp:68
std::shared_ptr< IO::Logger > logger
Definition data_exporter.hpp:96
auto & get_descriptor_mut(std::string_view name)
Definition data_exporter.hpp:188
void write_matrix(std::string_view name, std::span< const double > values, bool compress=false)
Definition impl_default_dataexporter.cpp:29
uint64_t export_counter
Definition data_exporter.hpp:179
std::unordered_map< std::string, MultiMatrixDescription > m_descriptors
Definition data_exporter.hpp:201
void write_properties(std::optional< std::string > specific_dataspace, const export_metadata_kv &values)
Definition impl_default_dataexporter.cpp:53
std::unordered_map< std::string, export_metadata_t > export_metadata_kv
Definition data_exporter.hpp:122
DataExporter()=default
void prepare_matrix(MultiMatrixDescription description)
Definition impl_default_dataexporter.cpp:36
DataExporter(const DataExporter &)=delete
DataExporter & operator=(DataExporter &&)=delete
Kokkos::View< double **, Kokkos::LayoutRight, HostSpace > ViewParticleProperties
View for particle properties.
Definition data_exporter.hpp:125
export_metadata_kv metadata
Definition data_exporter.hpp:178
std::variant< size_t, std::string, std::vector< size_t >, std::vector< std::string >, std::vector< double >, double, uint32_t > simple_export_t
Definition data_exporter.hpp:128
std::unique_ptr< impl > pimpl
Definition data_exporter.hpp:203
std::unordered_map< std::string, simple_export_t > export_initial_kv
Definition data_exporter.hpp:136
void set_logger(std::shared_ptr< IO::Logger > _logger)
Definition data_exporter.hpp:90
DataExporter(DataExporter &&)=delete
std::span< const std::size_t > get_dim(std::string_view name) const
Definition data_exporter.hpp:194
const auto & get_descriptor(std::string_view name) const
Definition data_exporter.hpp:182
DataExporter & operator=(const DataExporter &)=delete
Core component to perform simulation.
Definition data_exporter.hpp:38
bool fill_and_check_result_file_path(const std::shared_ptr< IO::Logger > &logger, Core::UserControlParameters &params)
Definition main_exporter.cpp:237
std::string format(MsgType &&... msgs)
Definition logger.hpp:160
Definition species_name_extractor.hpp:7
Describes the properties of a multi-dimensional matrix for export operations.
Definition data_exporter.hpp:109
std::vector< size_t > dims
Dataset dimensions.
Definition data_exporter.hpp:111
std::optional< std::vector< unsigned long long > > chunk_dims
Data chunk along each dimension.
Definition data_exporter.hpp:114
bool is_integer
Matrix data is integer type or floating point.
Definition data_exporter.hpp:116
std::vector< size_t > max_dims
Expected dataset max dimensions.
Definition data_exporter.hpp:112
bool compression
Matrix data has to be compressed or not.
Definition data_exporter.hpp:115
std::string name
Dataset name.
Definition data_exporter.hpp:110
A structure to hold user-defined control parameters for simulation settings.
Definition simulation_parameters.hpp:24
Definition execinfo.hpp:21