CuSparseResource_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022-2023 SINTEF AS
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include <exception>
20#include <opm/common/ErrorMacros.hpp>
22
23namespace Opm::cuistl::detail
24{
25
26namespace
27{
28 template <class T>
29 struct CuSparseDeleteAndCreate {
30 };
31
32 template <>
33 struct CuSparseDeleteAndCreate<bsrilu02Info_t> {
34 using DeleterType = typename CuSparseResource<bsrilu02Info_t>::DeleterType;
35 using CreatorType = typename CuSparseResource<bsrilu02Info_t>::CreatorType;
36
37 static DeleterType getDeleter()
38 {
39 return cusparseDestroyBsrilu02Info;
40 }
41
42 static CreatorType getCreator()
43 {
44 return cusparseCreateBsrilu02Info;
45 }
46 };
47
48 template <>
49 struct CuSparseDeleteAndCreate<bsrsv2Info_t> {
50 using DeleterType = typename CuSparseResource<bsrsv2Info_t>::DeleterType;
51 using CreatorType = typename CuSparseResource<bsrsv2Info_t>::CreatorType;
52
53 static DeleterType getDeleter()
54 {
55 return cusparseDestroyBsrsv2Info;
56 }
57
58 static CreatorType getCreator()
59 {
60 return cusparseCreateBsrsv2Info;
61 }
62 };
63
64 template <>
65 struct CuSparseDeleteAndCreate<cusparseMatDescr_t> {
66 using DeleterType = typename CuSparseResource<cusparseMatDescr_t>::DeleterType;
68
69 static DeleterType getDeleter()
70 {
71 return cusparseDestroyMatDescr;
72 }
73
74 static CreatorType getCreator()
75 {
76 return cusparseCreateMatDescr;
77 }
78 };
79
80} // namespace
81template <class T>
83 : m_deleter(deleter)
84{
85 // TODO: This should probably not use this macro since it will disguise the
86 // proper name of the function being called.
87 OPM_CUSPARSE_SAFE_CALL(creator(&m_resource));
88}
89
90template <class T>
92 : CuSparseResource<T>(CuSparseDeleteAndCreate<T>::getCreator(), CuSparseDeleteAndCreate<T>::getDeleter())
93{
94}
95
96template <class T>
98{
99 // TODO: This should probably not use this macro since it will disguise the
100 // proper name of the function being called.
101 OPM_CUSPARSE_WARN_IF_ERROR(m_deleter(m_resource));
102}
103} // namespace Opm::cuistl::detail
The CuSparseResource class wraps a CuSparse resource in a proper RAII pattern.
Definition: CuSparseResource.hpp:55
CuSparseResource()
CuSparseResource will automatically select the proper creator and deleter based on the type (and thro...
Definition: CuSparseResource_impl.hpp:91
typename std::function< cusparseStatus_t(bsrilu02Info_t *)> CreatorType
Definition: CuSparseResource.hpp:57
typename std::function< cusparseStatus_t(bsrilu02Info_t)> DeleterType
Definition: CuSparseResource.hpp:58
~CuSparseResource()
Definition: CuSparseResource_impl.hpp:97
#define OPM_CUSPARSE_SAFE_CALL(expression)
OPM_CUSPARSE_SAFE_CALL checks the return type of the cusparse expression (function call) and throws a...
Definition: cusparse_safe_call.hpp:185
#define OPM_CUSPARSE_WARN_IF_ERROR(expression)
OPM_CUSPARSE_WARN_IF_ERROR checks the return type of the cusparse expression (function call) and issu...
Definition: cusparse_safe_call.hpp:206
Definition: cublas_safe_call.hpp:32