Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose env var handling to python #856

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pyphare/pyphare/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def cpp_lib(override=None):
return importlib.import_module("pybindlibs.cpp")
try:
return importlib.import_module("pybindlibs.cpp_dbg")
except ImportError as err:
except ImportError:
return importlib.import_module("pybindlibs.cpp")


Expand All @@ -36,6 +36,20 @@ def build_config():
return cpp_etc_lib().phare_build_config()


def env_vars():
return cpp_etc_lib().phare_env_vars()


def print_env_vars_info():
# see: src/core/env.hpp
for env_var_name, env_var in cpp_etc_lib().phare_env_vars().items():
print(f"{env_var_name}: {env_var.desc}")
print("Options:")
for option_key, option_val in env_var.options:
print(f" {option_key}: {option_val}")
print("")


def build_config_as_json():
return json.dumps(build_config())

Expand Down
1 change: 1 addition & 0 deletions res/cmake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (test AND ${PHARE_EXEC_LEVEL_MIN} GREATER 0) # 0 = no tests
add_subdirectory(tests/core/data/maxwellian_particle_initializer)
add_subdirectory(tests/core/data/particle_initializer)
add_subdirectory(tests/core/utilities/box)
add_subdirectory(tests/core/utilities/env)
add_subdirectory(tests/core/utilities/range)
add_subdirectory(tests/core/utilities/index)
add_subdirectory(tests/core/utilities/indexer)
Expand Down
118 changes: 118 additions & 0 deletions src/core/env.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#ifndef PHARE_CORE_ENV_HPP
#define PHARE_CORE_ENV_HPP

// Single source for handling env vars

#include <array>
#include <cstdint>
#include <optional>
#include <string_view>
#include <unordered_map>
#include "core/utilities/types.hpp"
#include "core/utilities/mpi_utils.hpp"

namespace PHARE::env
{

struct Var
{
using value_type = std::string;
using results_type = std::unordered_map<std::string, std::string>;
auto constexpr static noop_results = [](Var const&) { return results_type{}; };

std::string_view const id;
std::string_view const desc;
std::vector<std::pair<std::string_view, std::string_view>> const options;

std::optional<value_type> const _default = std::nullopt;
std::function<results_type(Var const&)> const _results = noop_results;
std::optional<value_type> const v = get();
results_type const results = _results(*this);

std::optional<value_type> get() const
{
std::string _id{id};
if (_default)
return core::get_env(_id, *_default);
return core::get_env(_id);
}
Comment on lines +32 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling in get method.

The get method retrieves environment variables but does not handle potential errors from core::get_env. Consider adding error handling or logging to capture any issues that may arise.

std::optional<value_type> get() const
{
    std::string _id{id};
    try {
        if (_default)
            return core::get_env(_id, *_default);
        return core::get_env(_id);
    } catch (const std::exception& e) {
        // Handle or log the error
        return std::nullopt;
    }
}

auto const& operator()() const { return v; }
auto const& operator()(std::string const& s) const { return results.at(s); }
auto const& operator()(std::string const& s, std::string const& default_) const
{
if (results.count(s))
return results.at(s);
return default_;
}
bool exists() const { return v != std::nullopt; }
operator bool() const { return exists(); }
};
Comment on lines +17 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using explicit constructors for Var struct to enhance type safety.

The Var struct is initialized with several member variables that are likely to be required upon creation. Consider using an explicit constructor to ensure that all necessary fields are initialized correctly and to improve code readability.

struct Var
{
    explicit Var(std::string_view id, std::string_view desc, std::vector<std::pair<std::string_view, std::string_view>> options, std::optional<value_type> def = std::nullopt, std::function<results_type(Var const&)> res = noop_results)
    : id{id}, desc{desc}, options{std::move(options)}, _default{def}, _results{res}, v{get()}, results{_results(*this)}
    {}
};


} // namespace PHARE::env

namespace PHARE
{
class Env
{
public:
template<typename T>
using map_t = std::unordered_map<std::string, T const* const>;

static Env& INSTANCE()
{
if (!self)
self = std::make_unique<Env>();
return *self;
}
static auto& reinit() { return *(self = std::make_unique<Env>()); }

env::Var const PHARE_LOG{
"PHARE_LOG",
"Write logs to $CWD/.log",
{{{"RANK_FILES", "Write logs $CWD/.log, a file per rank"},
{"DATETIME_FILES", "Write logs $CWD/.log, filename per rank and datetime"},
{"NONE", "print normally to std::cout"}}},
std::nullopt,
[](auto const& self) {
std::string static const file_key = "PHARE_LOG_FILE";
typename env::Var::results_type map;
if (auto const& opt = self())
{
auto const& val = *opt;
if (val == "RANK_FILES")
map[file_key] = ".log/" + std::to_string(core::mpi::rank()) + ".out";
else if (val == "DATETIME_FILES")
{
auto date_time = core::mpi::date_time();
auto rank = std::to_string(core::mpi::rank());
auto size = std::to_string(core::mpi::size());
map[file_key] = ".log/" + date_time + "_" + rank + "_of_" + size + ".out";
}
else if (val != "NONE")
throw std::runtime_error("PHARE_LOG invalid type, valid keys are "
"RANK_FILES/DATETIME_FILES/NONE");
}
return map;
} //
};
Comment on lines +69 to +97
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add validation for PHARE_LOG options.

The PHARE_LOG variable has specific valid options. Consider adding validation to ensure that only valid options are accepted.

env::Var const PHARE_LOG{
    "PHARE_LOG",
    "Write logs to $CWD/.log",
    {{{"RANK_FILES", "Write logs $CWD/.log, a file per rank"},
      {"DATETIME_FILES", "Write logs $CWD/.log, filename per rank and datetime"},
      {"NONE", "print normally to std::cout"}}},
    std::nullopt,
    [](auto const& self) {
        std::string static const file_key = "PHARE_LOG_FILE";
        typename env::Var::results_type map;
        if (auto const& opt = self())
        {
            auto const& val = *opt;
            if (val == "RANK_FILES")
                map[file_key] = ".log/" + std::to_string(core::mpi::rank()) + ".out";
            else if (val == "DATETIME_FILES")
            {
                auto date_time = core::mpi::date_time();
                auto rank      = std::to_string(core::mpi::rank());
                auto size      = std::to_string(core::mpi::size());
                map[file_key]  = ".log/" + date_time + "_" + rank + "_of_" + size + ".out";
            }
            else if (val != "NONE")
                throw std::runtime_error("PHARE_LOG invalid type, valid keys are "
                                         "RANK_FILES/DATETIME_FILES/NONE");
        }
        return map;
    } //
};

env::Var const PHARE_SCOPE_TIMING{
"PHARE_SCOPE_TIMING", "Enable function scope timing", {{{"1", "ON"}, {"0", "OFF"}}}, "0"};

map_t<env::Var> const vars = {{
{"PHARE_LOG", &PHARE_LOG},
{"PHARE_SCOPE_TIMING", &PHARE_SCOPE_TIMING},
}};

auto& operator()(std::string const& s) const
{
assert(vars.count(s));
return *vars.at(s);
}

private:
static inline std::unique_ptr<Env> self = nullptr;
};
Comment on lines +55 to +114
Copy link

@coderabbitai coderabbitai bot Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making Env class a singleton.

The Env class is designed to be instantiated only once. Consider making it a singleton to enforce this design pattern and simplify access to the instance.

class Env
{
public:
    static Env& INSTANCE()
    {
        static Env instance;
        return instance;
    }

    // Delete copy constructor and assignment operator
    Env(const Env&) = delete;
    Env& operator=(const Env&) = delete;

    // Other methods and member variables...
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a singleton, just in this case we can reset the internal memory easily

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PhilipDeegan, thank you for the clarification. Your approach to implementing the singleton pattern while allowing for the internal memory to be reset provides the necessary flexibility for reinitialization scenarios. This design ensures that the Env instance can be reinitialized when needed, which can be beneficial in certain contexts.


} // namespace PHARE

#endif /* PHARE_CORE_ERRORS_H */
1 change: 1 addition & 0 deletions src/core/utilities/mpi_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std::size_t max(std::size_t const local, int mpi_size)




bool any(bool b)
{
int global_sum, local_sum = static_cast<int>(b);
Expand Down
2 changes: 2 additions & 0 deletions src/core/utilities/mpi_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ inline bool is_init()
return flag > 0;
}



template<typename Data>
NO_DISCARD auto mpi_type_for()
{
Expand Down
30 changes: 30 additions & 0 deletions src/core/utilities/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ namespace core
T var;
};

template<typename T>
NO_DISCARD T from_string(std::string const& s)
{
T t;
std::stringstream ss(s);
ss >> t;
ss >> t;
if (ss.fail())
throw std::runtime_error("PHARE::core::from_string - Conversion failed: " + s);
return t;
}

template<typename T>
NO_DISCARD std::string to_string_with_precision(T const& a_value, std::size_t const len)
{
Expand Down Expand Up @@ -246,6 +258,24 @@ namespace core
}


template<typename T>
NO_DISCARD inline std::optional<T> get_env_as(std::string const& key)
{
if (auto e = get_env(key))
return from_string<T>(*e);
return std::nullopt;
}
Comment on lines +261 to +267
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential conversion errors in get_env_as.

The get_env_as function should handle potential conversion errors to avoid undefined behavior and provide meaningful error messages.

template<typename T>
NO_DISCARD inline std::optional<T> get_env_as(std::string const& key)
{
    if (auto e = get_env(key))
    {
        try {
            return from_string<T>(*e);
        } catch (const std::exception& ex) {
            std::cerr << "Error converting environment variable " << key << ": " << ex.what() << std::endl;
        }
    }
    return std::nullopt;
}



template<typename T>
NO_DISCARD inline T get_env_as(std::string const& key, T const& t)
{
if (auto e = get_env(key))
return from_string<T>(*e);
return t;
}
Comment on lines +270 to +276
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential conversion errors in get_env_as with default value.

The get_env_as function should handle potential conversion errors to avoid undefined behavior and provide meaningful error messages.

template<typename T>
NO_DISCARD inline T get_env_as(std::string const& key, T const& t)
{
    if (auto e = get_env(key))
    {
        try {
            return from_string<T>(*e);
        } catch (const std::exception& ex) {
            std::cerr << "Error converting environment variable " << key << ": " << ex.what() << std::endl;
        }
    }
    return t;
}




} // namespace core
} // namespace PHARE
Expand Down
11 changes: 6 additions & 5 deletions src/hdf5/detail/h5/h5_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class HighFiveFile

~HighFiveFile() {}

NO_DISCARD HiFile& file() { return h5file_; }
NO_DISCARD HiFile& file()
{
return h5file_;
}


template<typename T, std::size_t dim = 1>
Expand Down Expand Up @@ -143,13 +146,11 @@ class HighFiveFile
void write_attribute(std::string const& keyPath, std::string const& key, Data const& data)
{
// assumes all keyPaths and values are identical, and no null patches
// clang-format off
PHARE_DEBUG_DO(
PHARE_DEBUG_DO({
auto const paths = core::mpi::collect(keyPath, core::mpi::size());
if (!core::all(paths, [&](auto const& path) { return path == paths[0]; }))
throw std::runtime_error("Function does not support different paths per mpi core");
)
// clang-format on
})

constexpr bool data_is_vector = core::is_std_vector_v<Data>;

Expand Down
4 changes: 3 additions & 1 deletion src/phare/phare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "core/def/phlop.hpp" // scope timing

#include "core/env.hpp" // scope timing

#include "simulator/simulator.hpp"
#include "core/utilities/algorithm.hpp"
#include "core/utilities/mpi_utils.hpp"
Expand Down Expand Up @@ -41,7 +43,7 @@ class SamraiLifeCycle
= std::make_shared<StreamAppender>(StreamAppender{&std::cout});
SAMRAI::tbox::Logger::getInstance()->setWarningAppender(appender);
PHARE_WITH_PHLOP( //
if (auto e = core::get_env("PHARE_SCOPE_TIMING", "false"); e == "1" || e == "true")
if (auto e = Env::INSTANCE().PHARE_SCOPE_TIMING(); e == "1" || e == "true")
phlop::ScopeTimerMan::INSTANCE()
.file_name(".phare_times." + std::to_string(core::mpi::rank()) + ".txt")
.init(); //
Expand Down
24 changes: 21 additions & 3 deletions src/python3/cpp_etc.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

#include "pybind11/stl.h"
#include "pybind11/stl_bind.h"
#include "python3/pybind_def.hpp"
#include "simulator/simulator.hpp"

#include "core/env.hpp"
#include "core/def/phare_config.hpp"


#include "amr/wrappers/hierarchy.hpp" // for HierarchyRestarter::getRestartFileFullPath



namespace py = pybind11;

PYBIND11_MAKE_OPAQUE(std::unordered_map<std::string, PHARE::env::Var*>);

namespace PHARE::pydata
{
auto pybind_version()
Expand Down Expand Up @@ -55,5 +57,21 @@ PYBIND11_MODULE(cpp_etc, m)
});

m.def("phare_build_config", []() { return PHARE::build_config(); });

m.def("phare_env_exists",
[](std::string const& s) { return Env::INSTANCE().vars.count(s) > 0; });
m.def("phare_env_val", [](std::string const& s) { return Env::INSTANCE()(s)(); });
py::class_<env::Var>(m, "phare_env_var")
.def_readonly("id", &env::Var::id)
.def_readonly("desc", &env::Var::desc)
.def_readonly("options", &env::Var::options)
.def_readonly("default", &env::Var::_default)
.def_readonly("results", &env::Var::results);

py::bind_map<std::unordered_map<std::string, env::Var*>>(m, "EnvVarMap");

m.def(
"phare_env_vars", []() -> auto& { return Env::INSTANCE().vars; },
py::return_value_policy::reference);
}
} // namespace PHARE::pydata
30 changes: 3 additions & 27 deletions src/simulator/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "phare_types.hpp"

#include "core/def.hpp"
#include "core/env.hpp"
#include "core/logger.hpp"
#include "core/utilities/types.hpp"
#include "core/utilities/mpi_utils.hpp"
Expand Down Expand Up @@ -116,32 +117,7 @@ class Simulator : public ISimulator
private:
auto find_model(std::string name);

auto static log_file_name()
{
// ".log" directory is not created here, but in python if PHARE_LOG != "NONE"
if (auto log = core::get_env("PHARE_LOG"))
{
if (log == "RANK_FILES")
return ".log/" + std::to_string(core::mpi::rank()) + ".out";


if (log == "DATETIME_FILES")
{
auto date_time = core::mpi::date_time();
auto rank = std::to_string(core::mpi::rank());
auto size = std::to_string(core::mpi::size());
return ".log/" + date_time + "_" + rank + "_of_" + size + ".out";
}

if (log != "NONE")
throw std::runtime_error(
"PHARE_LOG invalid type, valid keys are RANK_FILES/DATETIME_FILES/NONE");
}

return std::string{""}; // unused
}

std::ofstream log_out{log_file_name()};
std::ofstream log_out{Env::INSTANCE().PHARE_LOG("PHARE_LOG_FILE", "")};
std::streambuf* coutbuf = nullptr;
std::shared_ptr<PHARE::amr::Hierarchy> hierarchy_;
std::unique_ptr<Integrator> integrator_;
Expand Down Expand Up @@ -192,7 +168,7 @@ namespace
inline auto logging(std::ofstream& log_out)
{
std::streambuf* buf = nullptr;
if (auto log = core::get_env("PHARE_LOG"); log != "NONE")
if (auto log = Env::INSTANCE().PHARE_LOG(); log and log != "NONE")
{
buf = std::cout.rdbuf();
std::cout.rdbuf(log_out.rdbuf());
Expand Down
21 changes: 21 additions & 0 deletions tests/core/utilities/env/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required (VERSION 3.20.1)

project(test-phare-env)

set(SOURCES test_env.cpp)

add_executable(${PROJECT_NAME} ${SOURCES})

target_include_directories(${PROJECT_NAME} PRIVATE
${GTEST_INCLUDE_DIRS}
)

target_link_directories(${PROJECT_NAME} PUBLIC ${MPI_LIBRARY_PATH})

target_link_libraries(${PROJECT_NAME} PRIVATE
phare_core
MPI::MPI_C
${GTEST_LIBS}
)

add_phare_test(${PROJECT_NAME} ${CMAKE_CURRENT_BINARY_DIR})
Loading
Loading