refactor cuview

This commit is contained in:
Tobias Meyer Andersen 2024-08-22 15:27:23 +02:00
parent fba1858f42
commit 5919b417e3
8 changed files with 65 additions and 65 deletions

View File

@ -559,7 +559,7 @@ if(CUDA_FOUND)
GpuOwnerOverlapCopy
solver_adapter
GpuBuffer
cuview
GpuView
PROPERTIES LABELS ${gpu_label})
endif()

View File

@ -214,7 +214,7 @@ if (HAVE_CUDA)
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)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg GpuVector.cpp)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg CuView.cpp)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg GpuView.cpp)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg detail/vector_operations.cu)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg GpuSparseMatrix.cpp)
ADD_CUDA_OR_HIP_FILE(MAIN_SOURCE_FILES opm/simulators/linalg GpuDILU.cpp)
@ -241,7 +241,7 @@ if (HAVE_CUDA)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg OpmCuILU0.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg GpuJac.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg GpuVector.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg CuView.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg GpuView.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg GpuSparseMatrix.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/CuMatrixDescription.hpp)
ADD_CUDA_OR_HIP_FILE(PUBLIC_HEADER_FILES opm/simulators/linalg detail/CuSparseResource.hpp)
@ -390,7 +390,7 @@ if (HAVE_CUDA)
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_GpuBuffer.cu)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cuview.cu)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_GpuView.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)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_cuda_check_last_error.cpp)

View File

@ -21,7 +21,7 @@
#include <algorithm>
#include <fmt/core.h>
#include <opm/simulators/linalg/cuistl/GpuBuffer.hpp>
#include <opm/simulators/linalg/cuistl/CuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
namespace Opm::gpuistl
@ -193,12 +193,12 @@ template class GpuBuffer<float>;
template class GpuBuffer<int>;
template <class T>
CuView<const T> make_view(const GpuBuffer<T>& buf) {
return CuView<const T>(buf.data(), buf.size());
GpuView<const T> make_view(const GpuBuffer<T>& buf) {
return GpuView<const T>(buf.data(), buf.size());
}
template CuView<const double> make_view<double>(const GpuBuffer<double>&);
template CuView<const float> make_view<float>(const GpuBuffer<float>&);
template CuView<const int> make_view<int>(const GpuBuffer<int>&);
template GpuView<const double> make_view<double>(const GpuBuffer<double>&);
template GpuView<const float> make_view<float>(const GpuBuffer<float>&);
template GpuView<const int> make_view<int>(const GpuBuffer<int>&);
} // namespace Opm::gpuistl

View File

@ -24,7 +24,7 @@
#include <fmt/core.h>
#include <opm/common/ErrorMacros.hpp>
#include <opm/simulators/linalg/cuistl/detail/safe_conversion.hpp>
#include <opm/simulators/linalg/cuistl/CuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
#include <vector>
#include <string>
@ -274,7 +274,7 @@ private:
};
template <class T>
CuView<const T> make_view(const GpuBuffer<T>&);
GpuView<const T> make_view(const GpuBuffer<T>&);
} // namespace Opm::gpuistl
#endif

View File

@ -20,21 +20,21 @@
#include <cuda_runtime.h>
#include <algorithm>
#include <fmt/core.h>
#include <opm/simulators/linalg/cuistl/CuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
namespace Opm::gpuistl
{
template <class T>
CuView<T>::CuView(std::vector<T>& data)
: CuView(data.data(), data.size())
GpuView<T>::GpuView(std::vector<T>& data)
: GpuView(data.data(), data.size())
{
}
template <typename T>
std::vector<T>
CuView<T>::asStdVector() const
GpuView<T>::asStdVector() const
{
std::vector<T> temporary(m_numberOfElements);
copyToHost(temporary);
@ -43,7 +43,7 @@ CuView<T>::asStdVector() const
template <class T>
void
CuView<T>::copyFromHost(const T* dataPointer, size_t numberOfElements)
GpuView<T>::copyFromHost(const T* dataPointer, size_t numberOfElements)
{
if (numberOfElements > size()) {
OPM_THROW(std::runtime_error,
@ -56,7 +56,7 @@ CuView<T>::copyFromHost(const T* dataPointer, size_t numberOfElements)
template <class T>
void
CuView<T>::copyToHost(T* dataPointer, size_t numberOfElements) const
GpuView<T>::copyToHost(T* dataPointer, size_t numberOfElements) const
{
assertSameSize(numberOfElements);
OPM_CUDA_SAFE_CALL(cudaMemcpy(dataPointer, data(), numberOfElements * sizeof(T), cudaMemcpyDeviceToHost));
@ -64,19 +64,19 @@ CuView<T>::copyToHost(T* dataPointer, size_t numberOfElements) const
template <class T>
void
CuView<T>::copyFromHost(const std::vector<T>& data)
GpuView<T>::copyFromHost(const std::vector<T>& data)
{
copyFromHost(data.data(), data.size());
}
template <class T>
void
CuView<T>::copyToHost(std::vector<T>& data) const
GpuView<T>::copyToHost(std::vector<T>& data) const
{
copyToHost(data.data(), data.size());
}
template class CuView<double>;
template class CuView<float>;
template class CuView<int>;
template class GpuView<double>;
template class GpuView<float>;
template class GpuView<int>;
} // namespace Opm::gpuistl

View File

@ -16,8 +16,8 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_CUVIEW_HEADER_HPP
#define OPM_CUVIEW_HEADER_HPP
#ifndef OPM_GPUVIEW_HEADER_HPP
#define OPM_GPUVIEW_HEADER_HPP
#include <dune/common/fvector.hh>
@ -41,7 +41,7 @@ namespace Opm::gpuistl
{
/**
* @brief The CuView class is provides a view of some data allocated on the GPU
* @brief The GpuView class is provides a view of some data allocated on the GPU
* Essenstially is only stores a pointer and a size.
*
* This class supports being used from inside a CUDA/HIP Kernel.
@ -55,19 +55,19 @@ namespace Opm::gpuistl
*
**/
template <typename T>
class CuView
class GpuView
{
public:
using value_type = T;
/**
* @brief Default constructor that will initialize cublas and allocate 0 bytes of memory
*/
explicit CuView() = default;
explicit GpuView() = default;
//TODO: we probably dont need anything like this or is it useful to have views also be able to handle things on CPU?
/// @brief constructor based on std::vectors, this will make a view on the CPU
/// @param data std vector to pr
CuView(std::vector<T>& data);
GpuView(std::vector<T>& data);
/**
* @brief operator[] to retrieve a reference to an item in the buffer
@ -95,7 +95,7 @@ public:
/**
* @brief CuView allocates new GPU memory of size numberOfElements * sizeof(T) and copies numberOfElements from
* @brief GpuView allocates new GPU memory of size numberOfElements * sizeof(T) and copies numberOfElements from
* data
*
* @note This assumes the data is on the CPU.
@ -103,15 +103,15 @@ public:
* @param numberOfElements number of T elements to allocate
* @param dataOnHost data on host/CPU
*/
__host__ __device__ CuView(T* dataOnHost, size_t numberOfElements)
__host__ __device__ GpuView(T* dataOnHost, size_t numberOfElements)
: m_dataPtr(dataOnHost), m_numberOfElements(numberOfElements)
{
}
/**
* @brief ~CuView calls cudaFree
* @brief ~GpuView calls cudaFree
*/
~CuView() = default;
~GpuView() = default;
/**
* @return the raw pointer to the GPU data
@ -128,7 +128,7 @@ public:
}
/**
* @return fetch the first element in a CuView
* @return fetch the first element in a GpuView
*/
__host__ __device__ T& front()
{
@ -139,7 +139,7 @@ public:
}
/**
* @return fetch the last element in a CuView
* @return fetch the last element in a GpuView
*/
__host__ __device__ T& back()
{
@ -150,7 +150,7 @@ public:
}
/**
* @return fetch the first element in a CuView
* @return fetch the first element in a GpuView
*/
__host__ __device__ T front() const
{
@ -161,7 +161,7 @@ public:
}
/**
* @return fetch the last element in a CuView
* @return fetch the last element in a GpuView
*/
__host__ __device__ T back() const
{
@ -220,7 +220,7 @@ public:
* @return an std::vector containing the elements copied from the GPU.
*/
std::vector<T> asStdVector() const;
/// @brief Iterator class to make CuViews more similar to std containers
/// @brief Iterator class to make GpuViews more similar to std containers
class iterator {
public:
// Iterator typedefs
@ -363,7 +363,7 @@ private:
/// @brief Helper function to assert if another view has the same size
/// @param other view
__host__ __device__ void assertSameSize(const CuView<T>& other) const
__host__ __device__ void assertSameSize(const GpuView<T>& other) const
{
assertSameSize(other.m_numberOfElements);
}

View File

@ -24,7 +24,7 @@
#include <cuda_runtime.h>
#include <opm/simulators/linalg/cuistl/GpuBuffer.hpp>
#include <opm/simulators/linalg/cuistl/CuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
#include <array>
@ -36,14 +36,14 @@ 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<int>({1, 2, 3, 4, 5, 6});
const auto gpubuf = ::Opm::gpuistl::GpuBuffer<int>(buf);
auto gpuview = ::Opm::gpuistl::CuView<int>(buf.data(), buf.size());
bool gpuBufCreatedView = std::is_same<::Opm::gpuistl::CuView<int>, decltype(gpuview)>::value;
auto gpuview = ::Opm::gpuistl::GpuView<int>(buf.data(), buf.size());
bool gpuBufCreatedView = std::is_same<::Opm::gpuistl::GpuView<int>, decltype(gpuview)>::value;
BOOST_CHECK(gpuBufCreatedView);
// 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<const int>, decltype(gpuview2)>::value;
bool gpuBufCreatedView2 = std::is_same<::Opm::gpuistl::GpuView<const int>, decltype(gpuview2)>::value;
BOOST_CHECK(gpuBufCreatedView2);

View File

@ -18,13 +18,13 @@
*/
#include <config.h>
#define BOOST_TEST_MODULE TestCuView
#define BOOST_TEST_MODULE TestGpuView
#include <boost/test/unit_test.hpp>
#include <cuda_runtime.h>
#include <dune/common/fvector.hh>
#include <dune/istl/bvector.hh>
#include <opm/simulators/linalg/cuistl/CuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
#include <opm/simulators/linalg/cuistl/GpuBuffer.hpp>
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
#include <random>
@ -32,10 +32,10 @@
#include <algorithm>
#include <type_traits>
using CuViewDouble = ::Opm::gpuistl::CuView<double>;
using GpuViewDouble = ::Opm::gpuistl::GpuView<double>;
using GpuBufferDouble = ::Opm::gpuistl::GpuBuffer<double>;
__global__ void useCuViewOnGPU(CuViewDouble a, CuViewDouble b){
__global__ void useGpuViewOnGPU(GpuViewDouble a, GpuViewDouble b){
b[0] = a.front();
b[1] = a.back();
b[2] = *a.begin();
@ -48,24 +48,24 @@ BOOST_AUTO_TEST_CASE(TestCreationAndIndexing)
{
// A simple test to check that we can move data to and from the GPU
auto cpubuffer = std::vector<double>({1.0, 2.0, 42.0, 59.9451743, 10.7132692});
auto GpuBuffer = GpuBufferDouble(cpubuffer);
auto cuview = CuViewDouble(GpuBuffer.data(), GpuBuffer.size());
const auto const_cuview = CuViewDouble(GpuBuffer.data(), GpuBuffer.size());
auto cubuffer = GpuBufferDouble(cpubuffer);
auto gpuview = GpuViewDouble(cubuffer.data(), cubuffer.size());
const auto const_gpuview = GpuViewDouble(cubuffer.data(), cubuffer.size());
auto stdVecOfCuView = cuview.asStdVector();
auto const_stdVecOfCuView = cuview.asStdVector();
auto stdVecOfGpuView = gpuview.asStdVector();
auto const_stdVecOfGpuView = gpuview.asStdVector();
BOOST_CHECK_EQUAL_COLLECTIONS(
stdVecOfCuView.begin(), stdVecOfCuView.end(), cpubuffer.begin(), cpubuffer.end());
stdVecOfGpuView.begin(), stdVecOfGpuView.end(), cpubuffer.begin(), cpubuffer.end());
BOOST_CHECK_EQUAL_COLLECTIONS(
stdVecOfCuView.begin(), stdVecOfCuView.end(), const_stdVecOfCuView.begin(), const_stdVecOfCuView.end());
stdVecOfGpuView.begin(), stdVecOfGpuView.end(), const_stdVecOfGpuView.begin(), const_stdVecOfGpuView.end());
}
BOOST_AUTO_TEST_CASE(TestCuViewOnCPUTypes)
BOOST_AUTO_TEST_CASE(TestGpuViewOnCPUTypes)
{
auto buf = std::vector<double>({1.0, 2.0, 42.0, 59.9451743, 10.7132692});
auto cpuview = CuViewDouble(buf.data(), buf.size());
const auto const_cpuview = CuViewDouble(buf.data(), buf.size());
auto cpuview = GpuViewDouble(buf.data(), buf.size());
const auto const_cpuview = GpuViewDouble(buf.data(), buf.size());
// check that indexing a mutable view gives references when indexing it
bool correct_type_of_cpu_front = std::is_same_v<double&, decltype(cpuview.front())>;
@ -83,26 +83,26 @@ BOOST_AUTO_TEST_CASE(TestCuViewOnCPUTypes)
BOOST_CHECK(cpuview.back() == buf.back());
}
BOOST_AUTO_TEST_CASE(TestCuViewOnCPUWithSTLIteratorAlgorithm)
BOOST_AUTO_TEST_CASE(TestGpuViewOnCPUWithSTLIteratorAlgorithm)
{
auto buf = std::vector<double>({1.0, 2.0, 42.0, 59.9451743, 10.7132692});
auto cpuview = CuViewDouble(buf.data(), buf.size());
auto cpuview = GpuViewDouble(buf.data(), buf.size());
std::sort(buf.begin(), buf.end());
BOOST_CHECK(42.0 == cpuview[3]);
}
BOOST_AUTO_TEST_CASE(TestCuViewOnGPU)
BOOST_AUTO_TEST_CASE(TestGpuViewOnGPU)
{
auto buf = std::vector<double>({1.0, 2.0, 42.0, 59.9451743, 10.7132692});
auto cubufA = GpuBufferDouble(buf);
auto cuviewA = CuViewDouble(cubufA.data(), cubufA.size());
auto gpuviewA = GpuViewDouble(cubufA.data(), cubufA.size());
auto cubufB = GpuBufferDouble(4);
auto cuviewB = CuViewDouble(cubufB.data(), cubufB.size());
auto gpuviewB = GpuViewDouble(cubufB.data(), cubufB.size());
useCuViewOnGPU<<<1,1>>>(cuviewA, cuviewB);
useGpuViewOnGPU<<<1,1>>>(gpuviewA, gpuviewB);
auto vecA = cuviewA.asStdVector();
auto vecB = cuviewB.asStdVector();
auto vecA = gpuviewA.asStdVector();
auto vecB = gpuviewB.asStdVector();
// checks that front/back/begin/end works
BOOST_CHECK(vecB[0] == buf[0]);