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

First C bindings working version

Added working example examples/hello/bpWriter/helloBPWriter.c
Changed Engine signature to resolve Write void* at runtime (required by
C bindings).

To do:
CMake infrastructure
Testing
parent 65578617
No related branches found
No related tags found
1 merge request!229Initial C bindings
Showing with 208 additions and 79 deletions
......@@ -4,7 +4,7 @@ CXX=mpic++
CXXFLAGS=-O0 -g -Wall -std=c++11 -fPIC
BASE=adios2_c
ADIOS2_DIR=/home/wgodoy/tmp/adios2
ADIOS2_DIR=/home/wfg/tmp/adios2
all: $(BASE).h $(BASE).o $(BASE)_enums.h
$(CXX) -shared $(BASE).o -o libadios2_c.so -I./ -I$(ADIOS2_DIR)/include -L$(ADIOS2_DIR)/lib -ladios2 -ldataman
......
......@@ -160,17 +160,17 @@ adios2_define_variable(adios2_IO *io, const char *name, const adios2_type type,
case (adios2_type_double_complex):
variable = dynamic_cast<adios2::Variable<std::complex<double>> *>(
&ioCpp.DefineVariable<std::complex<double>>(
name, shapeV, startV, countV, constantSizeBool));
// variable = dynamic_cast<adios2::Variable<std::complex<double>>
// *>(
// &ioCpp.DefineVariable<std::complex<double>>(
// name, shapeV, startV, countV, constantSizeBool));
break;
case (adios2_type_int8_t):
// variable = dynamic_cast<adios2::Variable<int8_t> *>(
// &ioCpp.DefineVariable<int8_t>(name, shapeV, startV,
// countV,
// constantSizeBool));
variable = dynamic_cast<adios2::Variable<int8_t> *>(
&ioCpp.DefineVariable<int8_t>(name, shapeV, startV, countV,
constantSizeBool));
break;
case (adios2_type_int16_t):
......@@ -293,7 +293,7 @@ void adios2_write(adios2_Engine *engine, adios2_Variable *variable,
void adios2_write_by_name(adios2_Engine *engine, const char *variable_name,
const void *values)
{
// engine->EngineCpp->Write(variable_name, values);
engine->EngineCpp->Write(variable_name, values);
}
void adios2_advance(adios2_Engine *engine) { engine->EngineCpp->Advance(); }
......@@ -308,10 +308,6 @@ void adios2_close_by_index(adios2_Engine *engine,
const unsigned int transport_index)
{
engine->EngineCpp->Close(transport_index);
if (engine->EngineCpp) // need to add flag in Engine
{
delete engine;
}
}
void adios2_finalize(adios2_ADIOS *adios)
......
......@@ -11,16 +11,15 @@
#ifndef ADIOS2_BINDINGS_C_ADIOS2_C_H_
#define ADIOS2_BINDINGS_C_ADIOS2_C_H_
#include <stddef.h>
#include <mpi.h> //TODO: resolve mpi or mpidummy
#include <stddef.h> //size_t
#include "adios2_c_enums.h"
typedef void adios2_ADIOS;
typedef void adios2_IO;
typedef void adios2_Variable;
struct adios2_Engine;
#include "adios2_c_enums.h"
#include <mpi.h>
typedef struct adios2_Engine adios2_Engine;
#ifdef __cplusplus
extern "C" {
......@@ -159,4 +158,4 @@ void adios2_finalize(adios2_ADIOS *adios);
} // end extern C
#endif
#endif /* BINDINGS_C_ADIOS2_C_H_ */
#endif /* ADIOS2_BINDINGS_C_ADIOS2_C_H_ */
......@@ -15,20 +15,17 @@
extern "C" {
#endif
enum adios2_debug_mode
{
typedef enum {
adios2_debug_mode_on = 0,
adios2_debug_mode_off = 1,
};
} adios2_debug_mode;
enum adios2_constant_dims
{
typedef enum {
adios2_constant_dims_true = 0,
adios2_constant_dims_false = 1,
};
} adios2_constant_dims;
enum adios2_type
{
typedef enum {
adios2_type_string,
adios2_type_char,
......@@ -57,16 +54,15 @@ enum adios2_type
adios2_type_uint16_t,
adios2_type_uint32_t,
adios2_type_uint64_t
};
} adios2_type;
enum adios2_open_mode
{
typedef enum {
adios2_open_mode_undefined,
adios2_open_mode_write,
adios2_open_mode_read,
adios2_open_mode_append,
adios2_open_mode_read_write
};
} adios2_open_mode;
#ifdef __cplusplus
} // end extern C
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* helloBPWriter.c : C bindings version of C++11 helloBPWriter.cpp
*
* Created on: Aug 8, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include <mpi.h>
#include <stdlib.h>
#include <adios2_c.h>
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 input
const size_t Nx = 10;
float *myFloats;
myFloats = malloc(sizeof(float) * Nx);
unsigned int i;
for (i = 0; i < Nx; ++i)
{
myFloats[i] = i;
}
adios2_ADIOS *adiosH = adios2_init(MPI_COMM_WORLD, adios2_debug_mode_on);
adios2_IO *ioH = adios2_declare_io(adiosH, "BPFile_N2N");
// dims are allocated in stack
size_t shape[1];
shape[0] = (size_t)size * Nx;
size_t start[1];
start[0] = (size_t)rank * Nx;
size_t count[1];
count[0] = Nx;
adios2_Variable *variableH =
adios2_define_variable(ioH, "bpFloats", adios2_type_float, 1, shape,
start, count, adios2_constant_dims_true);
adios2_Engine *engineH =
adios2_open(ioH, "myVector.bp", adios2_open_mode_write, MPI_COMM_WORLD);
adios2_write(engineH, variableH, myFloats);
adios2_close(engineH);
adios2_finalize(adiosH);
MPI_Finalize();
return 0;
}
......@@ -24,6 +24,7 @@
//
#define ADIOS2_FOREACH_TYPE_1ARG(MACRO) \
MACRO(char) \
MACRO(signed char) \
MACRO(unsigned char) \
MACRO(short) \
MACRO(unsigned short) \
......@@ -42,6 +43,7 @@
#define ADIOS2_FOREACH_PRIMITIVE_TYPE_1ARG(MACRO) \
MACRO(char) \
MACRO(signed char) \
MACRO(unsigned char) \
MACRO(short) \
MACRO(unsigned short) \
......
......@@ -35,7 +35,15 @@ void Engine::SetCallBack(
{
}
// should these functions throw an exception?
void Engine::Write(VariableBase &variable, const void *values)
{
DoWrite(variable.m_Name, values);
}
void Engine::Write(const std::string &variableName, const void *values)
{
DoWrite(variableName, values);
}
void Engine::Advance(const float /*timeout_sec*/) {}
void Engine::Advance(const AdvanceMode /*mode*/, const float /*timeout_sec*/) {}
......@@ -89,13 +97,26 @@ void Engine::DoWrite(const std::string &variableName, const void *values)
if (type == "compound")
{
DoWrite(m_IO.GetVariableCompound(variableName), values);
VariableCompound &variable = m_IO.GetVariableCompound(variableName);
if (m_DebugMode)
{
variable.CheckDimsBeforeWrite("Write " + variable.m_Name);
}
DoWrite(variable, values);
}
#define declare_type(T) \
else if (type == GetType<T>()) \
{ \
DoWrite(m_IO.GetVariable<T>(variableName), \
reinterpret_cast<const T *>(values)); \
Variable<T> &variable = m_IO.GetVariable<T>(variableName); \
\
if (m_DebugMode) \
{ \
variable.CheckDimsBeforeWrite("Write " + variable.m_Name); \
} \
\
DoWrite(variable, reinterpret_cast<const T *>(values)); \
}
ADIOS2_FOREACH_TYPE_1ARG(declare_type)
#undef declare_type
......@@ -212,4 +233,14 @@ void Engine::ThrowUp(const std::string function) const
"\n");
}
} // end namespace adios
#define declare_template_instantiation(T) \
template void Engine::Write<T>(Variable<T> &, const T *); \
template void Engine::Write<T>(Variable<T> &, const T); \
\
template void Engine::Write<T>(const std::string &, const T *); \
template void Engine::Write<T>(const std::string &, const T);
ADIOS2_FOREACH_TYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
} // end namespace adios2
......@@ -63,31 +63,44 @@ public:
void Write(Variable<T> &variable, const T *values);
/**
* String version
* @param variableName
* Single value version
* @param variable
* @param values
*/
template <class T>
void Write(const std::string &variableName, const T *values);
void Write(Variable<T> &variable, const T value);
/**
* Single value version
* @param variable
* String version
* @param variableName
* @param values
*/
template <class T>
void Write(Variable<T> &variable, const T values);
void Write(const std::string &variableName, const T *values);
/**
* Single value version using string as variable handlers, allows rvalues to
* Single value version using string as variable handlers, allows
* rvalues to
* be passed
* @param variableName
* @param values
*/
template <class T>
void Write(const std::string &variableName, const T values);
void Write(const std::string &variableName, const T value);
/// Read API
/**
* Runtime version for either Variable<T> or VariableCompound
* @param variable
* @param values
*/
void Write(VariableBase &variable, const void *values);
/**
* Runtime version
* @param variableName
* @param values
*/
void Write(const std::string &variableName, const void *values);
/**
*
......@@ -394,7 +407,17 @@ private:
void ThrowUp(const std::string function) const;
};
} // end namespace adios
#define declare_template_instantiation(T) \
extern template void Engine::Write<T>(Variable<T> &, const T *); \
extern template void Engine::Write<T>(Variable<T> &, const T); \
\
extern template void Engine::Write<T>(const std::string &, const T *); \
extern template void Engine::Write<T>(const std::string &, const T);
ADIOS2_FOREACH_TYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
} // end namespace adios2
#include "Engine.inl"
......
......@@ -24,37 +24,6 @@ T *Engine::AllocateVariable(Variable<T> &variable, T fillValue)
variable.m_Name + " in call to \n");
}
template <class T>
void Engine::Write(Variable<T> &variable, const T *values)
{
if (m_DebugMode)
{
variable.CheckDimsBeforeWrite("Write " + variable.m_Name);
}
DoWrite(variable, values);
}
template <class T>
void Engine::Write(const std::string &variableName, const T *values)
{
Write(m_IO.GetVariable<T>(variableName), values);
}
template <class T>
void Engine::Write(Variable<T> &variable, const T values)
{
const T val = values; // need an address for memory copy
Write(variable, &values);
}
template <class T>
void Engine::Write(const std::string &variableName, const T values)
{
const T val = values; // need an address for memory copy
Write(m_IO.GetVariable<T>(variableName), &values);
}
template <class T>
void Engine::Read(Variable<T> &variable, T *values)
{
......@@ -122,6 +91,6 @@ void Engine::ScheduleRead(const std::string &variableName, T &values)
DoScheduleRead(variableName, &values);
}
} // end namespace adios
} // end namespace adios2
#endif /* ADIOS2_CORE_ENGINE_INL_ */
......@@ -16,6 +16,37 @@
namespace adios2
{
template <class T>
void Engine::Write(Variable<T> &variable, const T *values)
{
if (m_DebugMode)
{
variable.CheckDimsBeforeWrite("Write " + variable.m_Name);
}
DoWrite(variable, values);
}
template <class T>
void Engine::Write(Variable<T> &variable, const T values)
{
const T val = values; // need an address for memory copy
Write(variable, &values);
}
template <class T>
void Engine::Write(const std::string &variableName, const T *values)
{
Write(m_IO.GetVariable<T>(variableName), values);
}
template <class T>
void Engine::Write(const std::string &variableName, const T values)
{
const T val = values; // need an address for memory copy
Write(m_IO.GetVariable<T>(variableName), &values);
}
template <>
Variable<char> *Engine::InquireVariable<char>(const std::string &variableName,
const bool readIn)
......
......@@ -252,6 +252,7 @@ private:
/** Variable containers based on fixed-size type */
std::map<unsigned int, Variable<char>> m_Char;
std::map<unsigned int, Variable<signed char>> m_SChar;
std::map<unsigned int, Variable<unsigned char>> m_UChar;
std::map<unsigned int, Variable<short>> m_Short;
std::map<unsigned int, Variable<unsigned short>> m_UShort;
......
......@@ -63,6 +63,12 @@ std::map<unsigned int, Variable<char>> &IO::GetVariableMap()
return m_Char;
}
template <>
std::map<unsigned int, Variable<signed char>> &IO::GetVariableMap()
{
return m_SChar;
}
template <>
std::map<unsigned int, Variable<unsigned char>> &IO::GetVariableMap()
{
......
......@@ -33,6 +33,11 @@ inline std::string GetType<char>() noexcept
return "char";
}
template <>
inline std::string GetType<signed char>() noexcept
{
return "signed char";
}
template <>
inline std::string GetType<unsigned char>() noexcept
{
return "unsigned char";
......
......@@ -28,6 +28,12 @@ int8_t BP1Base::GetDataType<char>() const noexcept
return type_byte;
}
template <>
int8_t BP1Base::GetDataType<signed char>() const noexcept
{
return type_byte;
}
template <>
int8_t BP1Base::GetDataType<short>() const noexcept
{
......
......@@ -103,6 +103,11 @@ hid_t HDF5Common::GetHDF5Type<char>()
return H5T_NATIVE_CHAR;
}
template <>
hid_t HDF5Common::GetHDF5Type<signed char>()
{
return H5T_NATIVE_SCHAR;
}
template <>
hid_t HDF5Common::GetHDF5Type<unsigned char>()
{
return H5T_NATIVE_UCHAR;
......
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