Skip to content

Commit

Permalink
Propagating the GPU logic inside the DataMan engine
Browse files Browse the repository at this point in the history
  • Loading branch information
anagainaru committed Nov 18, 2023
1 parent 5752d99 commit 4d2ac4b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
3 changes: 2 additions & 1 deletion bindings/CXX11/adios2/cxx11/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ class Engine
void Put(Variable<T> variable, U const &data, const Mode launch = Mode::Deferred)
{
auto bufferView = static_cast<AdiosView<U>>(data);
#ifdef ADIOS2_HAVE_GPU_SUPPORT
auto bufferMem = bufferView.memory_space();
#ifdef ADIOS2_HAVE_GPU_SUPPORT
auto variableMem = variable.GetMemorySpace();
CheckMemorySpace(variableMem, bufferMem);
#endif
variable.SetMemorySpace(bufferMem);
Put(variable, bufferView.data(), launch);
}

Expand Down
8 changes: 4 additions & 4 deletions source/adios2/engine/dataman/DataManReader.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ void DataManReader::GetDeferredCommon(Variable<T> &variable, T *data)
{
while (true)
{
int ret =
m_Serializer.GetData(data, variable.m_Name, variable.m_Start, variable.m_Count,
m_CurrentStep, variable.m_MemoryStart, variable.m_MemoryCount);
int ret = m_Serializer.GetData(data, variable.m_Name, variable.m_Start,
variable.m_Count, m_CurrentStep, variable.m_MemSpace,
variable.m_MemoryStart, variable.m_MemoryCount);
if (ret == 0)
{
break;
Expand All @@ -57,7 +57,7 @@ void DataManReader::GetDeferredCommon(Variable<T> &variable, T *data)
while (true)
{
int ret = m_Serializer.GetData(data, variable.m_Name, start, count, m_CurrentStep,
memstart, memcount);
variable.m_MemSpace, memstart, memcount);
if (ret == 0)
{
break;
Expand Down
3 changes: 2 additions & 1 deletion source/adios2/engine/dataman/DataManWriter.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ void DataManWriter::PutDeferredCommon(Variable<T> &variable, const T *values)
std::reverse(memstart.begin(), memstart.end());
std::reverse(memcount.begin(), memcount.end());
m_Serializer.PutData(variable.m_Data, variable.m_Name, shape, start, count, memstart,
memcount, m_Name, CurrentStep(), m_MpiRank, "", variable.m_Operations);
memcount, variable.m_MemSpace, m_Name, CurrentStep(), m_MpiRank, "",
variable.m_Operations);
}

if (m_MonitorActive)
Expand Down
17 changes: 12 additions & 5 deletions source/adios2/toolkit/format/dataman/DataManSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ void DataManSerializer::Log(const int level, const std::string &message, const b
void DataManSerializer::PutData(const std::string *inputData, const std::string &varName,
const Dims &varShape, const Dims &varStart, const Dims &varCount,
const Dims &varMemStart, const Dims &varMemCount,
const std::string &doid, const size_t step, const int rank,
const std::string &address,
const MemorySpace varMemSpace, const std::string &doid,
const size_t step, const int rank, const std::string &address,
const std::vector<std::shared_ptr<core::Operator>> &ops,
VecPtr localBuffer, JsonPtr metadataJson)
{
Expand Down Expand Up @@ -646,8 +646,14 @@ void DataManSerializer::PutData(const std::string *inputData, const std::string

localBuffer->resize(localBuffer->size() + inputData->size());

std::memcpy(localBuffer->data() + localBuffer->size() - inputData->size(), inputData->data(),
inputData->size());
#ifdef ADIOS2_HAVE_GPU_SUPPORT
if (varMemSpace == MemorySpace::GPU)
helper::CopyFromGPUToBuffer(localBuffer->data(), localBuffer->size() - inputData->size(),
inputData->data(), varMemSpace, inputData->size());
#endif
if (varMemSpace == MemorySpace::Host)
std::memcpy(localBuffer->data() + localBuffer->size() - inputData->size(),
inputData->data(), inputData->size());

if (metadataJson == nullptr)
{
Expand All @@ -665,7 +671,8 @@ void DataManSerializer::PutData(const std::string *inputData, const std::string
template <>
int DataManSerializer::GetData(std::string *outputData, const std::string &varName,
const Dims &varStart, const Dims &varCount, const size_t step,
const Dims &varMemStart, const Dims &varMemCount)
const MemorySpace varMemSpace, const Dims &varMemStart,
const Dims &varMemCount)
{
PERFSTUBS_SCOPED_TIMER_FUNC();

Expand Down
15 changes: 8 additions & 7 deletions source/adios2/toolkit/format/dataman/DataManSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ class DataManSerializer
// put a variable for writer
void PutData(const std::string *inputData, const std::string &varName, const Dims &varShape,
const Dims &varStart, const Dims &varCount, const Dims &varMemStart,
const Dims &varMemCount, const std::string &doid, const size_t step,
const int rank, const std::string &address,
const Dims &varMemCount, const MemorySpace varMemSpace, const std::string &doid,
const size_t step, const int rank, const std::string &address,
const std::vector<std::shared_ptr<core::Operator>> &ops,
VecPtr localBuffer = nullptr, JsonPtr metadataJson = nullptr);

template <class T>
void PutData(const T *inputData, const std::string &varName, const Dims &varShape,
const Dims &varStart, const Dims &varCount, const Dims &varMemStart,
const Dims &varMemCount, const std::string &doid, const size_t step,
const int rank, const std::string &address,
const Dims &varMemCount, const MemorySpace varMemSpace, const std::string &doid,
const size_t step, const int rank, const std::string &address,
const std::vector<std::shared_ptr<core::Operator>> &ops,
VecPtr localBuffer = nullptr, JsonPtr metadataJson = nullptr);

Expand Down Expand Up @@ -134,8 +134,8 @@ class DataManSerializer

template <class T>
int GetData(T *output_data, const std::string &varName, const Dims &varStart,
const Dims &varCount, const size_t step, const Dims &varMemStart = Dims(),
const Dims &varMemCount = Dims());
const Dims &varCount, const size_t step, const MemorySpace varMemSpace,
const Dims &varMemStart = Dims(), const Dims &varMemCount = Dims());

void Erase(const size_t step, const bool allPreviousSteps = false);

Expand Down Expand Up @@ -166,7 +166,8 @@ class DataManSerializer
nlohmann::json DeserializeJson(const char *start, size_t size);

template <typename T>
void CalculateMinMax(const T *data, const Dims &count, nlohmann::json &metaj);
void CalculateMinMax(const T *data, const Dims &count, const MemorySpace varMemSpace,
nlohmann::json &metaj);

bool StepHasMinimumBlocks(const size_t step, const int requireMinimumBlocks);

Expand Down
61 changes: 38 additions & 23 deletions source/adios2/toolkit/format/dataman/DataManSerializer.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,43 @@ namespace format
template <>
inline void DataManSerializer::CalculateMinMax<std::complex<float>>(const std::complex<float> *data,
const Dims &count,
const MemorySpace varMemSpace,
nlohmann::json &metaj)
{
}

template <>
inline void
DataManSerializer::CalculateMinMax<std::complex<double>>(const std::complex<double> *data,
const Dims &count, nlohmann::json &metaj)
inline void DataManSerializer::CalculateMinMax<std::complex<double>>(
const std::complex<double> *data, const Dims &count, const MemorySpace varMemSpace,
nlohmann::json &metaj)
{
}

template <typename T>
void DataManSerializer::CalculateMinMax(const T *data, const Dims &count, nlohmann::json &metaj)
void DataManSerializer::CalculateMinMax(const T *data, const Dims &count,
const MemorySpace varMemSpace, nlohmann::json &metaj)
{
PERFSTUBS_SCOPED_TIMER_FUNC();
size_t size = std::accumulate(count.begin(), count.end(), 1, std::multiplies<size_t>());
T max = std::numeric_limits<T>::min();
T min = std::numeric_limits<T>::max();

for (size_t j = 0; j < size; ++j)
#ifdef ADIOS2_HAVE_GPU_SUPPORT
if (varMemSpace == MemorySpace::GPU)
helper::GetGPUMinMax(data, size, min, max);
#endif
if (varMemSpace == MemorySpace::Host)
{
T value = data[j];
if (value > max)
for (size_t j = 0; j < size; ++j)
{
max = value;
}
if (value < min)
{
min = value;
T value = data[j];
if (value > max)
{
max = value;
}
if (value < min)
{
min = value;
}
}
}

Expand All @@ -73,16 +81,16 @@ void DataManSerializer::PutData(const core::Variable<T> &variable, const std::st
{
PERFSTUBS_SCOPED_TIMER_FUNC();
PutData(variable.GetData(), variable.m_Name, variable.m_Shape, variable.m_Start,
variable.m_Count, variable.m_MemoryStart, variable.m_MemoryCount, doid, step, rank,
address, variable.m_Operations, localBuffer, metadataJson);
variable.m_Count, variable.m_MemoryStart, variable.m_MemoryCount, variable.m_MemSpace,
doid, step, rank, address, variable.m_Operations, localBuffer, metadataJson);
}

template <class T>
void DataManSerializer::PutData(const T *inputData, const std::string &varName,
const Dims &varShape, const Dims &varStart, const Dims &varCount,
const Dims &varMemStart, const Dims &varMemCount,
const std::string &doid, const size_t step, const int rank,
const std::string &address,
const MemorySpace varMemSpace, const std::string &doid,
const size_t step, const int rank, const std::string &address,
const std::vector<std::shared_ptr<core::Operator>> &ops,
VecPtr localBuffer, JsonPtr metadataJson)
{
Expand Down Expand Up @@ -111,7 +119,7 @@ void DataManSerializer::PutData(const T *inputData, const std::string &varName,

if (m_EnableStat)
{
CalculateMinMax(inputData, varCount, metaj);
CalculateMinMax(inputData, varCount, varMemSpace, metaj);
}

if (not m_IsRowMajor)
Expand Down Expand Up @@ -171,7 +179,13 @@ void DataManSerializer::PutData(const T *inputData, const std::string &varName,
}
else
{
std::memcpy(localBuffer->data() + localBuffer->size() - datasize, inputData, datasize);
#ifdef ADIOS2_HAVE_GPU_SUPPORT
if (varMemSpace == MemorySpace::GPU)
helper::CopyFromGPUToBuffer(localBuffer->data(), localBuffer->size() - datasize,
inputData, varMemSpace, datasize);
#endif
if (varMemSpace == MemorySpace::Host)
std::memcpy(localBuffer->data() + localBuffer->size() - datasize, inputData, datasize);
}

if (metadataJson == nullptr)
Expand All @@ -189,7 +203,8 @@ void DataManSerializer::PutData(const T *inputData, const std::string &varName,

template <class T>
int DataManSerializer::GetData(T *outputData, const std::string &varName, const Dims &varStart,
const Dims &varCount, const size_t step, const Dims &varMemStart,
const Dims &varCount, const size_t step,
const MemorySpace varMemSpace, const Dims &varMemStart,
const Dims &varMemCount)
{
PERFSTUBS_SCOPED_TIMER_FUNC();
Expand Down Expand Up @@ -238,7 +253,7 @@ int DataManSerializer::GetData(T *outputData, const std::string &varName, const
m_OperatorMapMutex.unlock();
decompressBuffer.reserve(helper::GetTotalSize(j.count, sizeof(T)));
core::Decompress(j.buffer->data() + j.position, j.size, decompressBuffer.data(),
MemorySpace::Host);
varMemSpace);
decompressed = true;
input_data = decompressBuffer.data();
}
Expand All @@ -261,14 +276,14 @@ int DataManSerializer::GetData(T *outputData, const std::string &varName, const
helper::NdCopy(input_data, j.start, j.count, true, j.isLittleEndian,
reinterpret_cast<char *>(outputData), varStart, varCount, true,
m_IsLittleEndian, sizeof(T), j.start, j.count, varMemStart,
varMemCount);
varMemCount, false, varMemSpace);
}
else
{
helper::NdCopy(input_data, j.start, j.count, j.isRowMajor, j.isLittleEndian,
reinterpret_cast<char *>(outputData), varStart, varCount,
m_IsRowMajor, m_IsLittleEndian, sizeof(T), j.start, j.count,
varMemStart, varMemCount);
varMemStart, varMemCount, false, varMemSpace);
}
}
else
Expand Down

0 comments on commit 4d2ac4b

Please sign in to comment.