Device management: Add SYCL DeviceBuffer
[alexxy/gromacs.git] / src / gromacs / gpu_utils / devicebuffer_datatype.h
index 48235f4bad1cce1d15f258a03f03ead4dd241ba9..e88782b1ac807e7169b9e1ae3507231204a5b8bd 100644 (file)
 
 #include "config.h"
 
+#include <memory>
+
+#include "gromacs/math/vectypes.h"
+
 #if GMX_GPU_CUDA
 
 //! \brief A device-side buffer of ValueTypes
@@ -87,11 +91,31 @@ using DeviceBuffer = TypedClMemory<ValueType>;
 
 #elif GMX_GPU_SYCL
 
-// SYCL-TODO:
-
-//! \brief A device-side buffer of ValueTypes
+/*! \libinternal \brief
+ * A minimal wrapper around \c cl::sycl::buffer to hide it away and simplify compilation.
+ */
 template<typename ValueType>
-using DeviceBuffer = ValueType*;
+struct DeviceBuffer
+{
+    class ClSyclBufferWrapper;
+    std::unique_ptr<ClSyclBufferWrapper> buffer_;
+
+    DeviceBuffer();
+    ~DeviceBuffer();
+    DeviceBuffer(DeviceBuffer<ValueType> const& src);
+    DeviceBuffer(DeviceBuffer<ValueType>&& src) noexcept;
+    DeviceBuffer& operator=(DeviceBuffer<ValueType> const& src);
+    DeviceBuffer& operator=(DeviceBuffer<ValueType>&& src) noexcept;
+
+    //! Helper function to get the size in bytes of a single element
+    static constexpr size_t elementSize() { return sizeof(ValueType); }
+
+    // static_case<void*> is used in MPI+CUDA code, this stub is necessary for compilation.
+    explicit operator void*() const { throw; }
+};
+
+// Must explicitly instantiate for some types.
+extern template struct DeviceBuffer<gmx::RVec>;
 
 #else
 
@@ -101,5 +125,4 @@ using DeviceBuffer = void*;
 
 #endif
 
-
 #endif // GMX_GPU_UTILS_DEVICEBUFFER_DATATYPE_H