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 <cstdlib>
33#include <iostream>
34
35#include <cuda_runtime.h>
36#include <cusolverDn.h>
37
38namespace mx
39{
40namespace cuda
41{
42
43/// Management of a cusolverDn handle
44/** RAII management of a cusolverDn handle.
45 *
46 * The handle is not created automatically on default construction, e.g. in case
47 * it is desired to do so in a critical block scope.
48 *
49 * The handle is destroyed on the call to this class's destructor.
50 *
51 * \todo throw exceptions in cuda::cusolverDNHandle
52 */
53struct cusolverDnHandle
54{
55
56 private:
57 cusolverDnHandle_t m_handle{ NULL };
58
59 public:
60 /// Default c'tor
61 /** Creates (allocates) the handle and sets the stream to nullptr
62 */
63 cusolverDnHandle()
64 {
65 create( nullptr );
66 }
67
68 /// Constructor with option to create / not create the handle
69 explicit cusolverDnHandle( bool create /**< [in] if true the handle is created. if false it is not created. */ )
70 {
71 if( create )
72 {
73 this->create();
74 }
75 }
76
77 /// Constructor which creates the handle and sets the stream
78 explicit cusolverDnHandle( cudaStream_t stream /**< [in] cuda stream to associate with this handle. */ )
79 {
80 create( stream );
81 }
82
83 /// Destructor
84 ~cusolverDnHandle()
85 {
86 if( m_handle )
87 {
88 cusolverDnDestroy( m_handle );
89 }
90 }
91
92 /// Create (allocate) the handle.
93 void create()
94 {
95 cusolverStatus_t csec = cusolverDnCreate( &m_handle );
96 if( csec != CUSOLVER_STATUS_SUCCESS )
97 {
98 std::cerr << __FILE__ << " " << __LINE__ << " " << csec << "\n";
99 exit( -1 );
100 }
101 }
102
103 /// Create (allocate) the handle.
104 void create( cudaStream_t stream )
105 {
106 cusolverStatus_t csec = cusolverDnCreate( &m_handle );
107 if( csec != CUSOLVER_STATUS_SUCCESS )
108 {
109 std::cerr << __FILE__ << " " << __LINE__ << " " << csec << "\n";
110 exit( -1 );
111 }
112 setStream( stream );
113 }
114
115 /// Create (allocate) the handle.
116 void setStream( cudaStream_t stream )
117 {
118 if( m_handle == NULL )
119 {
120 std::cerr << __FILE__ << " " << __LINE__ << " cusolverDnHandle::setStream m_handle not set";
121 exit( -1 );
122 }
123
124 cusolverStatus_t csec = cusolverDnSetStream( m_handle, stream );
125 if( csec != CUSOLVER_STATUS_SUCCESS )
126 {
127 std::cerr << __FILE__ << " " << __LINE__ << "\n";
128 exit( -1 );
129 }
130 }
131
132 /// Get the handle for use in calls to cusolverDN routines
133 /**
134 * \returns the cusolverDn handle
135 */
136 cusolverDnHandle_t operator()()
137 {
138 return m_handle;
139 }
140
141 /// Conversion operator, allows objects of this class to be used as if they are the handle
142 /**
143 * \returns the cusolverDn handle
144 */
145 operator cusolverDnHandle_t()
146 {
147 return m_handle;
148 }
149};
150
151} // namespace cuda
152} // namespace mx
153
154#endif // #MXLIB_CUDA
155
156#endif // math_cusolverDnHandle_hpp
The mxlib c++ namespace.
Definition mxlib.hpp:37