mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
cusolverDnHandle.hpp
Go to the documentation of this file.
1/** \file cusolverDnHandle.hpp
2 * \author Jared R. Males
3 * \brief Management of a cusolverDn handle
4 * \ingroup cuda_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2025 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef math_cusolverDnHandle_hpp
28#define math_cusolverDnHandle_hpp
29
30#ifdef MXLIB_CUDA
31
32#include <cuda_runtime.h>
33#include <cusolverDn.h>
34
35namespace mx
36{
37namespace cuda
38{
39
40/// Management of a cusolverDn handle
41/** RAII management of a cusolverDn handle.
42 *
43 * The handle is not created automatically on default construction, e.g. in case
44 * it is desired to do so in a critical block scope.
45 *
46 * The handle is destroyed on the call to this class's destructor.
47 *
48 * \todo throw exceptions in cuda::cusolverDNHandle
49 */
50struct cusolverDnHandle
51{
52
53 private:
54 cusolverDnHandle_t m_handle{ NULL };
55
56 public:
57 /// Default c'tor
58 /** Creates (allocates) the handle and sets the stream to nullptr
59 */
60 cusolverDnHandle()
61 {
62 create( nullptr );
63 }
64
65 /// Constructor with option to create / not create the handle
66 explicit cusolverDnHandle( bool create /**< [in] if true the handle is created. if false it is not created. */ )
67 {
68 if( create )
69 {
70 this->create();
71 }
72 }
73
74 /// Constructor which creates the handle and sets the stream
75 explicit cusolverDnHandle( cudaStream_t stream /**< [in] cuda stream to associate with this handle. */ )
76 {
77 create( stream );
78 }
79
80 /// Destructor
81 ~cusolverDnHandle()
82 {
83 if( m_handle )
84 {
85 cusolverDnDestroy( m_handle );
86 }
87 }
88
89 /// Create (allocate) the handle.
90 void create()
91 {
92 cusolverStatus_t csec = cusolverDnCreate( &m_handle );
93 if( csec != CUSOLVER_STATUS_SUCCESS )
94 {
95 std::cerr << __FILE__ << " " << __LINE__ << " " << csec << "\n";
96 exit( -1 );
97 }
98 }
99
100 /// Create (allocate) the handle.
101 void create( cudaStream_t stream )
102 {
103 cusolverStatus_t csec = cusolverDnCreate( &m_handle );
104 if( csec != CUSOLVER_STATUS_SUCCESS )
105 {
106 std::cerr << __FILE__ << " " << __LINE__ << " " << csec << "\n";
107 exit( -1 );
108 }
109 setStream( stream );
110 }
111
112 /// Create (allocate) the handle.
113 void setStream( cudaStream_t stream )
114 {
115 if( m_handle == NULL )
116 {
117 std::cerr << __FILE__ << " " << __LINE__ << " cusolverDnHandle::setStream m_handle not set";
118 exit( -1 );
119 }
120
121 cusolverStatus_t csec = cusolverDnSetStream( m_handle, stream );
122 if( csec != CUSOLVER_STATUS_SUCCESS )
123 {
124 std::cerr << __FILE__ << " " << __LINE__ << "\n";
125 exit( -1 );
126 }
127 }
128
129 /// Get the handle for use in calls to cusolverDN routines
130 /**
131 * \returns the cusolverDn handle
132 */
133 cusolverDnHandle_t operator()()
134 {
135 return m_handle;
136 }
137
138 /// Conversion operator, allows objects of this class to be used as if they are the handle
139 /**
140 * \returns the cusolverDn handle
141 */
142 operator cusolverDnHandle_t()
143 {
144 return m_handle;
145 }
146};
147
148} // namespace cuda
149} // namespace mx
150
151#endif // #MXLIB_CUDA
152
153#endif // math_cusolverDnHandle_hpp
The mxlib c++ namespace.
Definition mxlib.hpp:37