From 3a0dd7256b560d27650f2e97edc3109063265ba6 Mon Sep 17 00:00:00 2001 From: Artem Zhmurov Date: Thu, 10 Sep 2020 13:49:23 +0000 Subject: [PATCH] Use common device context header for OpenCL This moves the OpenCL specific definition into under macro in the main header. Makes device stream and context definitions more consistent. --- src/gromacs/gpu_utils/CMakeLists.txt | 2 + .../{device_context_ocl.h => device_context.cpp} | 52 ++++------------------ src/gromacs/gpu_utils/device_context.h | 25 ++++++++--- src/gromacs/gpu_utils/device_context_ocl.cpp | 3 +- src/gromacs/gpu_utils/device_stream_ocl.cpp | 2 +- 5 files changed, 31 insertions(+), 53 deletions(-) rename src/gromacs/gpu_utils/{device_context_ocl.h => device_context.cpp} (59%) diff --git a/src/gromacs/gpu_utils/CMakeLists.txt b/src/gromacs/gpu_utils/CMakeLists.txt index 9fea648387..58a81157d8 100644 --- a/src/gromacs/gpu_utils/CMakeLists.txt +++ b/src/gromacs/gpu_utils/CMakeLists.txt @@ -52,6 +52,7 @@ if(GMX_GPU_OPENCL) ) elseif(GMX_GPU_CUDA) gmx_add_libgromacs_sources( + device_context.cpp device_stream.cu gpu_utils.cu pinning.cu @@ -59,6 +60,7 @@ elseif(GMX_GPU_CUDA) ) else() gmx_add_libgromacs_sources( + device_context.cpp device_stream.cpp ) endif() diff --git a/src/gromacs/gpu_utils/device_context_ocl.h b/src/gromacs/gpu_utils/device_context.cpp similarity index 59% rename from src/gromacs/gpu_utils/device_context_ocl.h rename to src/gromacs/gpu_utils/device_context.cpp index c754ad54b0..63efe84bd5 100644 --- a/src/gromacs/gpu_utils/device_context_ocl.h +++ b/src/gromacs/gpu_utils/device_context.cpp @@ -32,56 +32,20 @@ * To help us fund GROMACS development, we humbly ask that you cite * the research papers on the package. Check out http://www.gromacs.org. */ -#ifndef GMX_GPU_UTILS_DEVICE_CONTEXT_OCL_H -#define GMX_GPU_UTILS_DEVICE_CONTEXT_OCL_H - -/*! \libinternal \file - * - * \brief Declarations for DeviceContext class. +/*! \internal \file * - * Only needed for OpenCL builds. Other platforms will be given a stub class. + * \brief Implements the DeviceContext for CPU build * * \author Mark Abraham * \author Artem Zhmurov * * \ingroup module_gpu_utils - * \inlibraryapi */ +#include "gmxpre.h" -#ifndef DOXYGEN - -# include "gromacs/gpu_utils/gmxopencl.h" -# include "gromacs/utility/classhelpers.h" - -struct DeviceInformation; - -// OpenCL device context class -class DeviceContext -{ -public: - /*! \brief Constructor that creates the \c cl_context - * - * \param[in] deviceInfo Platform-specific device information. - * - * \throws InternalError if context creation failed. - */ - DeviceContext(const DeviceInformation& deviceInfo); - //! Destructor - ~DeviceContext(); - - //! Get the associated device information - const DeviceInformation& deviceInfo() const { return deviceInfo_; } - //! Getter - cl_context context() const; - -private: - //! A reference to the device information used upon context creation - const DeviceInformation& deviceInfo_; - //! OpenCL context object - cl_context context_ = nullptr; - - GMX_DISALLOW_COPY_MOVE_AND_ASSIGN(DeviceContext); -}; +#include "device_context.h" -#endif // !defined DOXYGEN -#endif // GMX_GPU_UTILS_DEVICE_CONTEXT_OCL_H +//! Constructor. +DeviceContext::DeviceContext(const DeviceInformation& deviceInfo) : deviceInfo_(deviceInfo) {} +//! Destructor +DeviceContext::~DeviceContext() = default; diff --git a/src/gromacs/gpu_utils/device_context.h b/src/gromacs/gpu_utils/device_context.h index b3044c8912..c70536dd65 100644 --- a/src/gromacs/gpu_utils/device_context.h +++ b/src/gromacs/gpu_utils/device_context.h @@ -51,9 +51,11 @@ #include "config.h" #if GMX_GPU_OPENCL -# include "gromacs/gpu_utils/device_context_ocl.h" -#else -# include "gromacs/utility/classhelpers.h" +# include "gromacs/gpu_utils/gmxopencl.h" +#endif +#include "gromacs/gpu_utils/gpu_utils.h" +#include "gromacs/hardware/device_management.h" +#include "gromacs/utility/classhelpers.h" struct DeviceInformation; @@ -62,19 +64,30 @@ class DeviceContext { public: //! Constructor. - DeviceContext(const DeviceInformation& deviceInfo) : deviceInfo_(deviceInfo) {} + DeviceContext(const DeviceInformation& deviceInfo); //! Destructor - ~DeviceContext() = default; + ~DeviceContext(); //! Get the associated device information const DeviceInformation& deviceInfo() const { return deviceInfo_; } + void activate() { setActiveDevice(deviceInfo_); } + private: //! A reference to the device information used upon context creation const DeviceInformation& deviceInfo_; +#if GMX_GPU_OPENCL +public: + //! Getter + cl_context context() const; + +private: + //! OpenCL context object + cl_context context_ = nullptr; +#endif + GMX_DISALLOW_COPY_MOVE_AND_ASSIGN(DeviceContext); }; -#endif // !GMX_GPU_OPENCL #endif // GMX_GPU_UTILS_DEVICE_CONTEXT_H diff --git a/src/gromacs/gpu_utils/device_context_ocl.cpp b/src/gromacs/gpu_utils/device_context_ocl.cpp index e8deb7fab2..c7fa8af860 100644 --- a/src/gromacs/gpu_utils/device_context_ocl.cpp +++ b/src/gromacs/gpu_utils/device_context_ocl.cpp @@ -43,8 +43,7 @@ */ #include "gmxpre.h" -#include "device_context_ocl.h" - +#include "gromacs/gpu_utils/device_context.h" #include "gromacs/hardware/device_information.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/fatalerror.h" diff --git a/src/gromacs/gpu_utils/device_stream_ocl.cpp b/src/gromacs/gpu_utils/device_stream_ocl.cpp index 84407b1674..63c93f6723 100644 --- a/src/gromacs/gpu_utils/device_stream_ocl.cpp +++ b/src/gromacs/gpu_utils/device_stream_ocl.cpp @@ -42,7 +42,7 @@ */ #include "gmxpre.h" -#include "gromacs/gpu_utils/device_context_ocl.h" +#include "gromacs/gpu_utils/device_context.h" #include "gromacs/gpu_utils/device_stream.h" #include "gromacs/gpu_utils/gputraits_ocl.h" #include "gromacs/hardware/device_information.h" -- 2.11.4.GIT