From e48fd4adfcfeb0ce91831a8616ff804ce2fb8413 Mon Sep 17 00:00:00 2001 From: Chuck Atkins <chuck.atkins@kitware.com> Date: Tue, 13 Jun 2017 11:45:00 -0400 Subject: [PATCH] Make the adios2 python module compatible for both mpi and nompi (#14) --- bindings/CMakeLists.txt | 3 -- bindings/python/ADIOSPy.cpp | 6 ++- bindings/python/ADIOSPy.h | 2 + bindings/python/CMakeLists.txt | 15 ++++-- bindings/python/gluePyBind11.cpp | 18 +++++-- bindings/python/gluePyBind11_nompi.cpp | 70 -------------------------- 6 files changed, 31 insertions(+), 83 deletions(-) delete mode 100644 bindings/python/gluePyBind11_nompi.cpp diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 2aa4e17f6..1299ac394 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -1,6 +1,3 @@ if(ADIOS_USE_Python) - if(NOT BUILD_SHARED_LIBS) - message(ERROR "Python bindings are only supported for shared libraries") - endif() add_subdirectory(python) endif() diff --git a/bindings/python/ADIOSPy.cpp b/bindings/python/ADIOSPy.cpp index 6cdf457b0..2e16a24fc 100644 --- a/bindings/python/ADIOSPy.cpp +++ b/bindings/python/ADIOSPy.cpp @@ -20,7 +20,11 @@ ADIOSPy::ADIOSPy(MPI_Comm mpiComm, const bool debug) { } -ADIOSPy::ADIOSPy(const bool debug) : ADIOSPy(MPI_COMM_SELF, debug) {} +ADIOSPy::ADIOSPy(const bool debug) +: m_DebugMode(debug), m_ADIOS(debug) +{ +} + IOPy ADIOSPy::DeclareIO(const std::string name) { diff --git a/bindings/python/ADIOSPy.h b/bindings/python/ADIOSPy.h index d8c7d2281..d8cc68c9e 100644 --- a/bindings/python/ADIOSPy.h +++ b/bindings/python/ADIOSPy.h @@ -15,6 +15,8 @@ #include <string> /// \endcond +#include <adios2.h> + #include "IOPy.h" namespace adios diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 1c0627fb0..491ca2714 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -1,5 +1,6 @@ -find_package(PythonInterp REQUIRED) -find_package(PythonModule REQUIRED COMPONENTS mpi4py mpi4py/mpi4py.h) +if(NOT BUILD_SHARED_LIBS) + message(ERROR "Python bindings are only supported for shared libraries") +endif() pybind11_add_module(adios2py MODULE ADIOSPy.h @@ -16,12 +17,18 @@ pybind11_add_module(adios2py MODULE VariablePy.h VariablePy.cpp ) -target_link_libraries(adios2py PUBLIC adios2 PythonModule::mpi4py) +target_link_libraries(adios2py PRIVATE adios2) +if(ADIOS_USE_MPI) + find_package(PythonModule REQUIRED COMPONENTS mpi4py mpi4py/mpi4py.h) + target_link_libraries(adios2py PRIVATE PythonModule::mpi4py) +endif() +find_package(PythonInterp REQUIRED) set(python_package_dir ${CMAKE_INSTALL_LIBDIR}/python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}/site-packages) set_target_properties(adios2py PROPERTIES - PREFIX "" OUTPUT_NAME adios2 + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" LIBRARY_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir} RUNTIME_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir} ) diff --git a/bindings/python/gluePyBind11.cpp b/bindings/python/gluePyBind11.cpp index c7fff7f6a..6a22c6529 100644 --- a/bindings/python/gluePyBind11.cpp +++ b/bindings/python/gluePyBind11.cpp @@ -11,9 +11,12 @@ #include <stdexcept> #include <adios2.h> -#include <mpi4py/mpi4py.h> #include <pybind11/pybind11.h> +#ifdef ADIOS2_HAVE_MPI +#include <mpi4py/mpi4py.h> +#endif + #include "ADIOSPy.h" #include "EnginePy.h" #include "IOPy.h" @@ -21,6 +24,7 @@ #include "adiosPyFunctions.h" #include "adiosPyTypes.h" +#ifdef ADIOS2_HAVE_MPI adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode) { MPI_Comm *mpiCommPtr = PyMPIComm_Get(object.ptr()); @@ -39,14 +43,22 @@ adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode) } return adios::ADIOSPy(*mpiCommPtr, debugMode); } +#else +adios::ADIOSPy ADIOSPyInit(const bool debugMode) +{ + return adios::ADIOSPy(debugMode); +} +#endif PYBIND11_PLUGIN(adios2) { +#ifdef ADIOS2_HAVE_MPI if (import_mpi4py() < 0) { throw std::runtime_error( "ERROR: mpi4py not loaded correctly\n"); /* Python 2.X */ } +#endif pybind11::module m("adios2", "ADIOS2 Python bindings using pybind11"); m.attr("DebugON") = true; @@ -73,10 +85,6 @@ PYBIND11_PLUGIN(adios2) pybind11::arg("isConstantDims") = false) .def("GetVariable", &adios::IOPy::GetVariable, pybind11::return_value_policy::reference_internal) - // .def("Open", (adios::EnginePy (adios::IOPy::*)( - // const std::string &, const int, - // adios::pyObject &)) & - // adios::IOPy::Open) doesn't work .def("Open", (adios::EnginePy (adios::IOPy::*)(const std::string &, const int)) & adios::IOPy::Open); diff --git a/bindings/python/gluePyBind11_nompi.cpp b/bindings/python/gluePyBind11_nompi.cpp deleted file mode 100644 index a519a441c..000000000 --- a/bindings/python/gluePyBind11_nompi.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Distributed under the OSI-approved Apache License, Version 2.0. See - * accompanying file Copyright.txt for details. - * - * gluePyBind11_nompi.cpp - * - * Created on: Jun 12, 2017 - * Author: William F Godoy godoywf@ornl.gov - */ - -#include <stdexcept> - -#include <adios2.h> -#include <pybind11/pybind11.h> - -#include "ADIOSPy.h" -#include "EnginePy.h" -#include "IOPy.h" -#include "VariablePy.h" -#include "adiosPyFunctions.h" -#include "adiosPyTypes.h" - -adios::ADIOSPy ADIOSPyInit(const bool debugMode) -{ - return adios::ADIOSPy(debugMode); -} - -PYBIND11_PLUGIN(adios2) -{ - pybind11::module m("adios2", "ADIOS2 Python bindings using pybind11"); - m.attr("DebugON") = true; - m.attr("DebugOFF") = false; - m.attr("ConstantDims") = true; - m.attr("OpenModeWrite") = static_cast<int>(adios::OpenMode::Write); - m.attr("OpenModeRead") = static_cast<int>(adios::OpenMode::Read); - m.attr("OpenModeAppend") = static_cast<int>(adios::OpenMode::Append); - m.attr("OpenModeReadWrite") = static_cast<int>(adios::OpenMode::ReadWrite); - m.def("ADIOS", &ADIOSPyInit, "Function that creates an ADIOS class object"); - - pybind11::class_<adios::ADIOSPy>(m, "ADIOSPy") - .def("DeclareIO", &adios::ADIOSPy::DeclareIO); - - pybind11::class_<adios::IOPy>(m, "IOPy") - .def("SetEngine", &adios::IOPy::SetEngine) - .def("SetParameters", &adios::IOPy::SetParameters) - .def("AddTransport", &adios::IOPy::AddTransport) - .def("DefineVariable", &adios::IOPy::DefineVariable, - pybind11::return_value_policy::reference_internal, - pybind11::arg("name"), pybind11::arg("shape") = adios::pyList(), - pybind11::arg("start") = adios::pyList(), - pybind11::arg("count") = adios::pyList(), - pybind11::arg("isConstantDims") = false) - .def("GetVariable", &adios::IOPy::GetVariable, - pybind11::return_value_policy::reference_internal) - .def("Open", (adios::EnginePy (adios::IOPy::*)(const std::string &, - const int)) & - adios::IOPy::Open); - - pybind11::class_<adios::VariablePy>(m, "VariablePy") - .def("SetDimensions", &adios::VariablePy::SetDimensions); - - pybind11::class_<adios::EnginePy>(m, "EnginePy") - .def("Write", &adios::EnginePy::Write) - .def("Advance", &adios::EnginePy::Advance, - pybind11::arg("timeoutSeconds") = 0.) - .def("Close", &adios::EnginePy::Close, - pybind11::arg("transportIndex") = -1); - - return m.ptr(); -} -- GitLab