Commit c007740b authored by Chris Sullivan's avatar Chris Sullivan Committed by Robert Kimball

Add no-throw error checks (#1264)

* Broadcast and Pad bug fix.

* Added NO_THROW version of the cuda error checking defines. Now utilizing these in dtors.

This reverts commit 68d9d6eafb1475c83c47229ab3c784c3d392ddbd.

* Revert "Broadcast and Pad bug fix."

This reverts commit 099c79792a2e7b9b8727b48de90f623953691f4c.
parent cd5fe431
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#pragma once
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdint.h>
#include <string>
#include <cublas_v2.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cudnn.h>
#include <nvrtc.h>
//why use "do...while.."
//https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros
#define NVRTC_SAFE_CALL_NO_THROW(x) \
do \
{ \
nvrtcResult result = x; \
if (result != NVRTC_SUCCESS) \
{ \
std::cout << "\nerror: " #x " failed with error " \
<< std::string(nvrtcGetErrorString(result)) << std::endl; \
} \
} while (0)
#define NVRTC_SAFE_CALL(x) \
do \
{ \
nvrtcResult result = x; \
if (result != NVRTC_SUCCESS) \
{ \
throw std::runtime_error("\nerror: " #x " failed with error " + \
std::string(nvrtcGetErrorString(result))); \
} \
} while (0)
#define CUDA_SAFE_CALL_NO_THROW(x) \
do \
{ \
CUresult result = x; \
if (result != CUDA_SUCCESS) \
{ \
const char* msg; \
cuGetErrorName(result, &msg); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
std::cout << safe_call_ss.str() << std::endl; \
} \
} while (0)
#define CUDA_SAFE_CALL(x) \
do \
{ \
CUresult result = x; \
if (result != CUDA_SUCCESS) \
{ \
const char* msg; \
cuGetErrorName(result, &msg); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUDA_RT_SAFE_CALL_NO_THROW(x) \
do \
{ \
cudaError_t err = x; \
if (cudaSuccess != err) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ \
<< "\nmsg: " << cudaGetErrorString(err); \
std::cout << safe_call_ss.str() << std::endl; \
} \
} while (0)
#define CUDA_RT_SAFE_CALL(x) \
do \
{ \
cudaError_t err = x; \
if (cudaSuccess != err) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ \
<< "\nmsg: " << cudaGetErrorString(err); \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUDNN_SAFE_CALL_NO_THROW(func) \
do \
{ \
cudnnStatus_t e = (func); \
if (e != CUDNN_STATUS_SUCCESS) \
{ \
auto msg = cudnnGetErrorString(e); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
std::cout << safe_call_ss.str() << std::endl; \
} \
} while (0)
#define CUDNN_SAFE_CALL(func) \
do \
{ \
cudnnStatus_t e = (func); \
if (e != CUDNN_STATUS_SUCCESS) \
{ \
auto msg = cudnnGetErrorString(e); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUBLAS_SAFE_CALL_NO_THROW(func) \
do \
{ \
cublasStatus_t e = (func); \
if (e != CUBLAS_STATUS_SUCCESS) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << e; \
std::cout << safe_call_ss.str() << std::endl; \
} \
} while (0)
#define CUBLAS_SAFE_CALL(func) \
do \
{ \
cublasStatus_t e = (func); \
if (e != CUBLAS_STATUS_SUCCESS) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << e; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
...@@ -79,7 +79,7 @@ namespace ngraph ...@@ -79,7 +79,7 @@ namespace ngraph
} }
static void destroy(cudnnLRNDescriptor_t& desc) static void destroy(cudnnLRNDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyLRNDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyLRNDescriptor(desc));
} }
}; };
...@@ -92,7 +92,7 @@ namespace ngraph ...@@ -92,7 +92,7 @@ namespace ngraph
} }
static void destroy(cudnnTensorDescriptor_t& desc) static void destroy(cudnnTensorDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyTensorDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyTensorDescriptor(desc));
} }
}; };
...@@ -105,7 +105,7 @@ namespace ngraph ...@@ -105,7 +105,7 @@ namespace ngraph
} }
static void destroy(cudnnSpatialTransformerDescriptor_t& desc) static void destroy(cudnnSpatialTransformerDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroySpatialTransformerDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroySpatialTransformerDescriptor(desc));
} }
}; };
...@@ -118,7 +118,7 @@ namespace ngraph ...@@ -118,7 +118,7 @@ namespace ngraph
} }
static void destroy(cudnnReduceTensorDescriptor_t& desc) static void destroy(cudnnReduceTensorDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyReduceTensorDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyReduceTensorDescriptor(desc));
} }
}; };
...@@ -131,7 +131,7 @@ namespace ngraph ...@@ -131,7 +131,7 @@ namespace ngraph
} }
static void destroy(cudnnRNNDescriptor_t& desc) static void destroy(cudnnRNNDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyRNNDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyRNNDescriptor(desc));
} }
}; };
...@@ -144,7 +144,7 @@ namespace ngraph ...@@ -144,7 +144,7 @@ namespace ngraph
} }
static void destroy(cudnnPoolingDescriptor_t& desc) static void destroy(cudnnPoolingDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyPoolingDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyPoolingDescriptor(desc));
} }
}; };
...@@ -157,7 +157,7 @@ namespace ngraph ...@@ -157,7 +157,7 @@ namespace ngraph
} }
static void destroy(cudnnOpTensorDescriptor_t& desc) static void destroy(cudnnOpTensorDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyOpTensorDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyOpTensorDescriptor(desc));
} }
}; };
...@@ -170,7 +170,7 @@ namespace ngraph ...@@ -170,7 +170,7 @@ namespace ngraph
} }
static void destroy(cudnnFilterDescriptor_t& desc) static void destroy(cudnnFilterDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyFilterDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyFilterDescriptor(desc));
} }
}; };
...@@ -183,7 +183,7 @@ namespace ngraph ...@@ -183,7 +183,7 @@ namespace ngraph
} }
static void destroy(cudnnDropoutDescriptor_t& desc) static void destroy(cudnnDropoutDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyDropoutDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyDropoutDescriptor(desc));
} }
}; };
...@@ -196,7 +196,7 @@ namespace ngraph ...@@ -196,7 +196,7 @@ namespace ngraph
} }
static void destroy(cudnnConvolutionDescriptor_t& desc) static void destroy(cudnnConvolutionDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyConvolutionDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyConvolutionDescriptor(desc));
} }
}; };
...@@ -209,7 +209,7 @@ namespace ngraph ...@@ -209,7 +209,7 @@ namespace ngraph
} }
static void destroy(cudnnCTCLossDescriptor_t& desc) static void destroy(cudnnCTCLossDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyCTCLossDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyCTCLossDescriptor(desc));
} }
}; };
...@@ -222,7 +222,7 @@ namespace ngraph ...@@ -222,7 +222,7 @@ namespace ngraph
} }
static void destroy(cudnnActivationDescriptor_t& desc) static void destroy(cudnnActivationDescriptor_t& desc)
{ {
CUDNN_SAFE_CALL(cudnnDestroyActivationDescriptor(desc)); CUDNN_SAFE_CALL_NO_THROW(cudnnDestroyActivationDescriptor(desc));
} }
}; };
} }
......
...@@ -36,5 +36,5 @@ runtime::gpu::CudaContextManager::CudaContextManager() ...@@ -36,5 +36,5 @@ runtime::gpu::CudaContextManager::CudaContextManager()
runtime::gpu::CudaContextManager::~CudaContextManager() runtime::gpu::CudaContextManager::~CudaContextManager()
{ {
CUDA_SAFE_CALL(cuDevicePrimaryCtxRelease(m_device)); CUDA_SAFE_CALL_NO_THROW(cuDevicePrimaryCtxRelease(m_device));
} }
...@@ -59,7 +59,7 @@ runtime::gpu::GPU_TensorView::~GPU_TensorView() ...@@ -59,7 +59,7 @@ runtime::gpu::GPU_TensorView::~GPU_TensorView()
{ {
if (!m_custom_memory && (m_allocated_buffer_pool != nullptr)) if (!m_custom_memory && (m_allocated_buffer_pool != nullptr))
{ {
CUDA_RT_SAFE_CALL(cudaFree(m_allocated_buffer_pool)); CUDA_RT_SAFE_CALL_NO_THROW(cudaFree(m_allocated_buffer_pool));
} }
} }
......
...@@ -18,89 +18,13 @@ ...@@ -18,89 +18,13 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <sstream>
#include <stdexcept>
#include <stdint.h>
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include <cublas_v2.h> #include "ngraph/runtime/gpu/cuda_error_check.hpp"
#include <cuda.h>
#include <cuda_runtime.h>
#include <cudnn.h>
#include <nvrtc.h>
#include "ngraph/util.hpp" #include "ngraph/util.hpp"
//why use "do...while.."
//https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros
#define NVRTC_SAFE_CALL(x) \
do \
{ \
nvrtcResult result = x; \
if (result != NVRTC_SUCCESS) \
{ \
throw std::runtime_error("\nerror: " #x " failed with error " + \
std::string(nvrtcGetErrorString(result))); \
} \
} while (0)
#define CUDA_SAFE_CALL(x) \
do \
{ \
CUresult result = x; \
if (result != CUDA_SUCCESS) \
{ \
const char* msg; \
cuGetErrorName(result, &msg); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUDA_RT_SAFE_CALL(x) \
do \
{ \
cudaError_t err = x; \
if (cudaSuccess != err) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #x " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ \
<< "\nmsg: " << cudaGetErrorString(err); \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUDNN_SAFE_CALL(func) \
do \
{ \
cudnnStatus_t e = (func); \
if (e != CUDNN_STATUS_SUCCESS) \
{ \
auto msg = cudnnGetErrorString(e); \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << msg; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
#define CUBLAS_SAFE_CALL(func) \
do \
{ \
cublasStatus_t e = (func); \
if (e != CUBLAS_STATUS_SUCCESS) \
{ \
std::stringstream safe_call_ss; \
safe_call_ss << "\nerror: " #func " failed with error" \
<< "\nfile: " << __FILE__ << "\nline: " << __LINE__ << "\nmsg: " << e; \
throw std::runtime_error(safe_call_ss.str()); \
} \
} while (0)
namespace ngraph namespace ngraph
{ {
namespace runtime namespace runtime
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment