mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
cublasHandle.hpp
Go to the documentation of this file.
1/** \file cublasHandle.hpp
2 * \author Jared R. Males
3 * \brief Management of a cublas 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_cublasHandle_hpp
28#define math_cublasHandle_hpp
29
30#ifdef MXLIB_CUDA
31
32#include <cuda_runtime.h>
33#include <cublas_v2.h>
34
35namespace mx
36{
37namespace cuda
38{
39
40/// Management of a cublas handle
41/** RAII management of a cublas 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::cublasHandle
49 */
50struct cublasHandle
51{
52
53 private:
54 cublasHandle_t m_handle{ NULL };
55
56 public:
57 /// Default c'tor
58 /** Does not create the handle.
59 */
60 cublasHandle()
61 {
62 }
63
64 /// Constructor with option to create / not create the handle
65 explicit cublasHandle( bool create /**< [in] if true the handle is created. if false it is not created. */ )
66 {
67 if( create )
68 {
69 cublasStatus_t cbec = this->create();
70
71 if( cbec != CUBLAS_STATUS_SUCCESS )
72 {
73 std::string msg = std::format( "cublasHandle::cublasHandle error from create: [{}] {}\n",
74 cublasGetStatusName( cbec ),
75 cublasGetStatusString( cbec ) );
76
78 }
79 }
80 }
81
82 /// Destructor
83 ~cublasHandle()
84 {
85 cublasStatus_t cbec = destroy();
86 if( cbec != CUBLAS_STATUS_SUCCESS )
87 {
88 std::cerr << std::format( "cublasHandle::~cublasHandle error from destroy: [{}] {}\n",
89 cublasGetStatusName( cbec ),
90 cublasGetStatusString( cbec ) );
91 }
92 }
93
94 /// Create (allocate) the handle.
95 /**
96 * \returns the cuBLAS status code from cublasDestroy or cublasCreate
97 */
98 cublasStatus_t create()
99 {
100
101 cublasStatus_t cbec = destroy();
102 if( cbec != CUBLAS_STATUS_SUCCESS )
103 {
104 return cbec;
105 }
106
107 cbec = cublasCreate( &m_handle );
108
109 if( cbec != CUBLAS_STATUS_SUCCESS )
110 {
111 destroy(); // we try but ignore any errors. Hoping that any possible cleanup occurs, and sets nullptr if
112 // needed.
113 }
114
115 return cbec;
116 }
117
118 /// Destroy (de-allocate) the handle.
119 /**
120 * \returns the cuBLAS status code from cublasDestroy
121 */
122 cublasStatus_t destroy()
123 {
124 cublasStatus_t cbec = CUBLAS_STATUS_SUCCESS;
125 if( m_handle )
126 {
127 cublasStatus_t cbec = cublasDestroy( m_handle );
128
129 m_handle = nullptr;
130 }
131
132 return cbec;
133 }
134
135 /// Get the handle for use in calls to cublas routines
136 /**
137 * \returns the cublas handle
138 */
139 cublasHandle_t operator()()
140 {
141 return m_handle;
142 }
143
144 /// Conversion operator, allows objects of this class to be used as if they are the handle
145 /**
146 * \returns the cublas handle
147 */
148 operator cublasHandle_t()
149 {
150 return m_handle;
151 }
152};
153
154} // namespace cuda
155} // namespace mx
156
157#endif // MXLIB_CUDA
158
159#endif // math_cublasHandle_hpp
Augments an exception with the source file and line.
Definition exception.hpp:42
@ liberr
An error was returned by a library.
The mxlib c++ namespace.
Definition mxlib.hpp:37