Take over management of OpenCL context from PME and NBNXM
[alexxy/gromacs.git] / src / gromacs / ewald / tests / testhardwarecontexts.h
index 9846cbbb0713ab93184a3eaf4b105fd14eb2e270..03df38671cbeca40c09a0b0fedfb4b42588b7e33 100644 (file)
@@ -49,6 +49,7 @@
 #include <gtest/gtest.h>
 
 #include "gromacs/ewald/pme_gpu_program.h"
+#include "gromacs/gpu_utils/device_context.h"
 #include "gromacs/hardware/gpu_hw_info.h"
 #include "gromacs/utility/gmxassert.h"
 
@@ -80,6 +81,8 @@ struct TestHardwareContext
     std::string description_;
     //! Device information pointer
     const DeviceInformation* deviceInfo_;
+    //! Local copy of the device context pointer
+    DeviceContext deviceContext_;
     //! Persistent compiled GPU kernels for PME.
     PmeGpuProgramStorage program_;
 
@@ -88,20 +91,32 @@ public:
     CodePath getCodePath() const { return codePath_; }
     //! Returns a human-readable context description line
     std::string getDescription() const { return description_; }
+    //! Getter for the DeviceContext
+    const DeviceContext& deviceContext() const { return deviceContext_; }
     //! Returns the device info pointer
     const DeviceInformation* getDeviceInfo() const { return deviceInfo_; }
     //! Returns the persistent PME GPU kernels
     const PmeGpuProgram* getPmeGpuProgram() const { return program_.get(); }
-    //! Constructs the context
-    TestHardwareContext(CodePath codePath, const char* description, const DeviceInformation* deviceInfo) :
+    //! Constructs the context for CPU builds
+    TestHardwareContext(CodePath codePath, const char* description) :
+        codePath_(codePath),
+        description_(description)
+    {
+        GMX_RELEASE_ASSERT(codePath == CodePath::CPU,
+                           "A GPU code path should provide DeviceInformation to the "
+                           "TestHerdwareContext constructor.");
+    }
+    //! Constructs the context for GPU builds
+    TestHardwareContext(CodePath codePath, const char* description, const DeviceInformation& deviceInfo) :
         codePath_(codePath),
         description_(description),
-        deviceInfo_(deviceInfo)
+        deviceInfo_(&deviceInfo),
+        deviceContext_(deviceInfo),
+        program_(buildPmeGpuProgram(deviceInfo, deviceContext_))
     {
-        if (codePath == CodePath::GPU)
-        {
-            program_ = buildPmeGpuProgram(deviceInfo_);
-        }
+        GMX_RELEASE_ASSERT(codePath == CodePath::GPU,
+                           "TestHerdwareContext tries to construct DeviceContext and PmeGpuProgram "
+                           "in CPU build.");
     }
     ~TestHardwareContext();
 };