-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce new ERROR
and ASSERT
macros
#336
Conversation
…orting an error and exiting)
… confirm that they actually work) - moved the check on the value of ``gama``. Previously there was a logical bug in the check and the check was performed before the variable was initialized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of [[noreturn]]
and the gama
bugfix are great.
There's some C-style code in Abort_With_Err_
that should be C++ code.
While I think that ERROR
and ASSERT
are clever I'm unconvinced that they bring anything that couldn't be handled equally well or more idiomatically with some combination of assert
, exceptions, or just a direct call to std::cerr
. If we just want a warning then std::cerr
is easier, if we don't want the process to close upon finding an error then exceptions are a better since we can handle them, and if we do want Cholla to exit then IMO printing a warning then a direct call to chexit
is clearer in what it is doing.
As a semi-related note. Our testing framework, GoogleTest, is full of macros that look like ASSERT_<qualifier>
and I think that having another macro named ASSERT
could lead to confusion.
Thanks for reviewing this. I'll respond more generally in a few moments. But first, I realized that I was missing a commit that I had made on Friday (while I was writing up the PR) that I forgot to push. This change directly addressed a comment you already made - after this commit, |
To clarify this. C-style code isn't necessarily bad or not allowed in Cholla, since CUDA, MPI, and HDF5 expect it we need to use a lot of C-style code. My intention was that IMO this particular case (managing strings) is an example where the C++ version is more readable and maintainable. |
namingFirst, I just wanted to briefly mention that I would definitely be in favor of renaming motivating
|
NamingI think a change to MotivationI think I'm generally convinced. I'm not sure that I will use them but I think others might. I don't see any way that they harm the code base and I do see the benefits that you highlight. I'm fine with merging them in and I'll experiment with them. I do think we can, and should, move away from I will say that part of my aversion to char arrays/vectors is personal preference. I originally came from Python and deeply dislike how C handles strings (or more accurately, doesn't). Other thoughtsI know that people complain about exceptions being slow. IMO their performance isn't that important since they should only actually trigger in rare situations SummaryWith the changes to naming, at least investigating using |
Actually, follow-up. If we use the STL at all these cannot be called from within GPU kernels. If they were refactored to be callable from GPU kernels I would 100% be behind it but that's not straightforward to actually make it crash both the kernel and the code. |
e95f111
to
000cfbd
Compare
I'm very much on the same page. Given the option, I much prefer to use
I'm totally open to going that route. With that said, I did some quick google searches and it's not at all obvious to me how we would make that work (getting consistent behavior for aborting the run doesn't seem straight-forward). But, I don't think it's a huge deal if we just restrict this functionality to hosts. |
This PR primarily introduces new
ERROR
andASSERT
macros to simplify the process of reporting errors/aborting.(I kind of forged ahead with this before asking - I'm happy to drop this if the reviewers dislike it)
A brief note about
[[noreturn]]
I've annotated
chexit
(and another function introduced in this PR) with the[[noreturn]]
. Just in case you're unaware of what this means, this is the portable way to introduce "attributes" to functions that was introduced in C++ 11. Furthermore, noreturn is the name of an attribute (also introduced in C++11) that informs the compiler that the annotated function never returns.This annotation helps avoid compiler warnings about functions not-returning a result in a function like the hypothetical one shown in the following spoiler tag:
Contrived example function
About
ERROR
andABORT
ERROR
cause the program to print an error tostderr
, with some extra meta-data (e.g. process number, file-name and location where it was called).ASSERT
checks a boolean condition, and if that condition evaluates tofalse
it is equivalent to evaluatingERROR
.msg
is the error-msg that will be printed.msg
is specified with printf-style formatting, the remaining arguments are used to specify the output.cond
is the condition that is to be checked by the assertion statement.ASSERT
only produces the programASSERT
andERROR
are defined as macros rather than as functions so that they can automatically print the function-name, file-name, and line number where the error occurred. Under the hood they call a newly-defined function calledAbort_With_Err_
.Potential future improvements:
ASSERT
(like what is done in the standard library'sassert
macro).Bugfix: moved value-check of
::gama
To test out the new macros, I replaced a few snippets of existing code to make use of
ERROR
orASSERT
(and then intentionally passed parameters to make the code fail).One of the places where I did this was in
Check_Configuration
, where the value of::gama
got checked.gama > 1
- the error message said that code was failing becausegama
was less than or equal to 1.::gama
was being initialized from theP.gamma
(i.e. it had a value of0
).::gama
intoSet_Gammas
- the function where::gama
is initialized.