Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / gpu / src / error.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 #include "precomp.hpp"
43
44 using namespace cv;
45 using namespace cv::gpu;
46 using namespace std;
47
48 #ifdef HAVE_CUDA
49
50 namespace
51 {
52     #define error_entry(entry)  { entry, #entry }
53
54     struct ErrorEntry
55     {
56         int code;
57         string str;
58     };
59
60     struct ErrorEntryComparer
61     {
62         int code;
63         ErrorEntryComparer(int code_) : code(code_) {};
64         bool operator()(const ErrorEntry& e) const { return e.code == code; }
65     };
66
67     string getErrorString(int code, const ErrorEntry* errors, size_t n)
68     {
69         size_t idx = find_if(errors, errors + n, ErrorEntryComparer(code)) - errors;
70
71         const string& msg = (idx != n) ? errors[idx].str : string("Unknown error code");
72
73         ostringstream ostr;
74         ostr << msg << " [Code = " << code << "]";
75
76         return ostr.str();
77     }
78
79     //////////////////////////////////////////////////////////////////////////
80     // NPP errors
81
82     const ErrorEntry npp_errors [] =
83     {
84 #if defined (_MSC_VER)
85         error_entry( NPP_NOT_SUFFICIENT_COMPUTE_CAPABILITY ),
86 #endif
87
88 #if NPP_VERSION < 5500
89         error_entry( NPP_BAD_ARG_ERROR ),
90         error_entry( NPP_COEFF_ERROR ),
91         error_entry( NPP_RECT_ERROR ),
92         error_entry( NPP_QUAD_ERROR ),
93         error_entry( NPP_MEMFREE_ERR ),
94         error_entry( NPP_MEMSET_ERR ),
95         error_entry( NPP_MEM_ALLOC_ERR ),
96         error_entry( NPP_HISTO_NUMBER_OF_LEVELS_ERROR ),
97         error_entry( NPP_MIRROR_FLIP_ERR ),
98         error_entry( NPP_INVALID_INPUT ),
99         error_entry( NPP_POINTER_ERROR ),
100         error_entry( NPP_WARNING ),
101         error_entry( NPP_ODD_ROI_WARNING ),
102 #else
103         error_entry( NPP_INVALID_HOST_POINTER_ERROR ),
104         error_entry( NPP_INVALID_DEVICE_POINTER_ERROR ),
105         error_entry( NPP_LUT_PALETTE_BITSIZE_ERROR ),
106         error_entry( NPP_ZC_MODE_NOT_SUPPORTED_ERROR ),
107         error_entry( NPP_MEMFREE_ERROR ),
108         error_entry( NPP_MEMSET_ERROR ),
109         error_entry( NPP_QUALITY_INDEX_ERROR ),
110         error_entry( NPP_HISTOGRAM_NUMBER_OF_LEVELS_ERROR ),
111         error_entry( NPP_CHANNEL_ORDER_ERROR ),
112         error_entry( NPP_ZERO_MASK_VALUE_ERROR ),
113         error_entry( NPP_QUADRANGLE_ERROR ),
114         error_entry( NPP_RECTANGLE_ERROR ),
115         error_entry( NPP_COEFFICIENT_ERROR ),
116         error_entry( NPP_NUMBER_OF_CHANNELS_ERROR ),
117         error_entry( NPP_COI_ERROR ),
118         error_entry( NPP_DIVISOR_ERROR ),
119         error_entry( NPP_CHANNEL_ERROR ),
120         error_entry( NPP_STRIDE_ERROR ),
121         error_entry( NPP_ANCHOR_ERROR ),
122         error_entry( NPP_MASK_SIZE_ERROR ),
123         error_entry( NPP_MIRROR_FLIP_ERROR ),
124         error_entry( NPP_MOMENT_00_ZERO_ERROR ),
125         error_entry( NPP_THRESHOLD_NEGATIVE_LEVEL_ERROR ),
126         error_entry( NPP_THRESHOLD_ERROR ),
127         error_entry( NPP_CONTEXT_MATCH_ERROR ),
128         error_entry( NPP_FFT_FLAG_ERROR ),
129         error_entry( NPP_FFT_ORDER_ERROR ),
130         error_entry( NPP_SCALE_RANGE_ERROR ),
131         error_entry( NPP_DATA_TYPE_ERROR ),
132         error_entry( NPP_OUT_OFF_RANGE_ERROR ),
133         error_entry( NPP_DIVIDE_BY_ZERO_ERROR ),
134         error_entry( NPP_MEMORY_ALLOCATION_ERR ),
135         error_entry( NPP_RANGE_ERROR ),
136         error_entry( NPP_BAD_ARGUMENT_ERROR ),
137         error_entry( NPP_NO_MEMORY_ERROR ),
138         error_entry( NPP_ERROR_RESERVED ),
139         error_entry( NPP_NO_OPERATION_WARNING ),
140         error_entry( NPP_DIVIDE_BY_ZERO_WARNING ),
141         error_entry( NPP_WRONG_INTERSECTION_ROI_WARNING ),
142 #endif
143
144         error_entry( NPP_NOT_SUPPORTED_MODE_ERROR ),
145         error_entry( NPP_ROUND_MODE_NOT_SUPPORTED_ERROR ),
146         error_entry( NPP_RESIZE_NO_OPERATION_ERROR ),
147         error_entry( NPP_LUT_NUMBER_OF_LEVELS_ERROR ),
148         error_entry( NPP_TEXTURE_BIND_ERROR ),
149         error_entry( NPP_WRONG_INTERSECTION_ROI_ERROR ),
150         error_entry( NPP_NOT_EVEN_STEP_ERROR ),
151         error_entry( NPP_INTERPOLATION_ERROR ),
152         error_entry( NPP_RESIZE_FACTOR_ERROR ),
153         error_entry( NPP_HAAR_CLASSIFIER_PIXEL_MATCH_ERROR ),
154         error_entry( NPP_MEMCPY_ERROR ),
155         error_entry( NPP_ALIGNMENT_ERROR ),
156         error_entry( NPP_STEP_ERROR ),
157         error_entry( NPP_SIZE_ERROR ),
158         error_entry( NPP_NULL_POINTER_ERROR ),
159         error_entry( NPP_CUDA_KERNEL_EXECUTION_ERROR ),
160         error_entry( NPP_NOT_IMPLEMENTED_ERROR ),
161         error_entry( NPP_ERROR ),
162         error_entry( NPP_NO_ERROR ),
163         error_entry( NPP_SUCCESS ),
164         error_entry( NPP_WRONG_INTERSECTION_QUAD_WARNING ),
165         error_entry( NPP_MISALIGNED_DST_ROI_WARNING ),
166         error_entry( NPP_AFFINE_QUAD_INCORRECT_WARNING ),
167         error_entry( NPP_DOUBLE_SIZE_WARNING )
168     };
169
170     const size_t npp_error_num = sizeof(npp_errors) / sizeof(npp_errors[0]);
171
172     //////////////////////////////////////////////////////////////////////////
173     // NCV errors
174
175     const ErrorEntry ncv_errors [] =
176     {
177         error_entry( NCV_SUCCESS ),
178         error_entry( NCV_UNKNOWN_ERROR ),
179         error_entry( NCV_CUDA_ERROR ),
180         error_entry( NCV_NPP_ERROR ),
181         error_entry( NCV_FILE_ERROR ),
182         error_entry( NCV_NULL_PTR ),
183         error_entry( NCV_INCONSISTENT_INPUT ),
184         error_entry( NCV_TEXTURE_BIND_ERROR ),
185         error_entry( NCV_DIMENSIONS_INVALID ),
186         error_entry( NCV_INVALID_ROI ),
187         error_entry( NCV_INVALID_STEP ),
188         error_entry( NCV_INVALID_SCALE ),
189         error_entry( NCV_INVALID_SCALE ),
190         error_entry( NCV_ALLOCATOR_NOT_INITIALIZED ),
191         error_entry( NCV_ALLOCATOR_BAD_ALLOC ),
192         error_entry( NCV_ALLOCATOR_BAD_DEALLOC ),
193         error_entry( NCV_ALLOCATOR_INSUFFICIENT_CAPACITY ),
194         error_entry( NCV_ALLOCATOR_DEALLOC_ORDER ),
195         error_entry( NCV_ALLOCATOR_BAD_REUSE ),
196         error_entry( NCV_MEM_COPY_ERROR ),
197         error_entry( NCV_MEM_RESIDENCE_ERROR ),
198         error_entry( NCV_MEM_INSUFFICIENT_CAPACITY ),
199         error_entry( NCV_HAAR_INVALID_PIXEL_STEP ),
200         error_entry( NCV_HAAR_TOO_MANY_FEATURES_IN_CLASSIFIER ),
201         error_entry( NCV_HAAR_TOO_MANY_FEATURES_IN_CASCADE ),
202         error_entry( NCV_HAAR_TOO_LARGE_FEATURES ),
203         error_entry( NCV_HAAR_XML_LOADING_EXCEPTION ),
204         error_entry( NCV_NOIMPL_HAAR_TILTED_FEATURES ),
205         error_entry( NCV_WARNING_HAAR_DETECTIONS_VECTOR_OVERFLOW ),
206         error_entry( NPPST_SUCCESS ),
207         error_entry( NPPST_ERROR ),
208         error_entry( NPPST_CUDA_KERNEL_EXECUTION_ERROR ),
209         error_entry( NPPST_NULL_POINTER_ERROR ),
210         error_entry( NPPST_TEXTURE_BIND_ERROR ),
211         error_entry( NPPST_MEMCPY_ERROR ),
212         error_entry( NPPST_MEM_ALLOC_ERR ),
213         error_entry( NPPST_MEMFREE_ERR ),
214         error_entry( NPPST_INVALID_ROI ),
215         error_entry( NPPST_INVALID_STEP ),
216         error_entry( NPPST_INVALID_SCALE ),
217         error_entry( NPPST_MEM_INSUFFICIENT_BUFFER ),
218         error_entry( NPPST_MEM_RESIDENCE_ERROR ),
219         error_entry( NPPST_MEM_INTERNAL_ERROR )
220     };
221
222     const size_t ncv_error_num = sizeof(ncv_errors) / sizeof(ncv_errors[0]);
223
224     //////////////////////////////////////////////////////////////////////////
225     // CUFFT errors
226
227 #ifdef HAVE_CUFFT
228     const ErrorEntry cufft_errors[] =
229     {
230         error_entry( CUFFT_INVALID_PLAN ),
231         error_entry( CUFFT_ALLOC_FAILED ),
232         error_entry( CUFFT_INVALID_TYPE ),
233         error_entry( CUFFT_INVALID_VALUE ),
234         error_entry( CUFFT_INTERNAL_ERROR ),
235         error_entry( CUFFT_EXEC_FAILED ),
236         error_entry( CUFFT_SETUP_FAILED ),
237         error_entry( CUFFT_INVALID_SIZE ),
238         error_entry( CUFFT_UNALIGNED_DATA )
239     };
240
241     const int cufft_error_num = sizeof(cufft_errors) / sizeof(cufft_errors[0]);
242 #endif
243
244     //////////////////////////////////////////////////////////////////////////
245     // CUBLAS errors
246
247 #ifdef HAVE_CUBLAS
248     const ErrorEntry cublas_errors[] =
249     {
250         error_entry( CUBLAS_STATUS_SUCCESS ),
251         error_entry( CUBLAS_STATUS_NOT_INITIALIZED ),
252         error_entry( CUBLAS_STATUS_ALLOC_FAILED ),
253         error_entry( CUBLAS_STATUS_INVALID_VALUE ),
254         error_entry( CUBLAS_STATUS_ARCH_MISMATCH ),
255         error_entry( CUBLAS_STATUS_MAPPING_ERROR ),
256         error_entry( CUBLAS_STATUS_EXECUTION_FAILED ),
257         error_entry( CUBLAS_STATUS_INTERNAL_ERROR )
258     };
259
260     const int cublas_error_num = sizeof(cublas_errors) / sizeof(cublas_errors[0]);
261 #endif
262 }
263
264 namespace cv
265 {
266     namespace gpu
267     {
268         void nppError(int code, const char *file, const int line, const char *func)
269         {
270             string msg = getErrorString(code, npp_errors, npp_error_num);
271             cv::gpu::error(msg.c_str(), file, line, func);
272         }
273
274         void ncvError(int code, const char *file, const int line, const char *func)
275         {
276             string msg = getErrorString(code, ncv_errors, ncv_error_num);
277             cv::gpu::error(msg.c_str(), file, line, func);
278         }
279
280 #ifdef HAVE_CUFFT
281         void cufftError(int code, const char *file, const int line, const char *func)
282         {
283             string msg = getErrorString(code, cufft_errors, cufft_error_num);
284             cv::gpu::error(msg.c_str(), file, line, func);
285         }
286 #endif
287
288 #ifdef HAVE_CUBLAS
289         void cublasError(int code, const char *file, const int line, const char *func)
290         {
291             string msg = getErrorString(code, cublas_errors, cublas_error_num);
292             cv::gpu::error(msg.c_str(), file, line, func);
293         }
294 #endif
295     }
296 }
297
298 #endif