Commit 1685d941 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[IO] Add filename[_run]_params generator, base class of saving_params

parent 78651737
......@@ -19,8 +19,6 @@
#include <mpi.h>
#endif //defined(LIMA_ENABLE_MPI)
#include <fmt/format.h>
#include <lima/exceptions.hpp>
#include <lima/logging.hpp>
#include <lima/core/frame.hpp>
......@@ -143,16 +141,6 @@ namespace io
std::vector<std::shared_ptr<driver_counter>> m_drivers; // Vector elements must be copy-constructible
std::function<driver_t(std::string const&)> m_driver_factory;
/// Format the filename according to base path, prefix, start_number and suffix
std::filesystem::path filename(std::size_t driver_idx = 0)
{
using namespace fmt::literals;
return fmt::format(m_params.filename_format, "filename_prefix"_a = m_params.filename_prefix,
"rank"_a = m_rank, "file_number"_a = m_params.start_number + driver_idx,
"filename_suffix"_a = m_params.filename_suffix);
}
bool is_driver_available(std::size_t driver_idx) { return (bool) m_drivers[driver_idx]; }
/// Open and initialize a file
......@@ -161,7 +149,8 @@ namespace io
if (driver_idx >= m_drivers.size())
LIMA_THROW_EXCEPTION(io_error("Unexpected I/O driver index"));
std::filesystem::path filepath = m_params.base_path / filename(driver_idx);
filename_run_params run_pars{int(m_params.start_number + driver_idx), m_rank};
std::filesystem::path filepath = m_params.filename(run_pars);
LIMA_LOG(trace, io) << "Opening file " << filepath;
//Check whether the file exists, etc
......
......@@ -18,28 +18,41 @@ namespace lima
{
namespace io
{
BOOST_DESCRIBE_STRUCT(saving_params, (),
(base_path, filename_format, filename_prefix, filename_suffix, start_number,
file_exists_policy, nb_frames_per_file))
BOOST_DESCRIBE_STRUCT(filename_run_params, (), (index, rank))
// clang-format off
BOOST_ANNOTATE_MEMBER(saving_params, base_path,
(desc, "base path"),
(doc, "The base path where the files will be saved"))
BOOST_ANNOTATE_MEMBER(filename_run_params, index,
(desc, "index"),
(doc, "The index of the file in the sequence"))
BOOST_ANNOTATE_MEMBER(saving_params, filename_format,
(desc, "filename format"),
(doc, "The format of the generated filename "
"[default: {filename_prefix}_{rank}_{file_number:05d}{filename_suffix}]"))
BOOST_ANNOTATE_MEMBER(filename_run_params, rank,
(desc, "rank"),
(doc, "The (MPI) rank of the file writer (in the receivers group)"))
// clang-format on
// clang-format off
#define LIMA_IO_DESCRIBE_FILENAME(x) \
BOOST_DESCRIBE_STRUCT(x, (), (base_path, filename_format, filename_prefix, filename_suffix, filename)) \
\
BOOST_ANNOTATE_MEMBER(x, base_path, (desc, "base path"), (doc, "The base path where the files will be saved")) \
\
BOOST_ANNOTATE_MEMBER(x, filename_format, (desc, "filename format"), \
(doc, "The format of the generated filename " \
"[default: {filename_prefix}_{rank}_{file_number:05d}{filename_suffix}]")) \
\
BOOST_ANNOTATE_MEMBER(x, filename_prefix, (desc, "filename prefix"), \
(doc, "The prefix of the filename, see <filename_format> for usage description")) \
\
BOOST_ANNOTATE_MEMBER(x, filename_suffix, (desc, "filename suffix"), \
(doc, "The suffix of the filename, see <filename_format> for usage description"))
// clang-format on
BOOST_ANNOTATE_MEMBER(saving_params, filename_prefix,
(desc, "filename prefix"),
(doc, "The prefix of the filename, see <filename_format> for usage description"))
LIMA_IO_DESCRIBE_FILENAME(filename_saving_params)
BOOST_ANNOTATE_MEMBER(saving_params, filename_suffix,
(desc, "filename suffix"),
(doc, "The suffix of the filename, see <filename_format> for usage description"))
BOOST_DESCRIBE_STRUCT(saving_params, (filename_saving_params),
(start_number, file_exists_policy, nb_frames_per_file))
// clang-format off
BOOST_ANNOTATE_MEMBER(saving_params, start_number,
(desc, "start number"),
(doc, "Start number in the file sequence"))
......
......@@ -12,6 +12,8 @@
#include <lima/io/const.hpp>
#include <lima/io/enums.hpp>
#include <fmt/format.h>
namespace lima
{
namespace io
......@@ -20,12 +22,43 @@ namespace io
// only by the standard library so safe to bind here
using std::string_literals::operator"" s;
struct saving_params
struct filename_run_params
{
int index = 0;
int rank = 0;
};
struct DefaultFilenameValues
{
static constexpr char prefix[] = "";
};
template <typename DefaultValues>
struct filename_params_base
{
std::filesystem::path base_path = std::filesystem::temp_directory_path();
std::string filename_format = default_filename_format;
std::string filename_prefix = "output"s;
std::string filename_prefix = DefaultValues::prefix;
std::string filename_suffix = ".h5"s;
std::filesystem::path filename(filename_run_params const& run_pars = {}) const
{
using namespace fmt::literals;
auto name = fmt::format(filename_format, "filename_prefix"_a = filename_prefix, "rank"_a = run_pars.rank,
"file_number"_a = run_pars.index, "filename_suffix"_a = filename_suffix);
return base_path / name;
}
};
struct DefaultSavingValues : DefaultFilenameValues
{
static constexpr char prefix[] = "output";
};
using filename_saving_params = filename_params_base<DefaultSavingValues>;
struct saving_params : filename_saving_params
{
int start_number = 0;
file_exists_policy_enum file_exists_policy = file_exists_policy_enum::abort;
int nb_frames_per_file = 1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment