From 7c0b823641e3aec3dd54ade84b4d9149fe0c1dbd Mon Sep 17 00:00:00 2001 From: akuesters <42005107+akuesters@users.noreply.github.com> Date: Fri, 1 Mar 2019 15:13:47 +0100 Subject: [PATCH] Python feature: config file to present Arbor's support on mpi, gpu and version (#708) Fixes #700 Usage in python as: ``` >>>import arbor >>>arbor.config() {'mpi': True, 'mpi4py': True, 'gpu': False, 'version': '0.1.1-dev'} >>>d = arbor.config() >>>arbor.print_config(d) Arbor's configuration: mpi : True mpi4py : True gpu : False version: 0.1.1-dev ``` --- python/CMakeLists.txt | 1 + python/config.cpp | 12 +++++++++++ python/config.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++ python/pyarb.cpp | 3 ++- python/strings.cpp | 23 +++++++++++---------- 5 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 python/config.cpp create mode 100644 python/config.hpp diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 1ab9e95c..7e5b02f3 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -16,6 +16,7 @@ add_subdirectory(pybind11) # The Python library. MODULE will make a Python-exclusive model. add_library(pyarb MODULE + config.cpp context.cpp exception.cpp mpi.cpp diff --git a/python/config.cpp b/python/config.cpp new file mode 100644 index 00000000..50fc788e --- /dev/null +++ b/python/config.cpp @@ -0,0 +1,12 @@ +#include <pybind11/pybind11.h> + +#include "config.hpp" + +namespace pyarb { + +void register_config(pybind11::module &m) { + + m.def("config", &config, "Get Arbor's configuration.") + .def("print_config", [](const pybind11::dict& d){return print_config(d);}, "Print Arbor's configuration."); +} +} // namespace pyarb diff --git a/python/config.hpp b/python/config.hpp new file mode 100644 index 00000000..57cf0858 --- /dev/null +++ b/python/config.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include <arbor/version.hpp> + +#include <sstream> +#include <iomanip> +#include <ios> +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +namespace pyarb { + +// Create and return a dictionary +pybind11::dict config() { + pybind11::dict dict; + #ifdef ARB_MPI_ENABLED + dict[pybind11::str("mpi")] = pybind11::bool_(true); + #else + dict[pybind11::str("mpi")] = pybind11::bool_(false); + #endif + #ifdef ARB_WITH_MPI4PY + dict[pybind11::str("mpi4py")] = pybind11::bool_(true); + #else + dict[pybind11::str("mpi4py")] = pybind11::bool_(false); + #endif + #ifdef ARB_WITH_GPU + dict[pybind11::str("gpu")] = pybind11::bool_(true); + #else + dict[pybind11::str("gpu")] = pybind11::bool_(false); + #endif + dict[pybind11::str("version")] = pybind11::str(ARB_VERSION); + return dict; +} + +void print_config(const pybind11::dict &d) { + std::stringstream s; + s << "Arbor's configuration:" << std::endl; + + for (auto x: d) { + s << " " + << std::left << std::setw(7) << x.first << ": " + << std::right << std::setw(10) << x.second << "\n"; + } + + pybind11::print(s.str()); +} +} // namespace pyarb diff --git a/python/pyarb.cpp b/python/pyarb.cpp index 8f3be8f0..a4a897c5 100644 --- a/python/pyarb.cpp +++ b/python/pyarb.cpp @@ -6,18 +6,19 @@ // types and functions to be exposed to Python. namespace pyarb { +void register_config(pybind11::module& m); void register_contexts(pybind11::module& m); void register_queryenvvars(pybind11::module& m); #ifdef ARB_MPI_ENABLED void register_mpi(pybind11::module& m); #endif - } PYBIND11_MODULE(arbor, m) { m.doc() = "arbor: Python bindings for Arbor."; m.attr("__version__") = ARB_VERSION; + pyarb::register_config(m); pyarb::register_contexts(m); pyarb::register_queryenvvars(m); #ifdef ARB_MPI_ENABLED diff --git a/python/strings.cpp b/python/strings.cpp index abcb0649..f61e7c76 100644 --- a/python/strings.cpp +++ b/python/strings.cpp @@ -7,6 +7,18 @@ namespace pyarb { +std::string context_string(const arb::context& c) { + std::stringstream s; + const bool gpu = arb::has_gpu(c); + const bool mpi = arb::has_mpi(c); + s << "<context: threads " << arb::num_threads(c) + << ", gpu " << (gpu? "yes": "None") + << ", distributed " << (mpi? "MPI": "Local") + << " ranks " << arb::num_ranks(c) + << ">"; + return s.str(); +} + std::string proc_allocation_string(const arb::proc_allocation& a) { std::stringstream s; s << "<hardware resource allocation: threads " << a.num_threads << ", gpu "; @@ -20,15 +32,4 @@ std::string proc_allocation_string(const arb::proc_allocation& a) { return s.str(); } -std::string context_string(const arb::context& c) { - std::stringstream s; - const bool gpu = arb::has_gpu(c); - const bool mpi = arb::has_mpi(c); - s << "<context: threads " << arb::num_threads(c) - << ", gpu " << (gpu? "yes": "None") - << ", distributed " << (mpi? "MPI": "Local") - << " ranks " << arb::num_ranks(c) - << ">"; - return s.str(); -} } // namespace pyarb -- GitLab