Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tnl-dev
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
TNL
tnl-dev
Commits
9c3af95c
There was an error fetching the commit references. Please try again later.
Commit
9c3af95c
authored
7 years ago
by
Jakub Klinkovský
Browse files
Options
Downloads
Patches
Plain Diff
Revised assert macros for Python bindings
parent
cb838ebd
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/TNL/Assert.h
+120
-40
120 additions, 40 deletions
src/TNL/Assert.h
with
120 additions
and
40 deletions
src/TNL/Assert.h
+
120
−
40
View file @
9c3af95c
...
...
@@ -2,7 +2,7 @@
Assert.h - description
-------------------
begin : Jan 12, 2010
copyright : (C) 2013 by Tomas Oberhuber
copyright : (C) 2013 by Tomas Oberhuber
et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
...
...
@@ -10,21 +10,90 @@
#pragma once
#include
<TNL/Devices/CudaCallable.h>
/****
* Debugging assert
* The purpose of this file is to define the TNL_ASSERT_* debugging macros as
* shown below.
*
* If the 'NDEBUG' macro is defined, the build is considered to be optimized
* and all assert macros are empty. Otherwise, the conditions are checked and
* failures lead to the diagnostics message being printed to std::cerr and
* program abortion (via 'throw EXIT_FAILURE' statement).
*
* For the purpose of providing Python bindings it is possible to change the
* reporting behaviour by defining the TNL_THROW_ASSERTION_ERROR macro, which
* leads to throwing the ::TNL::Assert::AssertionError holding the error
* message (which is not printed in this case). The AssertionError class does
* not inherit from std::exception to avoid being caught by normal exception
* handlers, but the code for Python bindings can use it to translate it to the
* Python's AssertionError exception.
*
* Implemented by: Jakub Klinkovsky
*/
#ifndef NDEBUG
#ifdef NDEBUG
// empty macros for optimized build
#define TNL_ASSERT_TRUE( val, msg )
#define TNL_ASSERT_FALSE( val, msg )
#define TNL_ASSERT_EQ( val1, val2, msg )
#define TNL_ASSERT_NE( val1, val2, msg )
#define TNL_ASSERT_LE( val1, val2, msg )
#define TNL_ASSERT_LT( val1, val2, msg )
#define TNL_ASSERT_GE( val1, val2, msg )
#define TNL_ASSERT_GT( val1, val2, msg )
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command )
#else
/* #ifdef NDEBUG */
#include
<sstream>
#include
<iostream>
#include
<stdio.h>
#include
<TNL/Devices/CudaCallable.h>
namespace
TNL
{
namespace
Assert
{
#ifdef TNL_THROW_ASSERTION_ERROR
// This will be used by the code for Python bindings to translate assertion
// failures to the Python's AssertionError exception.
class
AssertionError
{
public:
AssertionError
(
const
std
::
string
&
msg
)
:
msg
(
msg
)
{}
const
char
*
what
()
const
{
return
msg
.
c_str
();
}
private
:
std
::
string
msg
;
};
inline
void
printDiagnosticsHost
(
const
char
*
assertion
,
const
char
*
message
,
const
char
*
file
,
const
char
*
function
,
int
line
,
const
char
*
diagnostics
)
{
std
::
stringstream
str
;
str
<<
"Assertion '"
<<
assertion
<<
"' failed !!!
\n
"
<<
"Message: "
<<
message
<<
"
\n
"
<<
"File: "
<<
file
<<
"
\n
"
<<
"Function: "
<<
function
<<
"
\n
"
<<
"Line: "
<<
line
<<
"
\n
"
<<
"Diagnostics:
\n
"
<<
diagnostics
<<
std
::
endl
;
throw
AssertionError
(
str
.
str
()
);
}
#else // TNL_THROW_ASSERTION_ERROR
// This will be used in regular C++ code
inline
void
printDiagnosticsHost
(
const
char
*
assertion
,
const
char
*
message
,
...
...
@@ -40,6 +109,7 @@ printDiagnosticsHost( const char* assertion,
<<
"Line: "
<<
line
<<
"
\n
"
<<
"Diagnostics:
\n
"
<<
diagnostics
<<
std
::
endl
;
}
#endif // TNL_THROW_ASSERTION_ERROR
__cuda_callable__
inline
void
...
...
@@ -247,46 +317,56 @@ TNL_IMPL_CMP_HELPER_( GT, > );
/****
* Original assert macro with custom command for diagnostic.
* Original assert macro with custom command for diagnostic
s
.
*/
// __CUDA_ARCH__ is defined by the compiler only for code executed on GPU
#ifdef __CUDA_ARCH__
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command ) \
if( ! ( ___tnl__assert_condition ) ) \
{ \
printf( "Assertion '%s' failed !!! \n File: %s \n Line: %d \n Diagnostics: Not supported with CUDA.\n", \
__STRING( ___tnl__assert_condition ), \
__FILE__, \
__LINE__ ); \
asm("trap;"); \
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command )
\
if( ! ( ___tnl__assert_condition ) )
\
{
\
printf( "Assertion '%s' failed !!! \n File: %s \n Line: %d \n Diagnostics: Not supported with CUDA.\n",
\
__STRING( ___tnl__assert_condition ),
\
__FILE__,
\
__LINE__ );
\
asm("trap;");
\
}
#else // __CUDA_ARCH__
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command ) \
if( ! ( ___tnl__assert_condition ) ) \
{ \
std::cerr << "Assertion '" << __STRING( ___tnl__assert_condition ) << "' failed !!!" << std::endl \
<< "File: " << __FILE__ << std::endl \
<< "Function: " << __TNL_PRETTY_FUNCTION << std::endl \
<< "Line: " << __LINE__ << std::endl \
<< "Diagnostics: "; \
___tnl__assert_command; \
throw EXIT_FAILURE; \
#else // #ifdef __CUDA_ARCH__
#ifdef TNL_THROW_ASSERTION_ERROR
// This will be used by the code for Python bindings to translate assertion
// failures to the Python's AssertionError exception.
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command ) \
if( ! ( ___tnl__assert_condition ) ) \
{ \
std::stringstream buffer; \
auto old = std::cerr.rdbuf( buffer.rdbuf() ); \
\
std::cerr << "Assertion '" << __STRING( ___tnl__assert_condition ) << "' failed !!!" << std::endl \
<< "File: " << __FILE__ << std::endl \
<< "Function: " << __PRETTY_FUNCTION__ << std::endl \
<< "Line: " << __LINE__ << std::endl \
<< "Diagnostics: "; \
___tnl__assert_command; \
\
std::string msg = buffer.str(); \
std::cerr.rdbuf( old ); \
throw ::TNL::Assert::AssertionError( msg ); \
}
#endif // __CUDA_ARCH__
#else
/* #ifndef NDEBUG */
// empty macros for optimized build
#define TNL_ASSERT_TRUE( val, msg )
#define TNL_ASSERT_FALSE( val, msg )
#define TNL_ASSERT_EQ( val1, val2, msg )
#define TNL_ASSERT_NE( val1, val2, msg )
#define TNL_ASSERT_LE( val1, val2, msg )
#define TNL_ASSERT_LT( val1, val2, msg )
#define TNL_ASSERT_GE( val1, val2, msg )
#define TNL_ASSERT_GT( val1, val2, msg )
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command )
#else // #ifdef TNL_THROW_ASSERTION_ERROR
// This will be used in regular C++ code
#define TNL_ASSERT( ___tnl__assert_condition, ___tnl__assert_command ) \
if( ! ( ___tnl__assert_condition ) ) \
{ \
std::cerr << "Assertion '" << __STRING( ___tnl__assert_condition ) << "' failed !!!" << std::endl \
<< "File: " << __FILE__ << std::endl \
<< "Function: " << __TNL_PRETTY_FUNCTION << std::endl \
<< "Line: " << __LINE__ << std::endl \
<< "Diagnostics: "; \
___tnl__assert_command; \
throw EXIT_FAILURE; \
}
#endif // #ifdef TNL_THROW_ASSERTION_ERROR
#endif // #ifdef __CUDA_ARCH__
#endif
/
*
#if
n
def NDEBUG
*/
#endif /
/
#ifdef NDEBUG
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment