Add HIP support for AMD GPUs

This commits adds cmake functionality that can
hipify the cuistl framework to support AMD GPUs.

Some tests have been written as HIP does not mirror
CUDA exactly.

CONVERT_CUDA_TO_HIP is the new CMAKE argument.
CMAKE version is increased to include HIP
as a language (3.21 required).

A macro is added to create a layer of indirection
that will make only cuistl files that have been
changed rehipified.

Some BDA stuff is extracted to make sure CUDA
is not accidentally included.
This commit is contained in:
Tobias Meyer Andersen
2024-03-11 15:30:51 +01:00
parent 0cafaf92cb
commit e9d6b326cc
13 changed files with 239 additions and 108 deletions

View File

@@ -21,14 +21,21 @@
#define BOOST_TEST_MODULE TestCublasHandle
#include <cuda_runtime.h>
#include <boost/test/unit_test.hpp>
#include <opm/simulators/linalg/cuistl/detail/CuBlasHandle.hpp>
BOOST_AUTO_TEST_CASE(TestGetCublasVersion)
{
#if USE_HIP
// As of April 2024 it does not seem that hip has implemented the function
// that checks the version of blas programatically. Let the test pass for now.
BOOST_CHECK(true);
#else
auto& cublasHandle = ::Opm::cuistl::detail::CuBlasHandle::getInstance();
int cuBlasVersion = -1;
OPM_CUBLAS_SAFE_CALL(cublasGetVersion(cublasHandle.get(), &cuBlasVersion));
BOOST_CHECK_LT(0, cuBlasVersion);
#endif
}

View File

@@ -33,7 +33,13 @@ BOOST_AUTO_TEST_CASE(TestCudaMalloc)
BOOST_AUTO_TEST_CASE(TestThrows)
{
// Just testing a subset here.
std::vector<cudaError_t> errorCodes {{cudaErrorAddressOfConstant, cudaErrorAlreadyAcquired}};
std::vector<cudaError_t> errorCodes;
#if USE_HIP
// A HIP equivalent of cudaErrorAdressOfConstant does not exist.
errorCodes = {{cudaErrorAlreadyAcquired}};
#else
errorCodes = {{cudaErrorAddressOfConstant, cudaErrorAlreadyAcquired}};
#endif
for (auto code : errorCodes) {
BOOST_CHECK_THROW(OPM_CUDA_SAFE_CALL(code), std::exception);
}

View File

@@ -25,6 +25,7 @@
#include <dune/common/fvector.hh>
#include <dune/istl/bvector.hh>
#include <opm/simulators/linalg/cuistl/CuVector.hpp>
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
#include <random>
BOOST_AUTO_TEST_CASE(TestDocumentedUsage)
@@ -105,7 +106,7 @@ BOOST_AUTO_TEST_CASE(TestDataPointer)
auto vectorOnGPU = Opm::cuistl::CuVector<double>(data.data(), data.size());
std::vector<double> buffer(data.size(), 0.0);
cudaMemcpy(buffer.data(), vectorOnGPU.data(), sizeof(double) * data.size(), cudaMemcpyDeviceToHost);
OPM_CUDA_SAFE_CALL(cudaMemcpy(buffer.data(), vectorOnGPU.data(), sizeof(double) * data.size(), cudaMemcpyDeviceToHost));
BOOST_CHECK_EQUAL_COLLECTIONS(data.begin(), data.end(), buffer.begin(), buffer.end());
}