CUDNN Frontend API 8.3.0

cudnn_backend_base.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #pragma once
24
25 #include <ostream>
26
27 #include <cudnn.h>
28
29 namespace cudnn_frontend {
30
36 class OpaqueBackendPointer {
37 cudnnBackendDescriptor_t m_desc = nullptr;
38 cudnnStatus_t status = CUDNN_STATUS_SUCCESS;
39
40 public:
41 OpaqueBackendPointer(const OpaqueBackendPointer&) = delete;
42 OpaqueBackendPointer&
43 operator=(const OpaqueBackendPointer&) = delete;
44 OpaqueBackendPointer(OpaqueBackendPointer&&) = default;
45
50 OpaqueBackendPointer(cudnnBackendDescriptorType_t type) { status = cudnnBackendCreateDescriptor(type, &m_desc); }
55 ~OpaqueBackendPointer() { cudnnBackendDestroyDescriptor(m_desc); };
61 cudnnBackendDescriptor_t const&
62 get_backend_descriptor() const {
63 return m_desc;
64 }
69 cudnnStatus_t
70 get_status() const {
71 return status;
72 }
77 bool
78 is_good() const {
79 return status == CUDNN_STATUS_SUCCESS;
80 }
81 };
82
84 using ManagedOpaqueDescriptor = std::shared_ptr<OpaqueBackendPointer>;
85
87 static ManagedOpaqueDescriptor
88 make_shared_backend_pointer(cudnnBackendDescriptorType_t type) {
89 return std::make_shared<OpaqueBackendPointer>(type);
90 }
91
99 class BackendDescriptor {
100 public:
102 virtual std::string
103 describe() const = 0;
104
107 cudnnBackendDescriptor_t
108 get_raw_desc() const {
109 return pointer->get_backend_descriptor();
110 }
111
113 cudnnStatus_t
114 get_status() const {
115 return status;
116 }
117
119 void
120 set_status(cudnnStatus_t const status_) const {
121 status = status_;
122 }
123
125 void
126 set_error(const char* message) const {
127 err_msg = message;
128 }
129
131 const char*
132 get_error() const {
133 return err_msg.c_str();
134 }
135
137 ManagedOpaqueDescriptor
138 get_desc() const {
139 return pointer;
140 }
141
143 cudnnStatus_t
144 initialize_managed_backend_pointer(cudnnBackendDescriptorType_t type) {
145 pointer = make_shared_backend_pointer(type);
146 return pointer->get_status();
147 }
148
149 protected:
154 BackendDescriptor(ManagedOpaqueDescriptor pointer_, cudnnStatus_t status_, std::string err_msg_)
155 : pointer(pointer_), status(status_), err_msg(err_msg_) {}
156 BackendDescriptor() = default;
157
158 ManagedOpaqueDescriptor pointer;
159
160 mutable cudnnStatus_t status = CUDNN_STATUS_SUCCESS;
161 mutable std::string err_msg;
162 };
163 }
cudnn_frontend::OpaqueBackendPointer::~OpaqueBackendPointer
~OpaqueBackendPointer()
Definition: cudnn_backend_base.h:55
cudnn_frontend::OpaqueBackendPointer::OpaqueBackendPointer
OpaqueBackendPointer(const OpaqueBackendPointer &)=delete
Delete the copy constructor to prevent bad copies.
cudnn_frontend::BackendDescriptor::set_status
void set_status(cudnnStatus_t const status_) const
Set status of the descriptor.
Definition: cudnn_backend_base.h:120
cudnn_frontend::BackendDescriptor::initialize_managed_backend_pointer
cudnnStatus_t initialize_managed_backend_pointer(cudnnBackendDescriptorType_t type)
Initializes the underlying managed descriptor.
Definition: cudnn_backend_base.h:144
cudnn_frontend::OpaqueBackendPointer
Definition: cudnn_backend_base.h:36
cudnn_frontend::OpaqueBackendPointer::m_desc
cudnnBackendDescriptor_t m_desc
Raw void pointer.
Definition: cudnn_backend_base.h:37
cudnn_frontend::BackendDescriptor
Definition: cudnn_backend_base.h:99
cudnn_frontend::OpaqueBackendPointer::is_good
bool is_good() const
Definition: cudnn_backend_base.h:78
cudnn_frontend::make_shared_backend_pointer
static ManagedOpaqueDescriptor make_shared_backend_pointer(cudnnBackendDescriptorType_t type)
Definition: cudnn_backend_base.h:88
cudnn_frontend::BackendDescriptor::get_raw_desc
cudnnBackendDescriptor_t get_raw_desc() const
Definition: cudnn_backend_base.h:108
cudnn_frontend::BackendDescriptor::get_desc
ManagedOpaqueDescriptor get_desc() const
Returns a copy of underlying managed descriptor.
Definition: cudnn_backend_base.h:138
cudnn_frontend::OpaqueBackendPointer::status
cudnnStatus_t status
status of creation of the Descriptor
Definition: cudnn_backend_base.h:38
cudnn_frontend::OpaqueBackendPointer::operator=
OpaqueBackendPointer & operator=(const OpaqueBackendPointer &)=delete
cudnn_frontend::OpaqueBackendPointer::get_backend_descriptor
cudnnBackendDescriptor_t const & get_backend_descriptor() const
Definition: cudnn_backend_base.h:62
cudnn_frontend::OpaqueBackendPointer::get_status
cudnnStatus_t get_status() const
Definition: cudnn_backend_base.h:70
cudnn_frontend::OpaqueBackendPointer::OpaqueBackendPointer
OpaqueBackendPointer(cudnnBackendDescriptorType_t type)
Definition: cudnn_backend_base.h:50
cudnn_frontend::BackendDescriptor::get_status
cudnnStatus_t get_status() const
Current status of the descriptor.
Definition: cudnn_backend_base.h:114
cudnn_frontend::ManagedOpaqueDescriptor
std::shared_ptr< OpaqueBackendPointer > ManagedOpaqueDescriptor
Definition: cudnn_backend_base.h:84
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::BackendDescriptor::get_error
const char * get_error() const
Diagonistic error message if any.
Definition: cudnn_backend_base.h:132
type
cudnn_frontend::BackendDescriptor::set_error
void set_error(const char *message) const
Set Diagonistic error message.
Definition: cudnn_backend_base.h:126
cudnn_frontend::BackendDescriptor::BackendDescriptor
BackendDescriptor(ManagedOpaqueDescriptor pointer_, cudnnStatus_t status_, std::string err_msg_)
Definition: cudnn_backend_base.h:154
cudnn_frontend::BackendDescriptor::err_msg
std::string err_msg
Error message if any being set.
Definition: cudnn_backend_base.h:161
cudnn_frontend::BackendDescriptor::pointer
ManagedOpaqueDescriptor pointer
Definition: cudnn_backend_base.h:158