From 69897753e870005caa9633c88ce555570ef2c9db Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 22 Aug 2024 14:12:30 +0200 Subject: [PATCH] refactor CuBuffer --- CMakeLists.txt | 2 +- CMakeLists_files.cmake | 6 +- opm/simulators/linalg/cuistl/CuView.hpp | 2 +- .../cuistl/{CuBuffer.cpp => GpuBuffer.cpp} | 62 +++++++++---------- .../cuistl/{CuBuffer.hpp => GpuBuffer.hpp} | 50 +++++++-------- .../{test_cubuffer.cu => test_GpuBuffer.cu} | 8 +-- tests/cuistl/test_cuview.cu | 14 ++--- 7 files changed, 72 insertions(+), 72 deletions(-) rename opm/simulators/linalg/cuistl/{CuBuffer.cpp => GpuBuffer.cpp} (71%) rename opm/simulators/linalg/cuistl/{CuBuffer.hpp => GpuBuffer.hpp} (83%) rename tests/cuistl/{test_cubuffer.cu => test_GpuBuffer.cu} (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae952bb5c..e78d09945 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -558,7 +558,7 @@ if(CUDA_FOUND) cuseqilu0 cuowneroverlapcopy solver_adapter - cubuffer + GpuBuffer cuview PROPERTIES LABELS ${gpu_label}) endif() diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 02cbc0922..b7423ceb0 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -209,7 +209,7 @@ if (HAVE_CUDA) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/CuBlasHandle.cpp) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/cusparse_matrix_operations.cu) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/CuSparseHandle.cpp) - ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg CuBuffer.cpp) + ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg GpuBuffer.cpp) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/preconditionerKernels/DILUKernels.cu) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/preconditionerKernels/ILU0Kernels.cu) ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/preconditionerKernels/JacKernels.cu) @@ -233,7 +233,7 @@ if (HAVE_CUDA) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/cuda_check_last_error.hpp) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/CuBlasHandle.hpp) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/CuSparseHandle.hpp) - ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg CuBuffer.hpp) + ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg GpuBuffer.hpp) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/preconditionerKernels/DILUKernels.hpp) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/preconditionerKernels/ILU0Kernels.hpp) ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/preconditionerKernels/JacKernels.hpp) @@ -389,7 +389,7 @@ if (HAVE_CUDA) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_converttofloatadapter.cpp) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cublas_handle.cpp) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cublas_safe_call.cpp) - ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cubuffer.cu) + ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_GpuBuffer.cu) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cuview.cu) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cusparse_safe_call.cpp) ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cuda_safe_call.cpp) diff --git a/opm/simulators/linalg/cuistl/CuView.hpp b/opm/simulators/linalg/cuistl/CuView.hpp index 6cfa1aa68..858f9215a 100644 --- a/opm/simulators/linalg/cuistl/CuView.hpp +++ b/opm/simulators/linalg/cuistl/CuView.hpp @@ -48,7 +48,7 @@ namespace Opm::gpuistl * Implementations are placed in this headerfile for functions that may be called * inside a kernel to avoid expensive RDC (relocatable device code) * - * The view will typically provide a view into a CuBuffer and be able to + * The view will typically provide a view into a GpuBuffer and be able to * manipulate the data within it * * @param T Type of the data we store, typically int/float/double w/o const specifier diff --git a/opm/simulators/linalg/cuistl/CuBuffer.cpp b/opm/simulators/linalg/cuistl/GpuBuffer.cpp similarity index 71% rename from opm/simulators/linalg/cuistl/CuBuffer.cpp rename to opm/simulators/linalg/cuistl/GpuBuffer.cpp index 9fb1c5958..b24bc3ef0 100644 --- a/opm/simulators/linalg/cuistl/CuBuffer.cpp +++ b/opm/simulators/linalg/cuistl/GpuBuffer.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include @@ -28,24 +28,24 @@ namespace Opm::gpuistl { template -CuBuffer::CuBuffer(const std::vector& data) - : CuBuffer(data.data(), data.size()) +GpuBuffer::GpuBuffer(const std::vector& data) + : GpuBuffer(data.data(), data.size()) { } template -CuBuffer::CuBuffer(const size_t numberOfElements) +GpuBuffer::GpuBuffer(const size_t numberOfElements) : m_numberOfElements(numberOfElements) { if (numberOfElements < 1) { - OPM_THROW(std::invalid_argument, "Setting a CuBuffer size to a non-positive number is not allowed"); + OPM_THROW(std::invalid_argument, "Setting a GpuBuffer size to a non-positive number is not allowed"); } OPM_CUDA_SAFE_CALL(cudaMalloc(&m_dataOnDevice, sizeof(T) * m_numberOfElements)); } template -CuBuffer::CuBuffer(const T* dataOnHost, const size_t numberOfElements) - : CuBuffer(numberOfElements) +GpuBuffer::GpuBuffer(const T* dataOnHost, const size_t numberOfElements) + : GpuBuffer(numberOfElements) { OPM_CUDA_SAFE_CALL(cudaMemcpy( @@ -53,8 +53,8 @@ CuBuffer::CuBuffer(const T* dataOnHost, const size_t numberOfElements) } template -CuBuffer::CuBuffer(const CuBuffer& other) - : CuBuffer(other.m_numberOfElements) +GpuBuffer::GpuBuffer(const GpuBuffer& other) + : GpuBuffer(other.m_numberOfElements) { assertHasElements(); assertSameSize(other); @@ -65,24 +65,24 @@ CuBuffer::CuBuffer(const CuBuffer& other) } template -CuBuffer::~CuBuffer() +GpuBuffer::~GpuBuffer() { OPM_CUDA_WARN_IF_ERROR(cudaFree(m_dataOnDevice)); } template -typename CuBuffer::size_type -CuBuffer::size() const +typename GpuBuffer::size_type +GpuBuffer::size() const { return m_numberOfElements; } template void -CuBuffer::resize(size_t newSize) +GpuBuffer::resize(size_t newSize) { if (newSize < 1) { - OPM_THROW(std::invalid_argument, "Setting a CuBuffer size to a non-positive number is not allowed"); + OPM_THROW(std::invalid_argument, "Setting a GpuBuffer size to a non-positive number is not allowed"); } // Allocate memory for the new buffer T* tmpBuffer = nullptr; @@ -107,7 +107,7 @@ CuBuffer::resize(size_t newSize) template std::vector -CuBuffer::asStdVector() const +GpuBuffer::asStdVector() const { std::vector temporary(m_numberOfElements); copyToHost(temporary); @@ -116,14 +116,14 @@ CuBuffer::asStdVector() const template void -CuBuffer::assertSameSize(const CuBuffer& x) const +GpuBuffer::assertSameSize(const GpuBuffer& x) const { assertSameSize(x.m_numberOfElements); } template void -CuBuffer::assertSameSize(size_t size) const +GpuBuffer::assertSameSize(size_t size) const { if (size != m_numberOfElements) { OPM_THROW(std::invalid_argument, @@ -133,7 +133,7 @@ CuBuffer::assertSameSize(size_t size) const template void -CuBuffer::assertHasElements() const +GpuBuffer::assertHasElements() const { if (m_numberOfElements <= 0) { OPM_THROW(std::invalid_argument, "We have 0 elements"); @@ -142,21 +142,21 @@ CuBuffer::assertHasElements() const template T* -CuBuffer::data() +GpuBuffer::data() { return m_dataOnDevice; } template const T* -CuBuffer::data() const +GpuBuffer::data() const { return m_dataOnDevice; } template void -CuBuffer::copyFromHost(const T* dataPointer, size_t numberOfElements) +GpuBuffer::copyFromHost(const T* dataPointer, size_t numberOfElements) { if (numberOfElements > size()) { OPM_THROW(std::runtime_error, @@ -169,7 +169,7 @@ CuBuffer::copyFromHost(const T* dataPointer, size_t numberOfElements) template void -CuBuffer::copyToHost(T* dataPointer, size_t numberOfElements) const +GpuBuffer::copyToHost(T* dataPointer, size_t numberOfElements) const { assertSameSize(numberOfElements); OPM_CUDA_SAFE_CALL(cudaMemcpy(dataPointer, data(), numberOfElements * sizeof(T), cudaMemcpyDeviceToHost)); @@ -177,28 +177,28 @@ CuBuffer::copyToHost(T* dataPointer, size_t numberOfElements) const template void -CuBuffer::copyFromHost(const std::vector& data) +GpuBuffer::copyFromHost(const std::vector& data) { copyFromHost(data.data(), data.size()); } template void -CuBuffer::copyToHost(std::vector& data) const +GpuBuffer::copyToHost(std::vector& data) const { copyToHost(data.data(), data.size()); } -template class CuBuffer; -template class CuBuffer; -template class CuBuffer; +template class GpuBuffer; +template class GpuBuffer; +template class GpuBuffer; template -CuView make_view(const CuBuffer& buf) { +CuView make_view(const GpuBuffer& buf) { return CuView(buf.data(), buf.size()); } -template CuView make_view(const CuBuffer&); -template CuView make_view(const CuBuffer&); -template CuView make_view(const CuBuffer&); +template CuView make_view(const GpuBuffer&); +template CuView make_view(const GpuBuffer&); +template CuView make_view(const GpuBuffer&); } // namespace Opm::gpuistl diff --git a/opm/simulators/linalg/cuistl/CuBuffer.hpp b/opm/simulators/linalg/cuistl/GpuBuffer.hpp similarity index 83% rename from opm/simulators/linalg/cuistl/CuBuffer.hpp rename to opm/simulators/linalg/cuistl/GpuBuffer.hpp index d3e671b3b..e17ab0f2b 100644 --- a/opm/simulators/linalg/cuistl/CuBuffer.hpp +++ b/opm/simulators/linalg/cuistl/GpuBuffer.hpp @@ -16,8 +16,8 @@ You should have received a copy of the GNU General Public License along with OPM. If not, see . */ -#ifndef OPM_CUBUFFER_HEADER_HPP -#define OPM_CUBUFFER_HEADER_HPP +#ifndef OPM_GPUBUFFER_HEADER_HPP +#define OPM_GPUBUFFER_HEADER_HPP #include #include #include @@ -33,18 +33,18 @@ namespace Opm::gpuistl { /** - * @brief The CuBuffer class is a simple container class for the GPU. + * @brief The GpuBuffer class is a simple container class for the GPU. * * * Example usage: * * @code{.cpp} - * #include + * #include * * void someFunction() { * auto someDataOnCPU = std::vector({1.0, 2.0, 42.0, 59.9451743, 10.7132692}); * - * auto dataOnGPU = CuBuffer(someDataOnCPU); + * auto dataOnGPU = GpuBuffer(someDataOnCPU); * * auto stdVectorOnCPU = dataOnGPU.asStdVector(); * } @@ -52,7 +52,7 @@ namespace Opm::gpuistl * @tparam T the type to store. Can be either float, double or int. */ template -class CuBuffer +class GpuBuffer { public: using field_type = T; @@ -60,17 +60,17 @@ public: using value_type = T; /** - * @brief CuBuffer allocates new GPU memory of the same size as other and copies the content of the other buffer to + * @brief GpuBuffer allocates new GPU memory of the same size as other and copies the content of the other buffer to * this newly allocated memory. * * @note This does synchronous transfer. * * @param other the buffer to copy from */ - CuBuffer(const CuBuffer& other); + GpuBuffer(const GpuBuffer& other); /** - * @brief CuBuffer allocates new GPU memory of the same size as data and copies the content of the data vector to + * @brief GpuBuffer allocates new GPU memory of the same size as data and copies the content of the data vector to * this newly allocated memory. * * @note This does CPU to GPU transfer. @@ -78,23 +78,23 @@ public: * * @param data the vector to copy from */ - explicit CuBuffer(const std::vector& data); + explicit GpuBuffer(const std::vector& data); /** * @brief Default constructor that will initialize cublas and allocate 0 bytes of memory */ - explicit CuBuffer(); + explicit GpuBuffer(); /** - * @brief CuBuffer allocates new GPU memory of size numberOfElements * sizeof(T) + * @brief GpuBuffer allocates new GPU memory of size numberOfElements * sizeof(T) * * @param numberOfElements number of T elements to allocate */ - explicit CuBuffer(const size_t numberOfElements); + explicit GpuBuffer(const size_t numberOfElements); /** - * @brief CuBuffer allocates new GPU memory of size numberOfElements * sizeof(T) and copies numberOfElements from + * @brief GpuBuffer allocates new GPU memory of size numberOfElements * sizeof(T) and copies numberOfElements from * data * * @note This assumes the data is on the CPU. @@ -102,12 +102,12 @@ public: * @param numberOfElements number of T elements to allocate * @param dataOnHost data on host/CPU */ - CuBuffer(const T* dataOnHost, const size_t numberOfElements); + GpuBuffer(const T* dataOnHost, const size_t numberOfElements); /** - * @brief ~CuBuffer calls cudaFree + * @brief ~GpuBuffer calls cudaFree */ - virtual ~CuBuffer(); + virtual ~GpuBuffer(); /** * @return the raw pointer to the GPU data @@ -120,7 +120,7 @@ public: const T* data() const; /** - * @return fetch the first element in a CuBuffer + * @return fetch the first element in a GpuBuffer */ __host__ __device__ T& front() { @@ -131,7 +131,7 @@ public: } /** - * @return fetch the last element in a CuBuffer + * @return fetch the last element in a GpuBuffer */ __host__ __device__ T& back() { @@ -142,7 +142,7 @@ public: } /** - * @return fetch the first element in a CuBuffer + * @return fetch the first element in a GpuBuffer */ __host__ __device__ T front() const { @@ -153,7 +153,7 @@ public: } /** - * @return fetch the last element in a CuBuffer + * @return fetch the last element in a GpuBuffer */ __host__ __device__ T back() const { @@ -176,7 +176,7 @@ public: // TODO: [perf] vector.size() can be replaced by bvector.N() * BlockDimension if (m_numberOfElements != bvector.size()) { OPM_THROW(std::runtime_error, - fmt::format("Given incompatible vector size. CuBuffer has size {}, \n" + fmt::format("Given incompatible vector size. GpuBuffer has size {}, \n" "however, BlockVector has N() = {}, and size = {}.", m_numberOfElements, bvector.N(), @@ -199,7 +199,7 @@ public: // TODO: [perf] vector.size() can be replaced by bvector.N() * BlockDimension if (m_numberOfElements != bvector.size()) { OPM_THROW(std::runtime_error, - fmt::format("Given incompatible vector size. CuBuffer has size {},\n however, the BlockVector " + fmt::format("Given incompatible vector size. GpuBuffer has size {},\n however, the BlockVector " "has has N() = {}, and size() = {}.", m_numberOfElements, bvector.N(), @@ -267,14 +267,14 @@ private: T* m_dataOnDevice = nullptr; size_t m_numberOfElements; - void assertSameSize(const CuBuffer& other) const; + void assertSameSize(const GpuBuffer& other) const; void assertSameSize(size_t size) const; void assertHasElements() const; }; template -CuView make_view(const CuBuffer&); +CuView make_view(const GpuBuffer&); } // namespace Opm::gpuistl #endif diff --git a/tests/cuistl/test_cubuffer.cu b/tests/cuistl/test_GpuBuffer.cu similarity index 88% rename from tests/cuistl/test_cubuffer.cu rename to tests/cuistl/test_GpuBuffer.cu index b3cf059f9..e9fb97386 100644 --- a/tests/cuistl/test_cubuffer.cu +++ b/tests/cuistl/test_GpuBuffer.cu @@ -18,12 +18,12 @@ */ #include -#define BOOST_TEST_MODULE TestCuBuffer +#define BOOST_TEST_MODULE TestGpuBuffer #include #include -#include +#include #include #include @@ -35,13 +35,13 @@ BOOST_AUTO_TEST_CASE(TestMakeView) { // test that we can create buffers and make views of the buffers using the pointer constructor auto buf = std::vector({1, 2, 3, 4, 5, 6}); - const auto gpubuf = ::Opm::gpuistl::CuBuffer(buf); + const auto gpubuf = ::Opm::gpuistl::GpuBuffer(buf); auto gpuview = ::Opm::gpuistl::CuView(buf.data(), buf.size()); bool gpuBufCreatedView = std::is_same<::Opm::gpuistl::CuView, decltype(gpuview)>::value; BOOST_CHECK(gpuBufCreatedView); - // test that we can make views of buffers by using the cubuffer constructor + // test that we can make views of buffers by using the GpuBuffer constructor auto gpuview2 = ::Opm::gpuistl::make_view(gpubuf); bool gpuBufCreatedView2 = std::is_same<::Opm::gpuistl::CuView, decltype(gpuview2)>::value; diff --git a/tests/cuistl/test_cuview.cu b/tests/cuistl/test_cuview.cu index 53bb31a39..5f0678035 100644 --- a/tests/cuistl/test_cuview.cu +++ b/tests/cuistl/test_cuview.cu @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include @@ -33,7 +33,7 @@ #include using CuViewDouble = ::Opm::gpuistl::CuView; -using CuBufferDouble = ::Opm::gpuistl::CuBuffer; +using GpuBufferDouble = ::Opm::gpuistl::GpuBuffer; __global__ void useCuViewOnGPU(CuViewDouble a, CuViewDouble b){ b[0] = a.front(); @@ -48,9 +48,9 @@ BOOST_AUTO_TEST_CASE(TestCreationAndIndexing) { // A simple test to check that we can move data to and from the GPU auto cpubuffer = std::vector({1.0, 2.0, 42.0, 59.9451743, 10.7132692}); - auto cubuffer = CuBufferDouble(cpubuffer); - auto cuview = CuViewDouble(cubuffer.data(), cubuffer.size()); - const auto const_cuview = CuViewDouble(cubuffer.data(), cubuffer.size()); + auto GpuBuffer = GpuBufferDouble(cpubuffer); + auto cuview = CuViewDouble(GpuBuffer.data(), GpuBuffer.size()); + const auto const_cuview = CuViewDouble(GpuBuffer.data(), GpuBuffer.size()); auto stdVecOfCuView = cuview.asStdVector(); auto const_stdVecOfCuView = cuview.asStdVector(); @@ -94,9 +94,9 @@ BOOST_AUTO_TEST_CASE(TestCuViewOnCPUWithSTLIteratorAlgorithm) BOOST_AUTO_TEST_CASE(TestCuViewOnGPU) { auto buf = std::vector({1.0, 2.0, 42.0, 59.9451743, 10.7132692}); - auto cubufA = CuBufferDouble(buf); + auto cubufA = GpuBufferDouble(buf); auto cuviewA = CuViewDouble(cubufA.data(), cubufA.size()); - auto cubufB = CuBufferDouble(4); + auto cubufB = GpuBufferDouble(4); auto cuviewB = CuViewDouble(cubufB.data(), cubufB.size()); useCuViewOnGPU<<<1,1>>>(cuviewA, cuviewB);