Skip to content
Snippets Groups Projects
Commit e48fd4ad authored by Atkins, Charles Vernon's avatar Atkins, Charles Vernon Committed by williamfgc
Browse files

Make the adios2 python module compatible for both mpi and nompi (#14)

parent 49cb71f0
No related branches found
No related tags found
1 merge request!126Python Bindings and their required internal code changes
if(ADIOS_USE_Python) if(ADIOS_USE_Python)
if(NOT BUILD_SHARED_LIBS)
message(ERROR "Python bindings are only supported for shared libraries")
endif()
add_subdirectory(python) add_subdirectory(python)
endif() endif()
...@@ -20,7 +20,11 @@ ADIOSPy::ADIOSPy(MPI_Comm mpiComm, const bool debug) ...@@ -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) IOPy ADIOSPy::DeclareIO(const std::string name)
{ {
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include <string> #include <string>
/// \endcond /// \endcond
#include <adios2.h>
#include "IOPy.h" #include "IOPy.h"
namespace adios namespace adios
......
find_package(PythonInterp REQUIRED) if(NOT BUILD_SHARED_LIBS)
find_package(PythonModule REQUIRED COMPONENTS mpi4py mpi4py/mpi4py.h) message(ERROR "Python bindings are only supported for shared libraries")
endif()
pybind11_add_module(adios2py MODULE pybind11_add_module(adios2py MODULE
ADIOSPy.h ADIOSPy.h
...@@ -16,12 +17,18 @@ pybind11_add_module(adios2py MODULE ...@@ -16,12 +17,18 @@ pybind11_add_module(adios2py MODULE
VariablePy.h VariablePy.h
VariablePy.cpp 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(python_package_dir ${CMAKE_INSTALL_LIBDIR}/python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}/site-packages)
set_target_properties(adios2py PROPERTIES set_target_properties(adios2py PROPERTIES
PREFIX ""
OUTPUT_NAME adios2 OUTPUT_NAME adios2
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
LIBRARY_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir} LIBRARY_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir}
RUNTIME_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir} RUNTIME_OUTPUT_DIRECTORY ${ADIOS_BINARY_DIR}/${python_package_dir}
) )
......
...@@ -11,9 +11,12 @@ ...@@ -11,9 +11,12 @@
#include <stdexcept> #include <stdexcept>
#include <adios2.h> #include <adios2.h>
#include <mpi4py/mpi4py.h>
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#ifdef ADIOS2_HAVE_MPI
#include <mpi4py/mpi4py.h>
#endif
#include "ADIOSPy.h" #include "ADIOSPy.h"
#include "EnginePy.h" #include "EnginePy.h"
#include "IOPy.h" #include "IOPy.h"
...@@ -21,6 +24,7 @@ ...@@ -21,6 +24,7 @@
#include "adiosPyFunctions.h" #include "adiosPyFunctions.h"
#include "adiosPyTypes.h" #include "adiosPyTypes.h"
#ifdef ADIOS2_HAVE_MPI
adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode) adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode)
{ {
MPI_Comm *mpiCommPtr = PyMPIComm_Get(object.ptr()); MPI_Comm *mpiCommPtr = PyMPIComm_Get(object.ptr());
...@@ -39,14 +43,22 @@ adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode) ...@@ -39,14 +43,22 @@ adios::ADIOSPy ADIOSPyInit(adios::pyObject &object, const bool debugMode)
} }
return adios::ADIOSPy(*mpiCommPtr, debugMode); return adios::ADIOSPy(*mpiCommPtr, debugMode);
} }
#else
adios::ADIOSPy ADIOSPyInit(const bool debugMode)
{
return adios::ADIOSPy(debugMode);
}
#endif
PYBIND11_PLUGIN(adios2) PYBIND11_PLUGIN(adios2)
{ {
#ifdef ADIOS2_HAVE_MPI
if (import_mpi4py() < 0) if (import_mpi4py() < 0)
{ {
throw std::runtime_error( throw std::runtime_error(
"ERROR: mpi4py not loaded correctly\n"); /* Python 2.X */ "ERROR: mpi4py not loaded correctly\n"); /* Python 2.X */
} }
#endif
pybind11::module m("adios2", "ADIOS2 Python bindings using pybind11"); pybind11::module m("adios2", "ADIOS2 Python bindings using pybind11");
m.attr("DebugON") = true; m.attr("DebugON") = true;
...@@ -73,10 +85,6 @@ PYBIND11_PLUGIN(adios2) ...@@ -73,10 +85,6 @@ PYBIND11_PLUGIN(adios2)
pybind11::arg("isConstantDims") = false) pybind11::arg("isConstantDims") = false)
.def("GetVariable", &adios::IOPy::GetVariable, .def("GetVariable", &adios::IOPy::GetVariable,
pybind11::return_value_policy::reference_internal) 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 &, .def("Open", (adios::EnginePy (adios::IOPy::*)(const std::string &,
const int)) & const int)) &
adios::IOPy::Open); adios::IOPy::Open);
......
/*
* 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();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment