Commit ee44d591 authored by Godoy, William F's avatar Godoy, William F
Browse files

Refactoring Python bindings

PIMPL to match C++11
parent bbd44091
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ namespace adios2
namespace py11
{

#ifdef ADIOS2_HAVE_MPI
ADIOS::ADIOS(const std::string configFile, MPI_Comm mpiComm,
             const bool debugMode)
: m_DebugMode(debugMode), m_ADIOS(std::make_shared<adios2::core::ADIOS>(
@@ -28,13 +29,16 @@ ADIOS::ADIOS(MPI_Comm mpiComm, const bool debugMode)
: ADIOS("", mpiComm, debugMode)
{
}

#else
ADIOS::ADIOS(const std::string configFile, const bool debugMode)
: ADIOS(configFile, MPI_COMM_SELF, debugMode)
: ADIOS(configFile, debugMode)
{
}

ADIOS::ADIOS(const bool debugMode) : ADIOS("", MPI_COMM_SELF, debugMode) {}
ADIOS::ADIOS(const bool debugMode) : ADIOS("", debugMode) {}
#endif

ADIOS::operator bool() const noexcept { return m_ADIOS ? true : false; }

IO ADIOS::DeclareIO(const std::string name)
{
+9 −4
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@
 *      Author: William F Godoy godoywf@ornl.gov
 */

#ifndef ADIOS2_BINDINGS_PYTHON_SOURCE_ADIOSPY_H_
#define ADIOS2_BINDINGS_PYTHON_SOURCE_ADIOSPY_H_
#ifndef ADIOS2_BINDINGS_PYTHON_ADIOS_H_
#define ADIOS2_BINDINGS_PYTHON_ADIOS_H_

#include "py11IO.h"

@@ -28,13 +28,18 @@ class ADIOS
{

public:
#ifdef ADIOS2_HAVE_MPI
    ADIOS(const std::string configFile, MPI_Comm mpiComm, const bool debugMode);
    ADIOS(MPI_Comm mpiComm, const bool debugMode);
#else
    ADIOS(const std::string configFile, const bool debugMode);
    ADIOS(const bool debugMode);

#endif
    ~ADIOS() = default;

    /** object inspection true: valid object, false: invalid object */
    explicit operator bool() const noexcept;

    IO DeclareIO(const std::string name);
    IO AtIO(const std::string name);

@@ -48,4 +53,4 @@ private:
} // end namespace py11
} // end namespace adios2

#endif /* BINDINGS_PYTHON_SOURCE_ADIOSPY_H_ */
#endif /* ADIOS2_BINDINGS_PYTHON_ADIOS_H_ */
+42 −0
Original line number Diff line number Diff line
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * py11Variable.cpp :
 *
 *  Created on: Dec 11, 2018
 *      Author: William F Godoy godoywf@ornl.gov
 */

#include "py11Attribute.h"

namespace adios2
{
namespace py11
{

Attribute::Attribute(core::Attribute *attribute) : m_Attribute(attribute) {}

Attribute::operator bool() const noexcept {}

std::string Attribute::Name() const
{
    helper::CheckForNullptr(m_Attribute, "in call to Attribute::Name");
    return m_Attribute->m_Name;
}

std::string Attribute::Type() const
{
    helper::CheckForNullptr(m_Attribute, "in call to Attribute::Type");
    return m_Attribute->m_Type;
}

pybind11::array Attribute::Data()
{
    helper::CheckForNullptr(m_Attribute, "in call to Attribute::Data");
    // TODO
    return m_Attribute->m_Name;
}

} // end namespace py11
} // end namespace adios2
+46 −0
Original line number Diff line number Diff line
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * py11Variable.h :
 *
 *  Created on: Dec 11, 2018
 *      Author: William F Godoy godoywf@ornl.gov
 */

#ifndef ADIOS2_BINDINGS_PYTHON_ATTRIBUTE_H_
#define ADIOS2_BINDINGS_PYTHON_ATTRIBUTE_H_

namespace adios2
{
namespace py11
{

class IO;

class Attribute
{
    friend class IO;

public:
    Attribute() = default;

    ~Attribute() = default;

    explicit operator bool() const noexcept;

    std::string Name() const;

    std::string Type() const;

    pybind11::array Data();

private:
    Attribute(core::Attribute *attribute);
    core::AttributeBase *m_Attribute = nullptr;
};

} // end namespace py11
} // end namespace adios2

#endif /* BINDINGS_PYTHON_PYATTRIBUTE_H_ */
+119 −46
Original line number Diff line number Diff line
@@ -21,31 +21,50 @@ namespace adios2
namespace py11
{

Engine::Engine(core::IO &io, const std::string &name, const Mode openMode,
               MPI_Comm mpiComm)
: m_Engine(io.Open(name, openMode, mpiComm)), m_DebugMode(io.m_DebugMode)
Engine::Engine(core::Engine *engine, const bool debugMode)
: m_Engine(engine), m_DebugMode(debugMode)
{
}

Engine::operator bool() const noexcept
{
    if (m_Engine == nullptr)
    {
        return false;
    }

    return *m_Engine ? true : false;
}

StepStatus Engine::BeginStep(const StepMode mode, const float timeoutSeconds)
{
    return m_Engine.BeginStep(mode, timeoutSeconds);
    helper::CheckForNullptr(m_Engine, "in call to Engine::BeginStep");
    return m_Engine->BeginStep(mode, timeoutSeconds);
}

void Engine::Put(core::VariableBase *variable, const pybind11::array &array,
StepStatus Engine::BeginStep()
{
    helper::CheckForNullptr(m_Engine, "in call to Engine::BeginStep");
    return m_Engine->BeginStep();
}

void Engine::Put(Variable variable, const pybind11::array &array,
                 const Mode launch)
{
    helper::CheckForNullptr(variable,
                            "for variable, in call to Put numpy array");
    helper::CheckForNullptr(m_Engine, "in call to Engine::Put numpy array");
    helper::CheckForNullptr(variable.m_Variable,
                            "for variable, in call to Engine::Put numpy array");

    const std::string type = variable.Type();

    if (variable->m_Type == "compound")
    if (type == "compound")
    {
        // not supported
    }
#define declare_type(T)                                                        \
    else if (variable->m_Type == helper::GetType<T>())                         \
    else if (type == helper::GetType<T>())                                     \
    {                                                                          \
        m_Engine.Put(*dynamic_cast<core::Variable<T> *>(variable),             \
        m_Engine->Put(*dynamic_cast<core::Variable<T> *>(variable.m_Variable), \
                      reinterpret_cast<const T *>(array.data()), launch);      \
    }
    ADIOS2_FOREACH_NUMPY_TYPE_1ARG(declare_type)
@@ -54,7 +73,8 @@ void Engine::Put(core::VariableBase *variable, const pybind11::array &array,
    {
        if (m_DebugMode)
        {
            throw std::invalid_argument("ERROR: variable " + variable->m_Name +
            throw std::invalid_argument("ERROR: for variable " +
                                        variable.Name() +
                                        " numpy array type is not supported or "
                                        "is not memory contiguous "
                                        ", in call to Put\n");
@@ -62,31 +82,49 @@ void Engine::Put(core::VariableBase *variable, const pybind11::array &array,
    }
}

void Engine::Put(core::VariableBase *variable, const std::string &string)
void Engine::Put(Variable variable, const std::string &string)
{
    helper::CheckForNullptr(variable,
                            "for variable, in call to PutSync string");
    helper::CheckForNullptr(m_Engine,
                            "for engine, in call to Engine::Put string");
    helper::CheckForNullptr(variable.m_Variable,
                            "for variable, in call to Engine::Put string");

    m_Engine.Put(*dynamic_cast<core::Variable<std::string> *>(variable),
    if (variable.Type() != GetType<std::string>())
    {
        throw std::invalid_argument(
            "ERROR: variable " + variable.Name() +
            " is not of string type, in call to Engine::Put");
    }

    m_Engine->Put(*dynamic_cast<core::Variable<std::string> *>(variable),
                  string);
}

void Engine::PerformPuts() { m_Engine.PerformPuts(); }
void Engine::PerformPuts()
{
    helper::CheckForNullptr(m_Engine, "in call to PerformPuts");
    m_Engine->PerformPuts();
}

void Engine::Get(core::VariableBase *variable, pybind11::array &array,
                 const Mode launch)
void Engine::Get(Variable variable, pybind11::array &array, const Mode launch)
{
    helper::CheckForNullptr(variable,
                            "for variable, in call to GetSync a numpy array");
    helper::CheckForNullptr(m_Engine,
                            "for engine, in call to Engine::Get a numpy array");

    helper::CheckForNullptr(
        variable.m_Variable,
        "for variable, in call to Engine::Get a numpy array");

    const std::string type = variable.Type();

    if (variable->m_Type == "compound")
    if (type == "compound")
    {
        // not supported
    }
#define declare_type(T)                                                        \
    else if (variable->m_Type == helper::GetType<T>())                         \
    else if (type == helper::GetType<T>())                                     \
    {                                                                          \
        m_Engine.Get(*dynamic_cast<core::Variable<T> *>(variable),             \
        m_Engine->Get(*dynamic_cast<core::Variable<T> *>(variable.m_Variable), \
                      reinterpret_cast<T *>(const_cast<void *>(array.data())), \
                      launch);                                                 \
    }
@@ -97,8 +135,8 @@ void Engine::Get(core::VariableBase *variable, pybind11::array &array,
        if (m_DebugMode)
        {
            throw std::invalid_argument(
                "ERROR: in variable " + variable->m_Name + " of type " +
                variable->m_Type +
                "ERROR: in variable " + variable.Name() + " of type " +
                variable.Type() +
                ", numpy array type is 1) not supported, 2) a type mismatch or"
                "3) is not memory contiguous "
                ", in call to Get\n");
@@ -106,40 +144,75 @@ void Engine::Get(core::VariableBase *variable, pybind11::array &array,
    }
}

void Engine::Get(core::VariableBase *variable, std::string &string,
                 const Mode launch)
void Engine::Get(Variable variable, std::string &string, const Mode launch)
{
    helper::CheckForNullptr(variable,
                            "for variable, in call to GetSync a string");
    helper::CheckForNullptr(m_Engine,
                            "for engine, in call to Engine::Get a numpy array");

    helper::CheckForNullptr(variable.m_Variable,
                            "for variable, in call to Engine::Get a string");

    const std::string type = variable.Type();

    if (variable->m_Type == helper::GetType<std::string>())
    if (type == helper::GetType<std::string>())
    {
        m_Engine.Get(*dynamic_cast<core::Variable<std::string> *>(variable),
        m_Engine->Get(
            *dynamic_cast<core::Variable<std::string> *>(variable.m_Variable),
            string, launch);
    }
    else
    {
        if (m_DebugMode)
        {
            throw std::invalid_argument("ERROR: variable " + variable->m_Name +
                                        " of type " + variable->m_Type +
                                        " is not string, in call to Get");
            throw std::invalid_argument(
                "ERROR: variable " + variable.Name() + " of type " +
                variable.Type() + " is not string, in call to Engine::Get");
        }
    }
}

void Engine::PerformGets() { m_Engine.PerformGets(); }
void Engine::PerformGets()
{
    helper::CheckForNullptr(m_Engine, "in call to Engine::PerformGets");
    m_Engine->PerformGets();
}

void Engine::EndStep() { m_Engine.EndStep(); }
void Engine::EndStep()
{
    helper::CheckForNullptr(m_Engine, "for engine, in call to Engine::EndStep");
    m_Engine->EndStep();
}

void Engine::Flush(const int transportIndex) { m_Engine.Flush(transportIndex); }
void Engine::Flush(const int transportIndex)
{
    helper::CheckForNullptr(m_Engine, "for engine, in call to Engine::Flush");
    m_Engine->Flush(transportIndex);
}

void Engine::Close(const int transportIndex) { m_Engine.Close(transportIndex); }
void Engine::Close(const int transportIndex)
{
    helper::CheckForNullptr(m_Engine, "for engine, in call to Engine::Close");
    m_Engine->Close(transportIndex);
}

size_t Engine::CurrentStep() const { return m_Engine.CurrentStep(); }
size_t Engine::CurrentStep() const
{
    helper::CheckForNullptr(m_Engine,
                            "for engine, in call to Engine::CurrentStep");
    return m_Engine->CurrentStep();
}

std::string Engine::Name() const noexcept { return m_Engine.m_Name; }
std::string Engine::Type() const noexcept { return m_Engine.m_EngineType; }
std::string Engine::Name() const
{
    helper::CheckForNullptr(m_Engine, "for engine, in call to Engine::Name");
    return m_Engine->m_Name;
}

std::string Engine::Type() const
{
    helper::CheckForNullptr(m_Engine, "for engine, in call to Engine::Type");
    return m_Engine->m_EngineType;
}

} // end namespace py11
} // end namespace adios2
Loading