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 <format>
33#include <iostream>
34#include <string>
35
36#include <cuda_runtime.h>
37#include <cublas_v2.h>
38
40
41namespace mx
42{
43namespace cuda
44{
45
46/// Management of a cublas handle
47/** RAII management of a cublas handle.
48 *
49 * The handle is not created automatically on default construction, e.g. in case
50 * it is desired to do so in a critical block scope.
51 *
52 * The handle is destroyed on the call to this class's destructor.
53 *
54 * \todo throw exceptions in cuda::cublasHandle
55 */
56struct cublasHandle
57{
58
59 private:
60 cublasHandle_t m_handle{ NULL };
61
62 public:
63 /// Default c'tor
64 /** Does not create the handle.
65 */
66 cublasHandle()
67 {
68 }
69
70 /// Constructor with option to create / not create the handle
71 explicit cublasHandle( bool create /**< [in] if true the handle is created. if false it is not created. */ )
72 {
73 if( create )
74 {
75 cublasStatus_t cbec = this->create();
76
77 if( cbec != CUBLAS_STATUS_SUCCESS )
78 {
79 std::string msg = std::format( "cublasHandle::cublasHandle error from create: [{}] {}\n",
80 cublasGetStatusName( cbec ),
81 cublasGetStatusString( cbec ) );
82
84 }
85 }
86 }
87
88 /// Destructor
89 ~cublasHandle()
90 {
91 cublasStatus_t cbec = destroy();
92 if( cbec != CUBLAS_STATUS_SUCCESS )
93 {
94 std::cerr << std::format( "cublasHandle::~cublasHandle error from destroy: [{}] {}\n",
95 cublasGetStatusName( cbec ),
96 cublasGetStatusString( cbec ) );
97 }
98 }
99
100 /// Create (allocate) the handle.
101 /**
102 * \returns the cuBLAS status code from cublasDestroy or cublasCreate
103 */
104 cublasStatus_t create()
105 {
106
107 cublasStatus_t cbec = destroy();
108 if( cbec != CUBLAS_STATUS_SUCCESS )
109 {
110 return cbec;
111 }
112
113 cbec = cublasCreate( &m_handle );
114
115 if( cbec != CUBLAS_STATUS_SUCCESS )
116 {
117 destroy(); // we try but ignore any errors. Hoping that any possible cleanup occurs, and sets nullptr if
118 // needed.
119 }
120
121 return cbec;
122 }
123
124 /// Destroy (de-allocate) the handle.
125 /**
126 * \returns the cuBLAS status code from cublasDestroy
127 */
128 cublasStatus_t destroy()
129 {
130 cublasStatus_t cbec = CUBLAS_STATUS_SUCCESS;
131 if( m_handle )
132 {
133 cublasStatus_t cbec = cublasDestroy( m_handle );
134
135 m_handle = nullptr;
136 }
137
138 return cbec;
139 }
140
141 /// Get the handle for use in calls to cublas routines
142 /**
143 * \returns the cublas handle
144 */
145 cublasHandle_t operator()()
146 {
147 return m_handle;
148 }
149
150 /// Conversion operator, allows objects of this class to be used as if they are the handle
151 /**
152 * \returns the cublas handle
153 */
154 operator cublasHandle_t()
155 {
156 return m_handle;
157 }
158};
159
160} // namespace cuda
161} // namespace mx
162
163#endif // MXLIB_CUDA
164
165#endif // math_cublasHandle_hpp
The mxlib exception class.
@ exception
An exception was thrown.
Definition error_t.hpp:51
@ liberr
An error was returned by a library.
Definition error_t.hpp:50
The mxlib c++ namespace.
Definition mxlib.hpp:37