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#include <cuda_runtime.h>
31#include <cublas_v2.h>
32
33namespace mx
34{
35namespace cuda
36{
37
38/// Management of a cublas handle
39/** RAII management of a cublas handle.
40 *
41 * The handle is not created automatically on default construction, e.g. in case
42 * it is desired to do so in a critical block scope.
43 *
44 * The handle is destroyed on the call to this class's destructor.
45 *
46 * \todo throw exceptions in cuda::cublasHandle
47 */
49{
50
51 private:
52 cublasHandle_t m_handle{ NULL };
53
54 public:
55 /// Default c'tor
56 /** Creates the handle.
57 */
59 {
60 create();
61 }
62
63 /// Constructor with option to create / not create the handle
64 explicit cublasHandle( bool create /**< [in] if true the handle is created. if false it is not created. */ )
65 {
66 if( create )
67 {
68 this->create();
69 }
70 }
71
72 /// Destructor
74 {
75 if( m_handle )
76 {
77 cublasDestroy( m_handle );
78 }
79 }
80
81 /// Create (allocate) the handle.
82 void create()
83 {
84 cublasStatus_t cbec = cublasCreate( &m_handle );
85 if( cbec != CUBLAS_STATUS_SUCCESS )
86 {
87 std::cerr << __FILE__ << " " << __LINE__ << " " << cbec << "\n";
88 exit( -1 );
89 }
90 }
91
92 /// Get the handle for use in calls to cublas routines
93 /**
94 * \returns the cublas handle
95 */
96 cublasHandle_t operator()()
97 {
98 return m_handle;
99 }
100
101 /// Conversion operator, allows objects of this class to be used as if they are the handle
102 /**
103 * \returns the cublas handle
104 */
105 operator cublasHandle_t()
106 {
107 return m_handle;
108 }
109};
110
111} // namespace cuda
112} // namespace mx
113#endif // math_cublasHandle_hpp
The mxlib c++ namespace.
Definition mxError.hpp:106
Management of a cublas handle.
void create()
Create (allocate) the handle.
cublasHandle(bool create)
Constructor with option to create / not create the handle.
cublasHandle_t operator()()
Get the handle for use in calls to cublas routines.
cublasHandle()
Default c'tor.