Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to C API a function to get blockinfo: #2837

Merged
merged 2 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions bindings/C/adios2/c/adios2_c_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,154 @@ adios2_error adios2_lock_reader_selections(adios2_engine *engine)
}
}

adios2_varinfo *adios2_inquire_blockinfo(adios2_engine *engine,
adios2_variable *variable,
const size_t step)
{
auto lf_CopyDims = [](const std::vector<size_t> &dims) -> size_t * {
size_t *a = nullptr;
size_t ndims = dims.size();
if (ndims > 0)
{
a = (size_t *)malloc(dims.size() * sizeof(size_t));
std::memcpy(a, dims.data(), dims.size() * sizeof(size_t));
}
return a;
};

adios2_varinfo *varinfo = NULL;

try
{
adios2::helper::CheckForNullptr(
engine, "for adios2_engine, in call to adios2_inquire_blockinfo");

adios2::core::Engine *engineCpp =
reinterpret_cast<adios2::core::Engine *>(engine);

if (engineCpp->m_EngineType == "NULL")
{
return NULL;
}
adios2::helper::CheckForNullptr(variable,
"for adios2_variable, in call "
"to adios2_get");

adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);

const adios2::DataType type(variableBase->m_Type);

const auto minBlocksInfo =
engineCpp->MinBlocksInfo(*variableBase, step);

if (minBlocksInfo)
{
varinfo = (adios2_varinfo *)malloc(sizeof(adios2_varinfo));
varinfo->nblocks = minBlocksInfo->BlocksInfo.size();
varinfo->BlocksInfo = (adios2_blockinfo *)malloc(
varinfo->nblocks * sizeof(adios2_blockinfo));
auto *b = varinfo->BlocksInfo;

varinfo->Dims = minBlocksInfo->Dims;
varinfo->Shape = minBlocksInfo->Shape;
varinfo->IsValue = (int)minBlocksInfo->IsValue;
varinfo->IsReverseDims = (int)minBlocksInfo->IsReverseDims;
for (size_t i = 0; i < varinfo->nblocks; ++i)
{
b[i].WriterID = minBlocksInfo->BlocksInfo[i].WriterID;
b[i].BlockID = minBlocksInfo->BlocksInfo[i].BlockID;
b[i].Start = minBlocksInfo->BlocksInfo[i].Start;
b[i].Count = minBlocksInfo->BlocksInfo[i].Count;
if (minBlocksInfo->IsValue)
{
b[i].Value.uint64 = 0;
// = *((T *)minBlocksInfo->BlocksInfo[i].BufferP);
}
else
{
b[i].MinUnion.uint64 = 0;
// = minBlocksInfo->BlocksInfo[i].MinUnion;
b[i].MaxUnion.uint64 = 0;
// = minBlocksInfo->BlocksInfo[i].MaxUnion;
}
}
delete minBlocksInfo;
return varinfo;
}

/* Call the big gun Engine::BlocksInfo<T> */
if (type == adios2::DataType::Compound)
{
return varinfo;
}
else if (type == adios2::helper::GetDataType<std::string>())
{
const auto blocksInfo = engineCpp->BlocksInfo<std::string>(
*dynamic_cast<adios2::core::Variable<std::string> *>(
variableBase),
step);
varinfo = (adios2_varinfo *)malloc(sizeof(adios2_varinfo));
varinfo->nblocks = blocksInfo.size();
varinfo->BlocksInfo = (adios2_blockinfo *)malloc(
varinfo->nblocks * sizeof(adios2_blockinfo));
auto *b = varinfo->BlocksInfo;

varinfo->Dims = static_cast<int>(blocksInfo[0].Shape.size());
varinfo->Shape = lf_CopyDims(blocksInfo[0].Shape);
varinfo->IsValue = (int)blocksInfo[0].IsValue;
varinfo->IsReverseDims = (int)blocksInfo[0].IsReverseDims;
for (size_t i = 0; i < varinfo->nblocks; ++i)
{
b[i].WriterID = blocksInfo[i].WriterID;
b[i].BlockID = blocksInfo[i].BlockID;
b[i].Start = lf_CopyDims(blocksInfo[i].Start);
b[i].Count = lf_CopyDims(blocksInfo[i].Count);
// minBlocksInfo->BlocksInfo[i].MinUnion;
b[i].MinUnion.uint64 = 0;
// minBlocksInfo->BlocksInfo[i].MaxUnion;
b[i].MaxUnion.uint64 = 0;
b[i].Value.str = (char *)malloc(blocksInfo[i].Value.size() + 1);
std::strcpy(b[i].Value.str, blocksInfo[i].Value.data());
};
}
#define declare_template_instantiation(T) \
else if (type == adios2::helper::GetDataType<T>()) \
{ \
const auto blocksInfo = engineCpp->BlocksInfo<T>( \
*dynamic_cast<adios2::core::Variable<T> *>(variableBase), step); \
varinfo = (adios2_varinfo *)malloc(sizeof(adios2_varinfo)); \
varinfo->nblocks = blocksInfo.size(); \
varinfo->BlocksInfo = (adios2_blockinfo *)malloc( \
varinfo->nblocks * sizeof(adios2_blockinfo)); \
auto *b = varinfo->BlocksInfo; \
\
varinfo->Dims = static_cast<int>(blocksInfo[0].Shape.size()); \
varinfo->Shape = lf_CopyDims(blocksInfo[0].Shape); \
varinfo->IsValue = (int)blocksInfo[0].IsValue; \
varinfo->IsReverseDims = (int)blocksInfo[0].IsReverseDims; \
for (size_t i = 0; i < varinfo->nblocks; ++i) \
{ \
b[i].WriterID = blocksInfo[i].WriterID; \
b[i].BlockID = blocksInfo[i].BlockID; \
b[i].Start = lf_CopyDims(blocksInfo[i].Start); \
b[i].Count = lf_CopyDims(blocksInfo[i].Count); \
b[i].MinUnion.uint64 = 0; \
b[i].MaxUnion.uint64 = 0; \
b[i].Value.uint64 = 0; \
}; \
}
ADIOS2_FOREACH_PRIMITIVE_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
}
catch (...)
{
adios2::helper::ExceptionToError("adios2_inquire_blockinfo");
return NULL;
}
return varinfo;
}

adios2_error adios2_close_by_index(adios2_engine *engine,
const int transport_index)
{
Expand Down
10 changes: 10 additions & 0 deletions bindings/C/adios2/c/adios2_c_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ adios2_error adios2_lock_writer_definitions(adios2_engine *engine);
*/
adios2_error adios2_lock_reader_selections(adios2_engine *engine);

/**
* Get the list of blocks for a variable in a given step.
* In Streaming mode, step is unused, always the current step is processed.
* @return Newly allocated adios2_varinfo structure, nullptr if step does not
* exist. The pointer must be freed by user
*/
adios2_varinfo *adios2_inquire_blockinfo(adios2_engine *engine,
adios2_variable *variable,
const size_t step);

#ifdef __cplusplus
} // end extern C
#endif
Expand Down
37 changes: 37 additions & 0 deletions bindings/C/adios2/c/adios2_c_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,43 @@ static const size_t adios2_string_array_element_max_size = 4096;

static const size_t adios2_local_value_dim = SIZE_MAX - 2;

union adios2_PrimitiveStdtypeUnion
{
int8_t int8;
int16_t int16;
int32_t int32;
int64_t int64;
uint8_t uint8;
uint16_t uint16;
uint32_t uint32;
uint64_t uint64;
float f;
double d;
long double ld;
char *str;
};

typedef struct
{
int WriterID;
size_t BlockID;
size_t *Start;
size_t *Count;
union adios2_PrimitiveStdtypeUnion MinUnion;
union adios2_PrimitiveStdtypeUnion MaxUnion;
union adios2_PrimitiveStdtypeUnion Value;
} adios2_blockinfo;

typedef struct
{
int Dims;
size_t *Shape;
int IsValue;
int IsReverseDims;
size_t nblocks;
adios2_blockinfo *BlocksInfo;
} adios2_varinfo;

#ifdef __cplusplus
} // end extern C
#endif
Expand Down
Loading