NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
CudaHelpers.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_CUDAHELPERS_H
27 #define NVNEURAL_CUDAHELPERS_H
28 
29 #include <nvneural/CudaTypes.h>
30 #include <nvneural/CoreHelpers.h>
31 #include <cassert>
32 #include <cuda_runtime_api.h>
33 
34 namespace nvneural {
35 
39 const size_t STANDARD_CUDA_BLOCK_SIZE = 32;
40 
46 inline NeuralResult CheckCudartErrors_Impl(cudaError_t status, const char file[], int line)
47 {
48  if (status != cudaSuccess)
49  {
50  DefaultLogger()->logError(0, "CUDA error: %s (error %d)", cudaGetErrorString(status), status);
51  DefaultLogger()->logError(1, "Location: %s, line %d", file, line);
52  if ((status == cudaErrorInsufficientDriver) ||
53  (status == cudaErrorInitializationError))
54  {
55  DefaultLogger()->logError(0, "Driver is not supported");
56  }
57 
58  return NeuralResult::Failure;
59  }
60 
61  return NeuralResult::Success;
62 }
63 
70 #define CheckCudartErrors(status_) \
71  do \
72  { \
73  const auto localResult = nvneural::CheckCudartErrors_Impl((status_), __FILE__, __LINE__); \
74  if (failed(localResult)) \
75  { \
76  return localResult; \
77  } \
78  } while (false)
79 
85 inline NeuralResult CheckCudaDriverErrors_Impl(CUresult status, const char file[], int line)
86 {
87  if (status != CUDA_SUCCESS)
88  {
89  DefaultLogger()->logError(0, "(CU)DA error: %d", status);
90  DefaultLogger()->logError(1, "Location: %s, line %d", file, line);
91 
92  return NeuralResult::Failure;
93  }
94 
95  return NeuralResult::Success;
96 }
97 
104 #define CheckCudaDriverErrors(status_) \
105  do \
106  { \
107  const auto localResult = nvneural::CheckCudaDriverErrors_Impl((status_), __FILE__, __LINE__); \
108  if (failed(localResult)) \
109  { \
110  return localResult; \
111  } \
112  } while (false)
113 
114 } // namespace nvneural
115 
116 #endif // NVNEURAL_CUDAHELPERS_H
Common helper classes and template function implementations.
ILogger * DefaultLogger()
Returns a pointer to the default logger for this module.
Definition: Logging.cpp:38
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
const size_t STANDARD_CUDA_BLOCK_SIZE
A commonly used Cuda Thread Block size.
Definition: CudaHelpers.h:39
NeuralResult CheckCudaDriverErrors_Impl(CUresult status, const char file[], int line)
Implementation function for CheckCudaDriverErrors.
Definition: CudaHelpers.h:85
NeuralResult CheckCudartErrors_Impl(cudaError_t status, const char file[], int line)
Implementation function for CheckCudartErrors.
Definition: CudaHelpers.h:46
All CUDA-specific NvNeural types are declared here.
virtual NeuralResult logError(VerbosityLevel verbosity, const char *format,...) noexcept=0
Logs an error message.