Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
cuda_errorhandling.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef SKA_CHEETAH_CUDA_UTILS_ERRORHANDLING_H
25 #define SKA_CHEETAH_CUDA_UTILS_ERRORHANDLING_H
26 
27 #ifdef ENABLE_CUDA
28 #include <cuda.h>
29 #include <cuda_runtime.h>
30 #include <cufft.h>
31 #include <sstream>
32 #include <stdexcept>
33 
39 //#include "helper_cuda.h" //remove this dependency
40 
44 #define CU_MSG "[ cuda msg ] "
45 
53 #define CUDA_ERROR_CHECK(ans) { cuda_assert_success((ans), __FILE__, __LINE__); }
54 
59 //inline void cuda_assert_success(cudaError_t code, const char *file, int line)
60 inline void cuda_assert_success(cudaError_t code, const char *file, int line)
61 {
62  if (code != cudaSuccess)
63  {
64  /* Ewan note 28/07/2015:
65  * This stringstream needs to be made safe.
66  * Error message formatting needs to be defined.
67  */
68  std::stringstream error_msg;
69  error_msg << "CUDA failed with error: "
70  << cudaGetErrorString(code) << std::endl
71  << "File: " << file << std::endl
72  << "Line: " << line << std::endl;
73  throw std::runtime_error(error_msg.str());
74  }
75 }
76 
80 #define CUFFT_ERROR_CHECK(ans) { cufft_assert_success((ans), __FILE__, __LINE__); }
81 
86 inline void cufft_assert_success(cufftResult code, const char *file, int line)
87 {
88  if (code != CUFFT_SUCCESS)
89  {
90  std::stringstream error_msg;
91  error_msg << "CUFFT failed with error: ";
92  switch (code)
93  {
94  case CUFFT_INVALID_PLAN:
95  error_msg << "CUFFT_INVALID_PLAN";
96  break;
97 
98  case CUFFT_ALLOC_FAILED:
99  error_msg << "CUFFT_ALLOC_FAILED";
100  break;
101 
102  case CUFFT_INVALID_TYPE:
103  error_msg << "CUFFT_INVALID_TYPE";
104  break;
105 
106  case CUFFT_INVALID_VALUE:
107  error_msg << "CUFFT_INVALID_VALUE";
108  break;
109 
110  case CUFFT_INTERNAL_ERROR:
111  error_msg << "CUFFT_INTERNAL_ERROR";
112  break;
113 
114  case CUFFT_EXEC_FAILED:
115  error_msg << "CUFFT_EXEC_FAILED";
116  break;
117 
118  case CUFFT_SETUP_FAILED:
119  error_msg << "CUFFT_SETUP_FAILED";
120  break;
121 
122  case CUFFT_INVALID_SIZE:
123  error_msg << "CUFFT_INVALID_SIZE";
124  break;
125 
126  case CUFFT_UNALIGNED_DATA:
127  error_msg << "CUFFT_UNALIGNED_DATA";
128  break;
129 
130  default:
131  error_msg << "CUFFT_UNKNOWN_ERROR";
132  }
133  error_msg << std::endl
134  << "File: " << file << std::endl
135  << "Line: " << line << std::endl;
136  throw std::runtime_error(error_msg.str());
137  }
138 }
139 #endif // ENABLE_CUDA
140 
141 #endif //SKA_CHEETAH_CUDA_UTILS_ERRORHANDLING_H