@@ -131,68 +131,72 @@ struct EOFException : public std::exception {
131131#define LIKELY (condition ) (condition)
132132#endif
133133
134+ inline bool is_error (bool stat) { return !stat; }
135+
134136template <typename ... Args>
135137inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
136138 bool stat, const Args&... args) {
137- if (UNLIKELY (!(stat))) {
138139#ifndef REPLACE_ENFORCE_GLOG
139- throw std::runtime_error (string::Sprintf (args...));
140+ throw std::runtime_error (string::Sprintf (args...));
140141#else
141- LOG (FATAL) << string::Sprintf (args...);
142+ LOG (FATAL) << string::Sprintf (args...);
142143#endif
143- }
144144}
145145
146146#ifdef PADDLE_WITH_CUDA
147147
148+ inline bool is_error (cudaError_t e) { return UNLIKELY (e); }
149+
148150template <typename ... Args>
149151inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
150152 cudaError_t e, const Args&... args) {
151- if (UNLIKELY (e)) {
152153#ifndef REPLACE_ENFORCE_GLOG
153- throw thrust::system_error (e, thrust::cuda_category (),
154- string::Sprintf (args...));
154+ throw thrust::system_error (e, thrust::cuda_category (),
155+ string::Sprintf (args...));
155156#else
156- LOG (FATAL) << string::Sprintf (args...);
157+ LOG (FATAL) << string::Sprintf (args...);
157158#endif
158- }
159+ }
160+
161+ inline bool is_error (curandStatus_t stat) {
162+ return stat != CURAND_STATUS_SUCCESS;
159163}
160164
161165template <typename ... Args>
162166inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
163167 curandStatus_t stat, const Args&... args) {
164- if (stat != CURAND_STATUS_SUCCESS) {
165168#ifndef REPLACE_ENFORCE_GLOG
166- throw thrust::system_error (cudaErrorLaunchFailure, thrust::cuda_category (),
167- string::Sprintf (args...));
169+ throw thrust::system_error (cudaErrorLaunchFailure, thrust::cuda_category (),
170+ string::Sprintf (args...));
168171#else
169- LOG (FATAL) << string::Sprintf (args...);
172+ LOG (FATAL) << string::Sprintf (args...);
170173#endif
171- }
174+ }
175+
176+ inline bool is_error (cudnnStatus_t stat) {
177+ return stat != CUDNN_STATUS_SUCCESS;
172178}
173179
174180template <typename ... Args>
175181inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
176182 cudnnStatus_t stat, const Args&... args) {
177- if (stat == CUDNN_STATUS_SUCCESS) {
178- return ;
179- } else {
180183#ifndef REPLACE_ENFORCE_GLOG
181- throw std::runtime_error (platform::dynload::cudnnGetErrorString (stat) +
182- string::Sprintf (args...));
184+ throw std::runtime_error (platform::dynload::cudnnGetErrorString (stat) +
185+ string::Sprintf (args...));
183186#else
184- LOG (FATAL) << string::Sprintf (args...);
187+ LOG (FATAL) << string::Sprintf (args...);
185188#endif
186- }
189+ }
190+
191+ inline bool is_error (cublasStatus_t stat) {
192+ return stat != CUBLAS_STATUS_SUCCESS;
187193}
188194
189195template <typename ... Args>
190196inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
191197 cublasStatus_t stat, const Args&... args) {
192198 std::string err;
193- if (stat == CUBLAS_STATUS_SUCCESS) {
194- return ;
195- } else if (stat == CUBLAS_STATUS_NOT_INITIALIZED) {
199+ if (stat == CUBLAS_STATUS_NOT_INITIALIZED) {
196200 err = " CUBLAS: not initialized, " ;
197201 } else if (stat == CUBLAS_STATUS_ALLOC_FAILED) {
198202 err = " CUBLAS: alloc failed, " ;
@@ -219,20 +223,18 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
219223}
220224
221225#if !defined(__APPLE__) && !defined(_WIN32)
226+ inline bool is_error (ncclResult_t stat) { return stat != ncclSuccess; }
227+
222228template <typename ... Args>
223229inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
224230 ncclResult_t stat, const Args&... args) {
225- if (stat == ncclSuccess) {
226- return ;
227- } else {
228231#ifndef REPLACE_ENFORCE_GLOG
229- throw std::runtime_error (platform::dynload::ncclGetErrorString (stat) +
230- string::Sprintf (args...));
232+ throw std::runtime_error (platform::dynload::ncclGetErrorString (stat) +
233+ string::Sprintf (args...));
231234#else
232- LOG (FATAL) << platform::dynload::ncclGetErrorString (stat)
233- << string::Sprintf (args...);
235+ LOG (FATAL) << platform::dynload::ncclGetErrorString (stat)
236+ << string::Sprintf (args...);
234237#endif
235- }
236238}
237239#endif // __APPLE__ and windows
238240#endif // PADDLE_WITH_CUDA
@@ -250,21 +252,49 @@ inline void throw_on_error(T e) {
250252 __FILE__, __LINE__); \
251253 } while (false )
252254
255+ #define __PADDLE_THROW_ERROR_I (_, _9, _8, _7, _6, _5, _4, _3, _2, X_, ...) X_;
256+
257+ #define __THROW_ON_ERROR_ONE_ARG (COND, ARG ) \
258+ ::paddle::platform::throw_on_error (COND, ::paddle::string::Sprintf(ARG));
259+
260+ #define __PADDLE_THROW_ON_ERROR (COND, ...) \
261+ __PADDLE_THROW_ERROR_I ( \
262+ __VA_ARGS__, ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
263+ ::paddle::platform::throw_on_error (COND, __VA_ARGS__), \
264+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
265+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
266+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
267+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
268+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
269+ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
270+ __THROW_ON_ERROR_ONE_ARG(COND, __VA_ARGS__))
271+
272+ #define __PADDLE_UNARY_COMPARE (COND, ...) \
273+ do { \
274+ auto __cond = COND; \
275+ if (UNLIKELY (::paddle::platform::is_error (__cond))) { \
276+ __PADDLE_THROW_ON_ERROR (__cond, __VA_ARGS__); \
277+ } \
278+ } while (0 )
279+
253280#ifndef REPLACE_ENFORCE_GLOG
254- #define PADDLE_ENFORCE ( ...) \
281+ #define __PADDLE_ENFORCE_I (COND, ...) \
255282 do { \
256283 try { \
257- ::paddle::platform::throw_on_error ( __VA_ARGS__); \
284+ __PADDLE_UNARY_COMPARE (COND, __VA_ARGS__); \
258285 } catch (...) { \
259286 throw ::paddle::platform::EnforceNotMet (std::current_exception (), \
260287 __FILE__, __LINE__); \
261288 } \
262- } while (false )
289+ } while (0 )
263290
264291#else
265- #define PADDLE_ENFORCE ( ...) ::paddle::platform::throw_on_error( __VA_ARGS__);
292+ #define __PADDLE_ENFORCE_I (COND, ...) __PADDLE_UNARY_COMPARE(COND, __VA_ARGS__);
266293#endif // REPLACE_ENFORCE_GLOG
267294
295+ #define __PADDLE_ENFORCE (__args ) __PADDLE_ENFORCE_I __args
296+ #define PADDLE_ENFORCE (...) __PADDLE_ENFORCE((__VA_ARGS__))
297+
268298#define PADDLE_THROW_EOF () \
269299 do { \
270300 throw ::paddle::platform::EOFException (" There is no next data." , __FILE__, \
0 commit comments