Add DeviceAccessor<T,read> ctor from const DeviceBuffer<T>
[alexxy/gromacs.git] / src / gromacs / gpu_utils / devicebuffer_sycl.h
index ff83c6d27c5d96dafbab44e9707bcd92da834203..187b7cf9311ca00ef03019ed538fb5b92acb1f96 100644 (file)
@@ -150,6 +150,18 @@ public:
         gmx::internal::PlaceholderAccessor<T, mode>(getSyclBuffer(buffer))
     {
     }
+    //! Construct read-only Accessor from a const DeviceBuffer (must be initialized)
+    DeviceAccessor(const DeviceBuffer<T>& buffer) :
+        gmx::internal::PlaceholderAccessor<T, mode>(getSyclBuffer(const_cast<DeviceBuffer<T>&>(buffer)))
+    {
+        /* There were some discussions about making it possible to create read-only sycl::accessor
+         * from a const sycl::buffer (https://github.com/KhronosGroup/SYCL-Docs/issues/10), but
+         * it did not make it into the SYCL2020 standard. So, we have to use const_cast above */
+        /* Using static_assert to ensure that only mode::read accessors can be created from a
+         * const DeviceBuffer. static_assert provides better error messages than std::enable_if. */
+        static_assert(mode == cl::sycl::access::mode::read,
+                      "Can not create non-read-only accessor from a const DeviceBuffer");
+    }
 
 private:
     //! Helper function to get sycl:buffer object from DeviceBuffer wrapper, with a sanity check.