1 #ifndef UTOPIA_MODELS_HDFBENCH_HH
2 #define UTOPIA_MODELS_HDFBENCH_HH
14 #include <boost/iterator/counting_iterator.hpp>
35 public Model<HdfBenchModel, HdfBenchModelTypes>
50 using Clock = std::chrono::high_resolution_clock;
53 using Time = std::chrono::high_resolution_clock::time_point;
80 std::map<std::string, double>
_times;
88 std::map<std::string, std::shared_ptr<DataSet>>
_dsets;
106 this->
_log->debug(
"Loading benchmark configurations ...");
107 std::map<std::string, Config> cfg;
110 this->
_log->trace(
"Loading benchmark configuration '{}' ...",
114 cfg[bname] = get_as<Config>(bname, this->
_cfg);
116 catch (std::exception &e) {
117 std::cerr <<
"Could not find a benchmark configuration with "
118 "name '" << bname <<
"'! Make sure the given "
119 "configuration contains such an entry."
120 << std::endl <<
"Original error message: ";
125 this->
_log->debug(
"Got {} benchmark configurations.", cfg.size());
141 template<
class ParentModel>
143 const std::string& name,
144 ParentModel& parent_model,
149 Base(name, parent_model, custom_cfg),
170 if (_delete_afterwards) {
171 throw std::invalid_argument(
"delete_afterwards feature is not yet "
179 this->
_log->debug(
"Associating setup functions ...");
180 _setup_funcs[
"setup_nd"] =
setup_nd;
184 this->
_log->debug(
"Associating write functions ...");
188 this->
_log->debug(
"Associated {} setup and {} write function(s).",
193 const bool initial_write = get_as<bool>(
"initial_write", this->
_cfg);
194 this->
_log->debug(
"initial_write: {}, sleep_step: {}s, "
195 "sleep_bench: {}s", initial_write ?
"yes" :
"no",
198 this->
_log->info(
"Performing setup and initial benchmarks ...");
202 _times[bname] = this->benchmark<true>(bname);
213 _dset_times->add_attribute(
"dim_name__1",
"benchmark");
215 _dset_times->add_attribute(
"initial_write", initial_write);
218 this->
_log->debug(
"Finished constructing HdfBench '{}'.", this->
_name);
260 [
this](
const auto& bname) {
261 return this->_times.at(bname);
271 template<
bool setup=false>
278 if constexpr (setup) {
279 bfunc =
_setup_funcs.at(get_as<std::string>(
"setup_func", bcfg));
282 bfunc =
_write_funcs.at(get_as<std::string>(
"write_func", bcfg));
286 const auto btime = bfunc(bname, bcfg);
289 this->
_log->debug(
"Benchmark result {:>20s} {} : {:>10.3f} ms",
290 bname, setup ?
"setup" :
"write", btime * 1E3);
302 return seconds.count();
314 auto shape = get_as<std::vector<hsize_t>>(
"write_shape", cfg);
317 const auto start = Clock::now();
322 _dsets[bname]->set_capacity(shape);
331 const auto time_setup = this->
setup_nd(bname, cfg);
334 const auto chunks = get_as<std::vector<hsize_t>>(
"chunks", cfg);
336 const auto start = Clock::now();
340 _dsets[bname]->set_chunksize(chunks);
352 const auto val = get_as<double>(
"const_val", cfg);
355 const auto shape = get_as<std::vector<std::size_t>>(
"write_shape", cfg);
356 const auto it_len = std::accumulate(shape.begin(), shape.end(),
357 1, std::multiplies<std::size_t>());
359 const auto start = Clock::now();
363 _dsets[bname]->write(boost::counting_iterator<std::size_t>(0),
364 boost::counting_iterator<std::size_t>(it_len),
365 [&val]([[maybe_unused]]
auto &count){
Base class interface for Models using the CRT Pattern.
Definition: model.hh:112
const std::shared_ptr< DataGroup > _hdfgrp
The HDF group this model instance should write its data to.
Definition: model.hh:176
Time get_time_max() const
Return the maximum time possible for this model.
Definition: model.hh:403
typename ModelTypes::DataSet DataSet
Data type that is used for storing data.
Definition: model.hh:125
const Config _cfg
Config node belonging to this model instance.
Definition: model.hh:158
const std::string _name
Name of the model instance.
Definition: model.hh:149
std::shared_ptr< DataSet > create_dset(const std::string name, const std::shared_ptr< DataGroup > &hdfgrp, std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
Create a new dataset within the given group.
Definition: model.hh:752
typename ModelTypes::Config Config
Data type that holds the configuration.
Definition: model.hh:116
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition: model.hh:164
The HdfBench Model.
Definition: HdfBench.hh:36
void monitor()
Monitor model information.
Definition: HdfBench.hh:249
const std::vector< std::string > _benchmarks
Names of benchmarks.
Definition: HdfBench.hh:74
typename Base::DataSet DataSet
Data type for a dataset.
Definition: HdfBench.hh:42
std::map< std::string, double > _times
The results of the measurements, stored under the benchmark name.
Definition: HdfBench.hh:80
double time_since(const Time start)
Returns the time (in seconds) since the given time point.
Definition: HdfBench.hh:295
std::shared_ptr< DataSet > _dset_times
Dataset to store the write times in.
Definition: HdfBench.hh:85
double benchmark(const std::string &bname)
Carries out the benchmark associated with the given name.
Definition: HdfBench.hh:272
HdfBenchModel(const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the HdfBench model.
Definition: HdfBench.hh:142
std::function< double(const std::string, Config)> BenchFunc
Type of a benchmark function pointer.
Definition: HdfBench.hh:59
Model< HdfBenchModel, HdfBenchModelTypes > Base
The base model type.
Definition: HdfBench.hh:39
const std::chrono::duration< double > _sleep_step
Sleep time in seconds at the beginning of each step.
Definition: HdfBench.hh:96
std::chrono::high_resolution_clock Clock
Type of clock.
Definition: HdfBench.hh:50
std::map< std::string, std::shared_ptr< DataSet > > _dsets
Dataset to write test data to are stored in a map of dataset pointers.
Definition: HdfBench.hh:88
void write_data()
Write the result times of each benchmark.
Definition: HdfBench.hh:258
std::map< std::string, Config > load_benchmarks()
Load the benchmark configurations into a map.
Definition: HdfBench.hh:105
double time_between(const Time start, const Time end)
Returns the absolute time (in seconds) between the given time points.
Definition: HdfBench.hh:300
typename Base::Config Config
Data type that holds the configuration.
Definition: HdfBench.hh:45
std::chrono::duration< double > DurationType
Type of the duration measure, should be a floating-point type.
Definition: HdfBench.hh:56
const std::chrono::duration< double > _sleep_bench
Sleep time in seconds before each benchmark.
Definition: HdfBench.hh:99
BenchFunc write_const
Writes a constant value into the dataset.
Definition: HdfBench.hh:350
const std::map< std::string, Config > _bench_cfgs
Configuration for the benchmarks.
Definition: HdfBench.hh:77
BenchFunc setup_nd_with_chunks
Definition: HdfBench.hh:329
std::map< std::string, BenchFunc > _write_funcs
A map of implemented write functions.
Definition: HdfBench.hh:71
const bool _delete_afterwards
Whether to delete datasets after the last step.
Definition: HdfBench.hh:93
std::chrono::high_resolution_clock::time_point Time
Type of a time point, retrieved from the clock.
Definition: HdfBench.hh:53
void perform_step()
Iterate a single step.
Definition: HdfBench.hh:229
std::map< std::string, BenchFunc > _setup_funcs
A map of implemented setup functions for datasets.
Definition: HdfBench.hh:68
BenchFunc setup_nd
Definition: HdfBench.hh:312
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
ReturnType get_as(const std::string &key, const DataIO::Config &node)
This function is a wrapper around the yaml-cpp YAML::Node::as function.
Definition: cfg_utils.hh:158
Wrapper struct for defining model class data types.
Definition: model.hh:92