CUDNN Frontend API  8.2.0
cudnn_frontend_utils.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 #include <exception>
25 #include <string>
26 
27 #include "cudnn_backend_base.h"
28 
29 namespace cudnn_frontend {
30 
31 #ifndef NV_CUDNN_DISABLE_EXCEPTION
32 class cudnnException : public std::runtime_error {
33  public:
34  cudnnException(const char *message) throw() : std::runtime_error(message) {}
35  virtual const char *
36  what() const throw() {
37  return std::runtime_error::what();
38  }
39 };
40 #endif
41 
42 static inline void
43 throw_if(std::function<bool()> expr, const char *message) {
44  if (expr()) {
45 #ifndef NV_CUDNN_DISABLE_EXCEPTION
46  throw cudnnException(message);
47 #endif
48  }
49 }
50 static inline void
51 throw_if(bool expr, const char *message) {
52  if (expr) {
53 #ifndef NV_CUDNN_DISABLE_EXCEPTION
54  throw cudnnException(message);
55 #endif
56  }
57 }
58 
59 static inline void
60 set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message) {
61  if (desc != nullptr) {
62  desc->set_status(status);
63  desc->set_error(message);
64  }
65 #ifndef NV_CUDNN_DISABLE_EXCEPTION
66  throw cudnnException(
67  std::string(std::string(message) + std::string(" cudnn_status: ") + std::to_string(status)).c_str());
68 #endif
69 }
70 }
void set_status(cudnnStatus_t const status_) const
Set status of the descriptor.
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:25855
virtual const char * what() const
static void throw_if(std::function< bool()> expr, const char *message)
void set_error(const char *message) const
Set Diagonistic error message.