Added handles for cusparse and cublas.

This commit is contained in:
Kjetil Olsen Lye 2023-03-28 10:29:53 +02:00
parent 03a7fb6c9d
commit 062d692c83
7 changed files with 296 additions and 1 deletions

View File

@ -144,11 +144,18 @@ if(CUDA_FOUND)
list (APPEND MAIN_SOURCE_FILES opm/simulators/linalg/bda/cuda/cusparseSolverBackend.cu)
list (APPEND MAIN_SOURCE_FILES opm/simulators/linalg/bda/cuda/cuWellContributions.cu)
# CUISTL
# CUISTL SOURCE
list (APPEND MAIN_SOURCE_FILES opm/simulators/linalg/cuistl/detail/CuBlasHandle.cpp)
list (APPEND MAIN_SOURCE_FILES opm/simulators/linalg/cuistl/detail/CuSparseHandle.cpp)
# CUISTL HEADERS
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp)
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/cusparse_safe_call.hpp)
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/cublas_safe_call.hpp)
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/cuda_check_last_error.hpp)
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/CuBlasHandle.hpp)
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/CuSparseHandle.hpp)
endif()
if(OPENCL_FOUND)
list (APPEND MAIN_SOURCE_FILES opm/simulators/linalg/bda/BlockedMatrix.cpp)
@ -230,6 +237,10 @@ if(CUDA_FOUND)
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cublas_safe_call.cpp)
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cuda_safe_call.cpp)
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cuda_check_last_error.cpp)
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cublas_handle.cpp)
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cusparse_handle.cpp)
endif()
if(OPENCL_FOUND)
list(APPEND TEST_SOURCE_FILES tests/test_openclSolver.cpp)

View File

@ -0,0 +1,49 @@
/*
Copyright 2022-2023 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cublas_v2.h>
#include <opm/simulators/linalg/cuistl/detail/CuBlasHandle.hpp>
#include <opm/simulators/linalg/cuistl/detail/cublas_safe_call.hpp>
namespace Opm::cuistl::detail
{
CuBlasHandle::CuBlasHandle()
{
OPM_CUBLAS_SAFE_CALL(cublasCreate(&m_handle));
}
CuBlasHandle::~CuBlasHandle()
{
OPM_CUBLAS_SAFE_CALL(cublasDestroy(m_handle));
}
cublasHandle_t
CuBlasHandle::get()
{
return m_handle;
}
CuBlasHandle&
CuBlasHandle::getInstance()
{
static CuBlasHandle instance;
return instance;
}
} // namespace Opm::cuistl::detail

View File

@ -0,0 +1,68 @@
/*
Copyright 2022-2023 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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_CUBLASHANDLE_HPP
#define OPM_CUBLASHANDLE_HPP
#include <cublas_v2.h>
#include <memory>
namespace Opm::cuistl::detail
{
/**
* @brief The CuBlasHandle class provides a singleton for the simulator universal cuBlasHandle.
*
* Example use:
* @code{.cpp}
* #include <opm/simulators/linalg/cuistl/detail/CuBlasHandle.hpp>
* void someFunction() {
* auto& cublasHandle = ::Opm::cuistl::detail::CuBlasHandle::getInstance();
* int cuBlasVersion = -1;
* OPM_CUBLAS_SAFE_CALL(cublasGetVersion(cublasHandle.get(), &cuBlasVersion));
* }
* @endcode
*
*/
class CuBlasHandle
{
public:
// This should not be copyable.
CuBlasHandle(const CuBlasHandle&) = delete;
CuBlasHandle& operator=(const CuBlasHandle&) = delete;
/**
* Calls cublasDestroy() on the handle
*/
~CuBlasHandle();
/**
* @brief get returns the underlying cuBlas handle (to be used in calls to cublas)
*/
cublasHandle_t get();
/**
* @brief getInstance creates (if necessary) and returns the single unique instance of CuBlasHandle (singleton)
*/
static CuBlasHandle& getInstance();
private:
CuBlasHandle();
cublasHandle_t m_handle;
};
} // namespace Opm::cuistl::detail
#endif // OPM_CUBLASHANDLE_HPP

View File

@ -0,0 +1,49 @@
/*
Copyright SINTEF AS 2022
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/simulators/linalg/cuistl/detail/CuSparseHandle.hpp>
#include <opm/simulators/linalg/cuistl/detail/cusparse_safe_call.hpp>
namespace Opm::cuistl::detail
{
CuSparseHandle::CuSparseHandle()
{
OPM_CUSPARSE_SAFE_CALL(cusparseCreate(&m_handle));
OPM_CUSPARSE_SAFE_CALL(cusparseSetStream(m_handle, 0));
}
CuSparseHandle::~CuSparseHandle()
{
OPM_CUSPARSE_SAFE_CALL(cusparseDestroy(m_handle));
}
cusparseHandle_t
CuSparseHandle::get()
{
return m_handle;
}
CuSparseHandle&
CuSparseHandle::getInstance()
{
static CuSparseHandle instance;
return instance;
}
} // namespace Opm::cuistl::detail

View File

@ -0,0 +1,51 @@
#ifndef OPM_CUSPARSEHANDLE_HPP
#define OPM_CUSPARSEHANDLE_HPP
#include <cusparse.h>
#include <memory>
namespace Opm::cuistl::detail
{
/**
* @brief The CuSparseHandle class provides a singleton for the simulator universal cuSparseHandle.
*
* Example use:
* @code{.cpp}
* #include <opm/simulators/linalg/cuistl/detail/CuSparseHandle.hpp>
* void someFunction() {
* auto& cuSparseHandle = ::Opm::cuistl::detail::CuSparseHandle::getInstance();
* int cuSparseVersion = -1;
* OPM_CUSPARSE_SAFE_CALL(cusparseGetVersion(cuSparseHandle.get(), &cuSparseVersion));
* }
* @endcode
*/
class CuSparseHandle
{
public:
// This should not be copyable.
CuSparseHandle(const CuSparseHandle&) = delete;
CuSparseHandle& operator=(const CuSparseHandle&) = delete;
/**
* Calls cuSparseDestroy on the handle
*/
~CuSparseHandle();
/**
* @brief get returns the underlying cuSparse handle (to be used in calls to cusparse)
*/
cusparseHandle_t get();
/**
* @brief getInstance creates (if necessary) and returns the single unique instance of CuSparseHandle (singleton)
*/
static CuSparseHandle& getInstance();
private:
CuSparseHandle();
cusparseHandle_t m_handle;
};
using CuSparseHandlePtr = std::unique_ptr<CuSparseHandle>;
} // namespace Opm::cuistl::detail
#endif // OPM_CUSPARSEHANDLE_HPP

View File

@ -0,0 +1,34 @@
/*
Copyright 2022-2023 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "opm/simulators/linalg/cuistl/detail/cublas_safe_call.hpp"
#include <config.h>
#define BOOST_TEST_MODULE TestCublasHandle
#include <boost/test/unit_test.hpp>
#include <opm/simulators/linalg/cuistl/detail/CuBlasHandle.hpp>
BOOST_AUTO_TEST_CASE(TestGetCublasVersion)
{
auto& cublasHandle = ::Opm::cuistl::detail::CuBlasHandle::getInstance();
int cuBlasVersion = -1;
OPM_CUBLAS_SAFE_CALL(cublasGetVersion(cublasHandle.get(), &cuBlasVersion));
BOOST_CHECK_LT(0, cuBlasVersion);
}

View File

@ -0,0 +1,33 @@
/*
Copyright 2022-2023 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#define BOOST_TEST_MODULE TestSparseHandle
#include <boost/test/unit_test.hpp>
#include <opm/simulators/linalg/cuistl/detail/CuSparseHandle.hpp>
#include <opm/simulators/linalg/cuistl/detail/cusparse_safe_call.hpp>
BOOST_AUTO_TEST_CASE(TestGetSparseVersion)
{
auto& cuSparseHandle = ::Opm::cuistl::detail::CuSparseHandle::getInstance();
int cuSparseVersion = -1;
OPM_CUSPARSE_SAFE_CALL(cusparseGetVersion(cuSparseHandle.get(), &cuSparseVersion));
BOOST_CHECK_LT(0, cuSparseVersion);
}