From 49cb71f0cd446edc932314c423db57bd2567d0b5 Mon Sep 17 00:00:00 2001 From: William F Godoy <williamfgc@yahoo.com> Date: Mon, 12 Jun 2017 17:25:41 -0400 Subject: [PATCH] Added gluePyBind11_nompi Removed IO Open that takes a new communicator (didn't work) --- bindings/python/ADIOSPy.cpp | 2 + bindings/python/ADIOSPy.h | 3 +- bindings/python/EnginePy.cpp | 4 -- bindings/python/IOPy.cpp | 26 ------- bindings/python/IOPy.h | 7 +- bindings/python/gluePyBind11.cpp | 15 ++-- bindings/python/gluePyBind11_nompi.cpp | 70 +++++++++++++++++++ .../hello/bpTimeWriter/helloBPTimeWriter.py | 11 ++- examples/hello/bpWriter/helloBPWriter.py | 8 +-- 9 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 bindings/python/gluePyBind11_nompi.cpp diff --git a/bindings/python/ADIOSPy.cpp b/bindings/python/ADIOSPy.cpp index f258de2bf..6cdf457b0 100644 --- a/bindings/python/ADIOSPy.cpp +++ b/bindings/python/ADIOSPy.cpp @@ -20,6 +20,8 @@ ADIOSPy::ADIOSPy(MPI_Comm mpiComm, const bool debug) { } +ADIOSPy::ADIOSPy(const bool debug) : ADIOSPy(MPI_COMM_SELF, debug) {} + IOPy ADIOSPy::DeclareIO(const std::string name) { return IOPy(m_ADIOS.DeclareIO(name), m_DebugMode); diff --git a/bindings/python/ADIOSPy.h b/bindings/python/ADIOSPy.h index f390697d5..d8c7d2281 100644 --- a/bindings/python/ADIOSPy.h +++ b/bindings/python/ADIOSPy.h @@ -24,7 +24,8 @@ class ADIOSPy { public: - ADIOSPy(MPI_Comm mpiComm, const bool debug = false); + ADIOSPy(MPI_Comm mpiComm, const bool debug); + ADIOSPy(const bool debug); ~ADIOSPy() = default; IOPy DeclareIO(const std::string name); diff --git a/bindings/python/EnginePy.cpp b/bindings/python/EnginePy.cpp index e703f61d1..7994a7f8e 100644 --- a/bindings/python/EnginePy.cpp +++ b/bindings/python/EnginePy.cpp @@ -10,12 +10,8 @@ #include "EnginePy.h" -#include "adios2/ADIOSMacros.h" - #include "adiosPyFunctions.h" -#include <iostream> - namespace adios { diff --git a/bindings/python/IOPy.cpp b/bindings/python/IOPy.cpp index e1bfc599d..bc26e1a43 100644 --- a/bindings/python/IOPy.cpp +++ b/bindings/python/IOPy.cpp @@ -10,10 +10,6 @@ #include "IOPy.h" -#include <mpi4py/mpi4py.h> - -#include "adiosPyFunctions.h" //PyObjectToMPIComm - namespace adios { @@ -71,28 +67,6 @@ VariablePy &IOPy::GetVariable(const std::string &name) return itVariable->second; } -EnginePy IOPy::Open(const std::string &name, const int openMode, - adios::pyObject &object) -{ - MPI_Comm *mpiCommPtr = PyMPIComm_Get(object.ptr()); - - if (import_mpi4py() < 0) - { - throw std::logic_error("ERROR: could not import mpi4py " - "communicator, in call to ADIOS " - "constructor\n"); - } - - if (mpiCommPtr == nullptr) - { - throw std::runtime_error("ERROR: mpi4py communicator is null, in call " - "to ADIOS constructor\n"); - } - - return EnginePy(m_IO, name, static_cast<adios::OpenMode>(openMode), - *mpiCommPtr); -} - EnginePy IOPy::Open(const std::string &name, const int openMode) { return EnginePy(m_IO, name, static_cast<adios::OpenMode>(openMode), diff --git a/bindings/python/IOPy.h b/bindings/python/IOPy.h index 8838d6a63..a5fff653c 100644 --- a/bindings/python/IOPy.h +++ b/bindings/python/IOPy.h @@ -44,12 +44,13 @@ public: VariablePy &GetVariable(const std::string &name); - EnginePy Open(const std::string &name, const int openMode, - adios::pyObject &object); - EnginePy Open(const std::string &name, const int openMode); private: + /** + * Extra map needed as Variables are not created in ADIOS at + * DefineVariable, but until Write when type is known from numpy + */ std::map<std::string, VariablePy> m_Variables; }; diff --git a/bindings/python/gluePyBind11.cpp b/bindings/python/gluePyBind11.cpp index 14ab57d1d..c7fff7f6a 100644 --- a/bindings/python/gluePyBind11.cpp +++ b/bindings/python/gluePyBind11.cpp @@ -49,14 +49,13 @@ PYBIND11_PLUGIN(adios2) } pybind11::module m("adios2", "ADIOS2 Python bindings using pybind11"); - m.attr("adiosDebugON") = true; - m.attr("adiosDebugOFF") = false; - m.attr("adiosConstantDims") = true; - m.attr("adiosOpenModeWrite") = static_cast<int>(adios::OpenMode::Write); - m.attr("adiosOpenModeRead") = static_cast<int>(adios::OpenMode::Read); - m.attr("adiosOpenModeAppend") = static_cast<int>(adios::OpenMode::Append); - m.attr("adiosOpenModeReadWrite") = - static_cast<int>(adios::OpenMode::ReadWrite); + 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") diff --git a/bindings/python/gluePyBind11_nompi.cpp b/bindings/python/gluePyBind11_nompi.cpp new file mode 100644 index 000000000..a519a441c --- /dev/null +++ b/bindings/python/gluePyBind11_nompi.cpp @@ -0,0 +1,70 @@ +/* + * 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(); +} diff --git a/examples/hello/bpTimeWriter/helloBPTimeWriter.py b/examples/hello/bpTimeWriter/helloBPTimeWriter.py index dc5c81f57..c6cbb6acf 100644 --- a/examples/hello/bpTimeWriter/helloBPTimeWriter.py +++ b/examples/hello/bpTimeWriter/helloBPTimeWriter.py @@ -1,14 +1,13 @@ -# clang-format off # # Distributed under the OSI-approved Apache License, Version 2.0. See # accompanying file Copyright.txt for details. # # test_hello.py # Created on: Feb 2, 2017 -# Author: wfg +# Author: William F Godoy godoywf@ornl.gov from mpi4py import MPI -import adios2 +import adios2 import numpy as np @@ -22,18 +21,18 @@ myArray = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) nx = myArray.size # ADIOS -adios = adios2.ADIOS(comm, adios2.adiosDebugON) +adios = adios2.ADIOS(comm, adios2.DebugON) # IO bpIO = adios.DeclareIO("BPN2N") # Variables bpArray = bpIO.DefineVariable("bpArray", [size * nx], [rank * nx], [nx], - adios2.adiosConstantDims) + adios2.ConstantDims) bpTimeStep = bpIO.DefineVariable("bpTimeStep") # Engine -bpFileWriter = bpIO.Open("myArray.bp", adios2.adiosOpenModeWrite) +bpFileWriter = bpIO.Open("myArray.bp", adios2.OpenModeWrite) # Doesn't work: bpFileWriter = bpIO.Open("myArray.bp", adiosOpenModeWrite, # MPI.COMM_WORLD) diff --git a/examples/hello/bpWriter/helloBPWriter.py b/examples/hello/bpWriter/helloBPWriter.py index f8c700954..780507684 100644 --- a/examples/hello/bpWriter/helloBPWriter.py +++ b/examples/hello/bpWriter/helloBPWriter.py @@ -5,7 +5,7 @@ # # test_hello.py # Created on: Feb 2, 2017 -# Author: wfg +# Author: William F Godoy godoywf@ornl.gov from mpi4py import MPI import adios2 @@ -15,17 +15,17 @@ import numpy as np myArray = np.array([1., 2., 3., 4., 5., 6.]) # ADIOS -adios = adios2.ADIOS(MPI.COMM_WORLD, adios2.adiosDebugON) +adios = adios2.ADIOS(MPI.COMM_WORLD, adios2.DebugON) # IO bpIO = adios.DeclareIO("BPN2N") # Variable ioArray = bpIO.DefineVariable( - "bpArray", [myArray.size], [0], [myArray.size], adios2.adiosConstantDims) + "bpArray", [myArray.size], [0], [myArray.size], adios2.ConstantDims) # Engine -bpFileWriter = bpIO.Open("myArray.bp", adios2.adiosOpenModeWrite) +bpFileWriter = bpIO.Open("myArray.bp", adios2.OpenModeWrite) # bpFileWriter = bpIO.Open("myArray.bp", adiosOpenModeWrite, # MPI.COMM_WORLD) //doesn't work bpFileWriter.Write(ioArray, myArray) -- GitLab