Unverified Commit 23f6464c authored by William F Godoy's avatar William F Godoy Committed by GitHub
Browse files

Merge pull request #1059 from williamfgc/bindings

Python low-level bindings
parents 11049198 bf657383
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
pybind11_add_module(adios2py MODULE
  py11ADIOS.cpp
  py11IO.cpp
  py11Variable.cpp
  py11Attribute.cpp
  py11Engine.cpp
  py11Operator.cpp
  py11File.cpp
  py11glue.cpp
)
+47 −10
Original line number Diff line number Diff line
@@ -17,10 +17,11 @@ namespace adios2
namespace py11
{

ADIOS::ADIOS(const std::string configFile, MPI_Comm mpiComm,
#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>(
                              configFile, mpiComm, debugMode, "Python"))
: m_ADIOS(std::make_shared<adios2::core::ADIOS>(configFile, mpiComm, debugMode,
                                                "Python"))
{
}

@@ -28,25 +29,61 @@ ADIOS::ADIOS(MPI_Comm mpiComm, const bool debugMode)
: ADIOS("", mpiComm, debugMode)
{
}

ADIOS::ADIOS(const std::string configFile, const bool debugMode)
: ADIOS(configFile, MPI_COMM_SELF, debugMode)
#else
ADIOS::ADIOS(const std::string &configFile, const bool debugMode)
: m_ADIOS(
      std::make_shared<adios2::core::ADIOS>(configFile, debugMode, "Python"))
{
}

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)
{
    return IO(m_ADIOS->DeclareIO(name), m_DebugMode);
    CheckPointer("for io name " + name + ", in call to ADIOS::DeclareIO");
    return IO(&m_ADIOS->DeclareIO(name));
}

IO ADIOS::AtIO(const std::string name)
{
    return IO(m_ADIOS->AtIO(name), m_DebugMode);
    CheckPointer("for io name " + name + ", in call to ADIOS::AtIO");
    return IO(&m_ADIOS->AtIO(name));
}

Operator ADIOS::DefineOperator(const std::string name, const std::string type,
                               const Params &parameters)
{
    CheckPointer("for operator name " + name +
                 ", in call to ADIOS::DefineOperator");
    return Operator(&m_ADIOS->DefineOperator(name, type, parameters));
}

Operator ADIOS::InquireOperator(const std::string name)
{
    CheckPointer("for operator name " + name + ", in call to InquireOperator");
    return Operator(m_ADIOS->InquireOperator(name));
}

void ADIOS::FlushAll() { m_ADIOS->FlushAll(); }
void ADIOS::FlushAll()
{
    CheckPointer("in call to ADIOS::FlushAll");
    m_ADIOS->FlushAll();
}

// PRIVATE
void ADIOS::CheckPointer(const std::string hint)
{
    if (!m_ADIOS)
    {
        throw std::invalid_argument("ERROR: invalid ADIOS object, did you call "
                                    "any of the ADIOS explicit "
                                    "constructors?, " +
                                    hint + "\n");
    }
}

} // end namespace py11
} // end namespace adios2
+21 −8
Original line number Diff line number Diff line
@@ -8,10 +8,11 @@
 *      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"
#include "py11Operator.h"

#include <memory> //std::shared_ptr
#include <string>
@@ -28,24 +29,36 @@ class ADIOS
{

public:
    ADIOS(const std::string configFile, MPI_Comm mpiComm, const bool debugMode);
    ADIOS(MPI_Comm mpiComm, const bool debugMode);
    ADIOS(const std::string configFile, const bool debugMode);
#ifdef ADIOS2_HAVE_MPI
    ADIOS(const std::string &configFile, MPI_Comm comm,
          const bool debugMode = true);
    ADIOS(MPI_Comm comm, const bool debugMode = true);
#else
    ADIOS(const std::string &configFile, const bool debugMode = true);
    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);

    Operator DefineOperator(const std::string name, const std::string type,
                            const Params &parameters = Params());

    Operator InquireOperator(const std::string name);

    void FlushAll();

private:
    const bool m_DebugMode = true;
    std::shared_ptr<adios2::core::ADIOS> m_ADIOS;

    void CheckPointer(const std::string hint);
};

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

#endif /* BINDINGS_PYTHON_SOURCE_ADIOSPY_H_ */
#endif /* ADIOS2_BINDINGS_PYTHON_ADIOS_H_ */
+109 −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"
#include "py11types.h"

#include <string.h> //std::memcpy

#include "adios2/helper/adiosFunctions.h"

namespace adios2
{
namespace py11
{

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

Attribute::operator bool() const noexcept
{
    return (m_Attribute == nullptr) ? false : true;
}

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;
}

std::vector<std::string> Attribute::DataString()
{
    helper::CheckForNullptr(m_Attribute, "in call to Attribute::DataStrings");
    const std::string type = m_Attribute->m_Type;

    std::vector<std::string> data;

    if (type == "string")
    {
        const core::Attribute<std::string> *attribute =
            dynamic_cast<core::Attribute<std::string> *>(m_Attribute);

        data.reserve(attribute->m_Elements);
        if (attribute->m_IsSingleValue)
        {
            data.push_back(attribute->m_DataSingleValue);
        }
        else
        {
            data = attribute->m_DataArray;
        }
    }
    else
    {
        throw std::invalid_argument(
            "ERROR: data type for attribute " + m_Attribute->m_Name +
            " is not string, in call to Attribute::DataStrings\n");
    }
    return data;
}

pybind11::array Attribute::Data()
{
    helper::CheckForNullptr(m_Attribute, "in call to Attribute::Data");
    const std::string type = m_Attribute->m_Type;

    if (type == "compound")
    {
        // not supported
    }
#define declare_type(T)                                                        \
    else if (type == helper::GetType<T>())                                     \
    {                                                                          \
        pybind11::array pyArray(pybind11::dtype::of<T>(),                      \
                                m_Attribute->m_Elements);                      \
        if (m_Attribute->m_IsSingleValue)                                      \
        {                                                                      \
            const T value = dynamic_cast<core::Attribute<T> *>(m_Attribute)    \
                                ->m_DataSingleValue;                           \
            std::memcpy(const_cast<void *>(pyArray.data()), &value,            \
                        sizeof(T));                                            \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            const std::vector<T> &values =                                     \
                dynamic_cast<core::Attribute<T> *>(m_Attribute)->m_DataArray;  \
            std::memcpy(const_cast<void *>(pyArray.data()), values.data(),     \
                        sizeof(T) * m_Attribute->m_Elements);                  \
        }                                                                      \
        return pyArray;                                                        \
    }
    ADIOS2_FOREACH_NUMPY_ATTRIBUTE_TYPE_1ARG(declare_type)
#undef declare_type
    return pybind11::array();
}

} // end namespace py11
} // end namespace adios2
+52 −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_

#include <pybind11/numpy.h>

#include "adios2/core/AttributeBase.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();

    std::vector<std::string> DataString();

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

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

#endif /* BINDINGS_PYTHON_PYATTRIBUTE_H_ */
Loading