Skip to content

Commit

Permalink
Throw a runtime error for incorrect memory space when Kokkos::Views a…
Browse files Browse the repository at this point in the history
…re used
  • Loading branch information
anagainaru committed Dec 29, 2022
1 parent 5109157 commit 5bdb3e0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 43 deletions.
10 changes: 9 additions & 1 deletion bindings/CXX11/adios2/cxx11/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
#include "Engine.tcc"

#include "adios2/core/Engine.h"

#include "adios2/helper/adiosFunctions.h"

namespace adios2
{

#ifdef ADIOS2_HAVE_GPU_SUPPORT
void Engine::CheckMemorySpace(MemorySpace variableMem, MemorySpace bufferMem)
{
if (variableMem != MemorySpace::Detect && variableMem != bufferMem)
helper::Throw<std::runtime_error>("CXX-Bindings", "Engine", "Put",
"Memory space mismatch");
}
#endif

Engine::operator bool() const noexcept
{
if (m_Engine == nullptr)
Expand Down
15 changes: 11 additions & 4 deletions bindings/CXX11/adios2/cxx11/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Engine
friend class IO;
friend class QueryWorker;

#ifdef ADIOS2_HAVE_GPU_SUPPORT
void CheckMemorySpace(MemorySpace variableMem, MemorySpace bufferMem);
#endif

public:
/**
* Empty (default) constructor, use it as a placeholder for future
Expand Down Expand Up @@ -217,10 +221,13 @@ class Engine
void Put(Variable<T> variable, U const &data,
const Mode launch = Mode::Deferred)
{
auto adios_data = static_cast<AdiosView<U>>(data);
auto mem_space = adios_data.memory_space();
variable.SetMemorySpace(mem_space);
Put(variable, adios_data.data(), launch);
auto bufferView = static_cast<AdiosView<U>>(data);
#ifdef ADIOS2_HAVE_GPU_SUPPORT
auto bufferMem = bufferView.memory_space();
auto variableMem = variable.GetMemorySpace();
CheckMemorySpace(variableMem, bufferMem);
#endif
Put(variable, bufferView.data(), launch);
}

/** Perform all Put calls in Deferred mode up to this point. Specifically,
Expand Down
14 changes: 13 additions & 1 deletion bindings/CXX11/adios2/cxx11/KokkosView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ struct memspace_kokkos_to_adios2<Kokkos::HostSpace>
static constexpr adios2::MemorySpace value = adios2::MemorySpace::Host;
};

#ifdef KOKKOS_ENABLE_CUDA
#if defined(KOKKOS_ENABLE_CUDA) && defined(ADIOS2_HAVE_CUDA)

template <>
struct memspace_kokkos_to_adios2<Kokkos::CudaSpace>
{
static constexpr adios2::MemorySpace value = adios2::MemorySpace::CUDA;
};

template <>
struct memspace_kokkos_to_adios2<Kokkos::CudaUVMSpace>
{
static constexpr adios2::MemorySpace value = adios2::MemorySpace::CUDA;
};

template <>
struct memspace_kokkos_to_adios2<Kokkos::CudaHostPinnedSpace>
{
static constexpr adios2::MemorySpace value = adios2::MemorySpace::CUDA;
};

#endif

} // namespace detail
Expand Down
6 changes: 6 additions & 0 deletions bindings/CXX11/adios2/cxx11/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ namespace adios2
} \
\
template <> \
MemorySpace Variable<T>::GetMemorySpace() \
{ \
return m_Variable->m_MemSpace; \
} \
\
template <> \
void Variable<T>::SetShape(const Dims &shape) \
{ \
helper::CheckForNullptr(m_Variable, \
Expand Down
6 changes: 6 additions & 0 deletions bindings/CXX11/adios2/cxx11/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ class Variable
*/
void SetMemorySpace(const MemorySpace mem);

/**
* Get the memory space that was set by the application
* @return the memory space stored in the Variable object
*/
MemorySpace GetMemorySpace();

/**
* Set new shape, care must be taken when reading back the variable for
* different steps. Only applies to Global arrays.
Expand Down
2 changes: 1 addition & 1 deletion source/adios2/core/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace core
info.StepsCount = stepsCount; \
info.Data = const_cast<T *>(data); \
info.Operations = m_Operations; \
info.MemSpace = DetectMemorySpace((void *)data); \
info.MemSpace = GetMemorySpace((void *)data); \
m_BlocksInfo.push_back(info); \
return m_BlocksInfo.back(); \
} \
Expand Down
28 changes: 4 additions & 24 deletions source/adios2/core/VariableBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ size_t VariableBase::TotalSize() const noexcept
return helper::GetTotalSize(m_Count);
}

MemorySpace VariableBase::DetectMemorySpace(const void *ptr)
MemorySpace VariableBase::GetMemorySpace(const void *ptr)
{
#ifdef ADIOS2_HAVE_GPU_SUPPORT
if (m_MemSpaceRequested != MemorySpace::Detect)
if (m_MemSpace != MemorySpace::Detect)
{
m_MemSpaceDetected = m_MemSpaceRequested;
return m_MemSpaceRequested;
return m_MemSpace;
}
#endif

Expand All @@ -63,32 +62,13 @@ MemorySpace VariableBase::DetectMemorySpace(const void *ptr)
cudaPointerGetAttributes(&attr, ptr);
if (attr.type == cudaMemoryTypeDevice)
{
m_MemSpaceDetected = MemorySpace::CUDA;
return MemorySpace::CUDA;
}
#endif
m_MemSpaceDetected = MemorySpace::Host;
return MemorySpace::Host;
}

MemorySpace VariableBase::GetMemorySpace(const void *ptr)
{
#ifdef ADIOS2_HAVE_GPU_SUPPORT
if (m_MemSpaceDetected == MemorySpace::Detect)
m_MemSpaceDetected = DetectMemorySpace(ptr);
return m_MemSpaceDetected;
#endif
return MemorySpace::Host;
}

void VariableBase::SetMemorySpace(const MemorySpace mem)
{
m_MemSpaceRequested = mem;
// reset the detected memory space
#ifdef ADIOS2_HAVE_GPU_SUPPORT
m_MemSpaceDetected = MemorySpace::Detect;
#endif
}
void VariableBase::SetMemorySpace(const MemorySpace mem) { m_MemSpace = mem; }

void VariableBase::SetShape(const adios2::Dims &shape)
{
Expand Down
15 changes: 4 additions & 11 deletions source/adios2/core/VariableBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ class VariableBase
/** Variable -> sizeof(T),
* VariableStruct -> from constructor sizeof(struct) */
const size_t m_ElementSize;
/* User requested memory space and the detected memory space */

/* User requested memory space */
#ifdef ADIOS2_HAVE_GPU_SUPPORT
MemorySpace m_MemSpaceRequested = MemorySpace::Detect;
MemorySpace m_MemSpaceDetected = MemorySpace::Detect;
MemorySpace m_MemSpace = MemorySpace::Detect;
#else
MemorySpace m_MemSpaceRequested = MemorySpace::Host;
MemorySpace m_MemSpaceDetected = MemorySpace::Host;
MemorySpace m_MemSpace = MemorySpace::Host;
#endif

ShapeID m_ShapeID = ShapeID::Unknown; ///< see shape types in ADIOSTypes.h
Expand Down Expand Up @@ -123,12 +122,6 @@ class VariableBase
*/
size_t TotalSize() const noexcept;

/**
* Detect the memory space where a buffer was allocated and return it
* @param pointer to the user data
*/
MemorySpace DetectMemorySpace(const void *ptr);

/**
* Get the memory space where a given buffers was allocated
* @param pointer to the user data
Expand Down
2 changes: 1 addition & 1 deletion source/adios2/engine/bp5/BP5Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ void BP5Writer::PutCommon(VariableBase &variable, const void *values, bool sync)
}

// if the user buffer is allocated on the GPU always use sync mode
if (variable.DetectMemorySpace(values) != MemorySpace::Host)
if (variable.GetMemorySpace(values) != MemorySpace::Host)
sync = true;

size_t *Shape = NULL;
Expand Down

0 comments on commit 5bdb3e0

Please sign in to comment.