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

Added operator/callback/Signature2

parent 5d141ed2
No related branches found
No related tags found
1 merge request!294Bp1read : Initial Reading Capabilities and latest API
...@@ -4,12 +4,17 @@ ...@@ -4,12 +4,17 @@
#------------------------------------------------------------------------------# #------------------------------------------------------------------------------#
if(ADIOS2_HAVE_MPI) if(ADIOS2_HAVE_MPI)
add_executable(hello_Callback helloCallback.cpp) add_executable(hello_Callback1 helloCallback1.cpp)
target_include_directories(hello_Callback PRIVATE ${MPI_C_INCLUDE_PATH}) target_include_directories(hello_Callback1 PRIVATE ${MPI_C_INCLUDE_PATH})
target_link_libraries(hello_Callback ${MPI_C_LIBRARIES}) target_link_libraries(hello_Callback1 ${MPI_C_LIBRARIES})
add_executable(hello_Callback2 helloCallback2.cpp)
target_include_directories(hello_Callback2 PRIVATE ${MPI_C_INCLUDE_PATH})
target_link_libraries(hello_Callback2 ${MPI_C_LIBRARIES})
else() else()
#add_executable(hello_Callback helloBPZfpWrapper_nompi.cpp) #add_executable(hello_Callback helloBPZfpWrapper_nompi.cpp)
endif() endif()
target_link_libraries(hello_Callback adios2) target_link_libraries(hello_Callback1 adios2)
target_link_libraries(hello_Callback2 adios2)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Distributed under the OSI-approved Apache License, Version 2.0. See * Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details. * accompanying file Copyright.txt for details.
* *
* helloCallback.cpp * helloCallback1.cpp
* *
* Created on: Oct 18, 2017 * Created on: Oct 18, 2017
* Author: William F Godoy godoywf@ornl.gov * Author: William F Godoy godoywf@ornl.gov
...@@ -27,11 +27,8 @@ void UserCallBack(const float *data, const std::string &id, ...@@ -27,11 +27,8 @@ void UserCallBack(const float *data, const std::string &id,
std::cout << "Variable type : " << type << "\n"; std::cout << "Variable type : " << type << "\n";
std::cout << "Variable id : " << id << "\n"; std::cout << "Variable id : " << id << "\n";
size_t totalSize = 1; const size_t totalSize = std::accumulate(count.begin(), count.end(), 1,
for (const size_t n : count) std::multiplies<std::size_t>());
{
totalSize *= n;
}
std::cout << "Variable data :\n { "; std::cout << "Variable data :\n { ";
for (size_t i = 0; i < totalSize; ++i) for (size_t i = 0; i < totalSize; ++i)
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* helloCallback2.cpp
*
* Created on: Oct 20, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include <mpi.h>
#include <cstdint> //std::int32_t
#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <numeric> //std::iota
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>
#include <adios2.h>
void UserCallBack(void *data, const std::string &doid, const std::string &var,
const std::string &dtype,
const std::vector<std::size_t> &varshape)
{
std::cout << "data object ID = " << doid << "\n";
std::cout << "variable name = " << var << "\n";
std::cout << "data type = " << dtype << "\n";
std::size_t varsize = std::accumulate(varshape.begin(), varshape.end(), 1,
std::multiplies<std::size_t>());
for (unsigned int i = 0; i < varsize; ++i)
{
std::cout << (reinterpret_cast<float *>(data))[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/** Application variable uints from 0 to 100 */
std::vector<float> myFloats(100);
std::iota(myFloats.begin(), myFloats.end(), 0.f);
const std::size_t Nx = myFloats.size();
const std::size_t inputBytes = Nx * sizeof(float);
try
{
/** ADIOS class factory of IO class objects, DebugON is recommended */
adios2::ADIOS adios(MPI_COMM_WORLD, adios2::DebugON);
adios2::Operator &callback = adios.DefineOperator(
"Print Variable<float>",
std::function<void(void *, const std::string &, const std::string &,
const std::string &,
const std::vector<std::size_t> &)>(
&UserCallBack));
if (callback.m_Type == "Signature2")
{
callback.RunCallback2(myFloats.data(), "0", "bpFloats", "float",
std::vector<std::size_t>{Nx});
}
}
catch (std::invalid_argument &e)
{
std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::ios_base::failure &e)
{
std::cout
<< "IO System base failure exception, STOPPING PROGRAM from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::exception &e)
{
std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
std::cout << e.what() << "\n";
}
MPI_Finalize();
return 0;
}
...@@ -16,10 +16,9 @@ ...@@ -16,10 +16,9 @@
#include <adios2.h> #include <adios2.h>
// matches Signature1 in ADIOS2 // matches Signature2 in ADIOS2
template <class T> void UserCallBack(void *data, const std::string &doid, const std::string &var,
void UserCallBack(const T *data, const std::string &doid, const std::string &dtype,
const std::string &var, const std::string &dtype,
const std::vector<std::size_t> &varshape) const std::vector<std::size_t> &varshape)
{ {
std::cout << "data object ID = " << doid << "\n"; std::cout << "data object ID = " << doid << "\n";
...@@ -50,21 +49,20 @@ int main(int argc, char *argv[]) ...@@ -50,21 +49,20 @@ int main(int argc, char *argv[])
adios2::Operator &callbackFloat = adios.DefineOperator( adios2::Operator &callbackFloat = adios.DefineOperator(
"Print float Variable callback", "Print float Variable callback",
std::function<void(const float *, const std::string &, std::function<void(void *, const std::string &, const std::string &,
const std::string &, const std::string &, const std::string &, const adios2::Dims &)>(
const adios2::Dims &)>(UserCallBack<float>)); UserCallBack));
adios2::IO &dataManIO = adios.DeclareIO("WAN"); adios2::IO &dataManIO = adios.DeclareIO("WAN");
dataManIO.SetEngine("DataManReader"); dataManIO.SetEngine("DataManReader");
dataManIO.SetParameters({{"real_time", "yes"}, dataManIO.SetParameters({{"real_time", "yes"},
{"method_type", "stream"}, {"method_type", "stream"},
{"method", "dump"}}); {"method", "dump"}});
dataManIO.AddOperator(callbackFloat); // progated to all Engines
adios2::Engine &dataManReader = adios2::Engine &dataManReader =
dataManIO.Open("myDoubles.bp", adios2::Mode::Read); dataManIO.Open("myDoubles.bp", adios2::Mode::Read);
// dataManReader.SetCallBack(UserCallBack);
for (unsigned int i = 0; i < 3; ++i) for (unsigned int i = 0; i < 3; ++i)
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
......
...@@ -13,7 +13,10 @@ add_library(adios2 ...@@ -13,7 +13,10 @@ add_library(adios2
core/Variable.cpp core/Variable.tcc core/Variable.cpp core/Variable.tcc
core/VariableBase.cpp core/VariableBase.cpp
core/VariableCompound.cpp core/VariableCompound.tcc core/VariableCompound.cpp core/VariableCompound.tcc
#operator callback
operator/callback/Signature2.cpp
#helper #helper
helper/adiosDynamicBinder.h helper/adiosDynamicBinder.cpp helper/adiosDynamicBinder.h helper/adiosDynamicBinder.cpp
helper/adiosMath.cpp helper/adiosMath.cpp
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
// callback // callback
#include "adios2/operator/callback/Signature1.h" #include "adios2/operator/callback/Signature1.h"
#include "adios2/operator/callback/Signature2.h"
namespace adios2 namespace adios2
{ {
...@@ -174,7 +175,22 @@ void ADIOS::CheckMPI() const ...@@ -174,7 +175,22 @@ void ADIOS::CheckMPI() const
auto itPair = m_Operators.emplace(name, std::move(callbackOperator)); \ auto itPair = m_Operators.emplace(name, std::move(callbackOperator)); \
return *itPair.first->second; \ return *itPair.first->second; \
} }
ADIOS2_FOREACH_TYPE_1ARG(declare_type) ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type #undef declare_type
Operator &ADIOS::DefineCallBack(
const std::string name,
const std::function<void(void *, const std::string, const std::string,
const std::string, const Dims &)> &function,
const Params &parameters)
{
std::shared_ptr<Operator> callbackOperator =
std::make_shared<callback::Signature2>(function, parameters,
m_DebugMode);
auto itPair = m_Operators.emplace(name, std::move(callbackOperator));
return *itPair.first->second;
}
} // end namespace adios2 } // end namespace adios2
...@@ -158,8 +158,16 @@ private: ...@@ -158,8 +158,16 @@ private:
const std::string, const std::string, \ const std::string, const std::string, \
const Dims &)> &function, \ const Dims &)> &function, \
const Params &parameters); const Params &parameters);
ADIOS2_FOREACH_TYPE_1ARG(declare_type) ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type #undef declare_type
/** define CallBack2 */
Operator &DefineCallBack(
const std::string name,
const std::function<void(void *, const std::string, const std::string,
const std::string, const Dims &)> &function,
const Params &parameters);
}; };
} // end namespace adios2 } // end namespace adios2
......
...@@ -237,7 +237,7 @@ public: ...@@ -237,7 +237,7 @@ public:
* @param parameters specific parameters for IO * @param parameters specific parameters for IO
*/ */
void AddOperator(Operator &adiosOperator, void AddOperator(Operator &adiosOperator,
const Params &parameters) noexcept; const Params &parameters = Params()) noexcept;
/** /**
* Creates a polymorphic object that derives the Engine class, * Creates a polymorphic object that derives the Engine class,
......
...@@ -30,15 +30,22 @@ Params &Operator::GetParameters() noexcept { return m_Parameters; } ...@@ -30,15 +30,22 @@ Params &Operator::GetParameters() noexcept { return m_Parameters; }
#define declare_type(T) \ #define declare_type(T) \
\ \
void Operator::RunCallback1(const T *arg1, const std::string &arg2, \ void Operator::RunCallback1(const T *arg0, const std::string &arg1, \
const std::string &arg3, \ const std::string &arg2, \
const std::string &arg4, const Dims &arg5) \ const std::string &arg3, const Dims &arg4) \
{ \ { \
CheckCallbackType("Callback1"); \ CheckCallbackType("Callback1"); \
} }
ADIOS2_FOREACH_TYPE_1ARG(declare_type) ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type #undef declare_type
void Operator::RunCallback2(void *arg0, const std::string &arg1,
const std::string &arg2, const std::string &arg3,
const Dims &arg4)
{
CheckCallbackType("Callback2");
}
size_t Operator::BufferMaxSize(const size_t sizeIn) const size_t Operator::BufferMaxSize(const size_t sizeIn) const
{ {
if (m_DebugMode) if (m_DebugMode)
......
...@@ -52,6 +52,9 @@ public: ...@@ -52,6 +52,9 @@ public:
const Dims &); const Dims &);
ADIOS2_FOREACH_TYPE_1ARG(declare_type) ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type #undef declare_type
virtual void RunCallback2(void *, const std::string &, const std::string &,
const std::string &, const Dims &);
/** /**
* Returns a conservative buffer size to hold input data for classes * Returns a conservative buffer size to hold input data for classes
* @param sizeIn size of input data to be compressed in bytes * @param sizeIn size of input data to be compressed in bytes
......
...@@ -40,7 +40,8 @@ void Signature1<T>::RunCallback1(const T *arg1, const std::string &arg2, ...@@ -40,7 +40,8 @@ void Signature1<T>::RunCallback1(const T *arg1, const std::string &arg2,
} }
else else
{ {
throw std::runtime_error("ERROR: function Callback1 failed\n"); throw std::runtime_error(
"ERROR: callback function of Signature1<T> type failed\n");
} }
} }
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Signature2.cpp
*
* Created on: Oct 20, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include "Signature2.h"
namespace adios2
{
namespace callback
{
Signature2::Signature2(
const std::function<void(void *, const std::string &, const std::string &,
const std::string &, const Dims &)> &function,
const Params &parameters, const bool debugMode)
: Operator("Signature2", parameters, debugMode), m_Function(function)
{
}
void Signature2::RunCallback2(void *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: callback function of Signature2 type failed\n");
}
}
} // end namespace callback
} // end namespace adios2
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Signature2.h
*
* Created on: Oct 20, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#ifndef ADIOS2_OPERATOR_CALLBACK_SIGNATURE2_H_
#define ADIOS2_OPERATOR_CALLBACK_SIGNATURE2_H_
#include "adios2/core/Operator.h"
namespace adios2
{
namespace callback
{
class Signature2 : public Operator
{
public:
Signature2(const std::function<
void(void *, const std::string &, const std::string &,
const std::string &, const Dims &)> &function,
const Params &parameters, const bool debugMode);
~Signature2() = default;
void RunCallback2(void *, const std::string &, const std::string &,
const std::string &, const Dims &) final;
private:
std::function<void(void *, const std::string &, const std::string &,
const std::string &, const Dims &)>
m_Function;
};
}
}
#endif /* ADIOS2_OPERATOR_CALLBACK_SIGNATURE2_H_ */
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