Skip to content
Snippets Groups Projects
Commit 6f2c6b2e authored by William F Godoy's avatar William F Godoy
Browse files

Implemented callbacks

Callback Signature classes extend Operator
Operators can be used per Variable, IO or Engine
parent a3a4ae4d
No related branches found
No related tags found
1 merge request!294Bp1read : Initial Reading Capabilities and latest API
......@@ -18,9 +18,27 @@
#include <adios2.h>
void UserCallBack(const unsigned int userID)
void UserCallBack(const float *data, const std::string &id,
const std::string &variableName, const std::string &type,
const std::vector<std::size_t> &count)
{
std::cout << "Hello callback UserCallBack " << userID << "\n";
std::cout << "Hello callback UserCallBack\n";
std::cout << "Variable name : " << variableName << "\n";
std::cout << "Variable type : " << type << "\n";
std::cout << "Variable id : " << id << "\n";
size_t totalSize = 1;
for (const size_t n : count)
{
totalSize *= n;
}
std::cout << "Variable data :\n { ";
for (size_t i = 0; i < totalSize; ++i)
{
std::cout << data[i] << ", ";
}
std::cout << " }\n";
}
int main(int argc, char *argv[])
......@@ -42,13 +60,17 @@ int main(int argc, char *argv[])
adios2::ADIOS adios(MPI_COMM_WORLD, adios2::DebugON);
adios2::Operator &callback = adios.DefineOperator(
"UserCallback",
std::function<void(const unsigned int)>(UserCallBack));
// std::function<void(const unsigned int)> myFunction =
// callback.GetCallback<void, const unsigned int>();
"Print Variable<float>",
std::function<void(const float *, const std::string &,
const std::string &, const std::string &,
const std::vector<std::size_t> &)>(
&UserCallBack));
// myFunction(1);
if (callback.m_Type == "Signature1")
{
callback.RunCallback1(myFloats.data(), "0", "bpFloats", "float",
std::vector<std::size_t>{Nx});
}
}
catch (std::invalid_argument &e)
{
......
......@@ -7,7 +7,6 @@ add_library(adios2
core/Attribute.cpp core/Attribute.tcc
core/AttributeBase.cpp
core/ADIOS.cpp
core/Callback.cpp
core/Engine.cpp
core/IO.cpp core/IO.tcc
core/Operator.cpp core/Operator.tcc
......
......@@ -21,7 +21,7 @@
#include "adios2/ADIOSMPI.h"
#include "adios2/helper/adiosFunctions.h" //InquireKey
// transforms
// compress
#ifdef ADIOS2_HAVE_BZIP2
#include "adios2/operator/compress/CompressBZip2.h"
#endif
......@@ -30,6 +30,9 @@
#include "adios2/operator/compress/CompressZfp.h"
#endif
// callback
#include "adios2/operator/callback/Signature1.h"
namespace adios2
{
......@@ -156,4 +159,22 @@ void ADIOS::CheckMPI() const
}
}
#define declare_type(T) \
Operator &ADIOS::DefineCallBack( \
const std::string name, \
const std::function<void(const T *, const std::string, \
const std::string, const std::string, \
const Dims &)> &function, \
const Params &parameters) \
{ \
std::shared_ptr<Operator> callbackOperator = \
std::make_shared<callback::Signature1<T>>(function, parameters, \
m_DebugMode); \
\
auto itPair = m_Operators.emplace(name, std::move(callbackOperator)); \
return *itPair.first->second; \
}
ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type
} // end namespace adios2
......@@ -12,7 +12,7 @@
#define ADIOS2_CORE_ADIOS_H_
/// \cond EXCLUDE_FROM_DOXYGEN
#include <adios2/core/Operator.h>
#include <functional>
#include <map>
#include <memory> //std::shared_ptr
#include <string>
......@@ -21,6 +21,7 @@
#include "adios2/ADIOSConfig.h"
#include "adios2/ADIOSMPICommOnly.h"
#include "adios2/ADIOSTypes.h"
#include "adios2/core/IO.h"
#include "adios2/core/Operator.h"
......@@ -148,6 +149,17 @@ private:
/** throws exception if m_MPIComm = MPI_COMM_NULL */
void CheckMPI() const;
/** define CallBack1 */
#define declare_type(T) \
Operator &DefineCallBack( \
const std::string name, \
const std::function<void(const T *, const std::string, \
const std::string, const std::string, \
const Dims &)> &function, \
const Params &parameters);
ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type
};
} // end namespace adios2
......
......@@ -28,14 +28,10 @@ Operator &ADIOS::DefineOperator(const std::string name,
{
throw std::invalid_argument("ERROR: operator with name " + name +
" previously defined, must be unique, in "
"call to DefineOperatorCallback\n");
"call to DefineOperator\n");
}
std::shared_ptr<Operator> callback = std::make_shared<Callback<R, Args...>>(
function, parameters, m_DebugMode);
auto itPair = m_Operators.emplace(name, std::move(callback));
return *itPair.first->second;
return DefineCallBack(name, function, parameters);
}
} // end namespace adios2
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Callback.cpp
*
* Created on: Oct 18, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include "Callback.h"
namespace adios2
{
} // end namespace adios2
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* CallBack.h
*
* Created on: Oct 18, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#ifndef ADIOS2_CORE_CALLBACK_H_
#define ADIOS2_CORE_CALLBACK_H_
#include <functional>
#include "adios2/core/Operator.h"
namespace adios2
{
template <class R, class... Args>
class Callback : public Operator
{
public:
std::function<R(Args...)> m_Function;
/**
* Unique constructor
* @param debugMode
*/
Callback<R, Args...>(std::function<R(Args...)> function,
const Params &parameters, const bool debugMode);
~Callback<R, Args...>() = default;
};
} // end namespace adios2
#include "Callback.inl"
#endif /* ADIOS2_OPERATOR_CALLBACK_CALLBACK_H_ */
......@@ -20,6 +20,17 @@ Operator::Operator(const std::string type, const Params &parameters,
{
}
#define declare_type(T) \
\
void Operator::RunCallback1(const T *arg1, const std::string &arg2, \
const std::string &arg3, \
const std::string &arg4, const Dims &arg5) \
{ \
CheckCallbackType("Callback1"); \
}
ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type
size_t Operator::BufferMaxSize(const size_t sizeIn) const
{
if (m_DebugMode)
......@@ -96,4 +107,15 @@ size_t Operator::DoBufferMaxSize(const void *dataIn, const Dims &dimensions,
return 0;
}
// PRIVATE
void Operator::CheckCallbackType(const std::string type) const
{
if (m_DebugMode && m_Type != type)
{
throw std::invalid_argument("ERROR: operator of type " + m_Type +
" doesn't match expected callback type " +
type + " arguments\n");
}
}
} // end namespace adios2
......@@ -19,16 +19,12 @@
#include <vector>
/// \endcond
#include "adios2/ADIOSMacros.h"
#include "adios2/ADIOSTypes.h"
namespace adios2
{
template <class R, class... Args>
class Callback; // forward declaration
/** Base class that defines data variable transformations implemented under
* adios2/transform */
class Operator
{
......@@ -46,9 +42,12 @@ public:
virtual ~Operator() = default;
template <class R, class... Args>
std::function<R(Args...)> GetCallback();
#define declare_type(T) \
virtual void RunCallback1(const T *, const std::string &, \
const std::string &, const std::string &, \
const Dims &);
ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type
/**
* Returns a conservative buffer size to hold input data for classes
* @param sizeIn size of input data to be compressed in bytes
......@@ -117,10 +116,11 @@ protected:
virtual size_t DoBufferMaxSize(const void *dataIn, const Dims &dimensions,
const std::string type,
const Params &parameters) const;
private:
void CheckCallbackType(const std::string type) const;
};
} // end namespace adios2
#include "Operator.inl"
#endif /* ADIOS2_CORE_OPERATOR_H_ */
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Operator.inl
*
* Created on: Oct 18, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#ifndef ADIOS2_CORE_OPERATOR_INL_
#define ADIOS2_CORE_OPERATOR_INL_
#ifndef ADIOS2_CORE_OPERATOR_H_
#error "Inline file should only be included from it's header, never on it's own"
#endif
#include "adios2/core/Callback.h"
namespace adios2
{
template <class R, class... Args>
std::function<R(Args...)> Operator::GetCallback()
{
if (m_DebugMode && m_Type != "Callback")
{
throw std::invalid_argument(
"ERROR: operator of type " + m_Type +
" doesn't support GetCallback, it's "
" only allowed for Callback Operator types\n");
}
Callback<R, Args...> *callback = dynamic_cast<Callback<R, Args...> *>(this);
return callback->m_Function;
}
} // end namespace adios2
#endif /* ADIOS2_CORE_OPERATOR_INL_ */
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Signature1.h
*
* Created on: Oct 19, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#ifndef ADIOS2_OPERATOR_CALLBACK_SIGNATURE1_H_
#define ADIOS2_OPERATOR_CALLBACK_SIGNATURE1_H_
#include "adios2/core/Operator.h"
namespace adios2
{
namespace callback
{
template <class T>
class Signature1 : public Operator
{
public:
Signature1<T>(const std::function<
void(const T *, const std::string &, const std::string &,
const std::string &, const Dims &)> &function,
const Params &parameters, const bool debugMode);
~Signature1<T>() = default;
void RunCallback1(const T *, const std::string &, const std::string &,
const std::string &, const Dims &) final;
private:
std::function<void(const T *, const std::string &, const std::string &,
const std::string &, const Dims &)>
m_Function;
};
} // end namespace callback
} // end namespace adios2
#include <adios2/operator/callback/Signature1.inl>
#endif /* ADIOS2_OPERATOR_CALLBACK_CALLBACK1_H_ */
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Callback1.inl
*
* Created on: Oct 19, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#ifndef ADIOS2_OPERATOR_CALLBACK_SIGNATURE1_INL_
#define ADIOS2_OPERATOR_CALLBACK_SIGNATURE1_INL_
#ifndef ADIOS2_OPERATOR_CALLBACK_SIGNATURE1_H_
#error "Inline file should only be included from it's header, never on it's own"
#endif
namespace adios2
{
namespace callback
{
template <class T>
Signature1<T>::Signature1(
const std::function<void(const T *, const std::string &,
const std::string &, const std::string &,
const Dims &)> &function,
const Params &parameters, const bool debugMode)
: Operator("Signature1", parameters, debugMode), m_Function(function)
{
}
template <class T>
void Signature1<T>::RunCallback1(const T *arg1, const std::string &arg2,
const std::string &arg3,
const std::string &arg4, const Dims &arg5)
{
if (m_Function)
{
m_Function(arg1, arg2, arg3, arg4, arg5);
}
else
{
throw std::runtime_error("ERROR: function Callback1 failed\n");
}
}
} // end namespace callback
} // end namespace adios2
#endif /* ADIOS2_OPERATOR_CALLBACK_CALLBACK1_INL_ */
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