Skip to content
Snippets Groups Projects
Commit 7c0b8236 authored by akuesters's avatar akuesters Committed by Benjamin Cumming
Browse files

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
```
parent 1c89fbbd
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
#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
#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
......@@ -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
......
......@@ -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
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