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
Use parser for input parameters of type long #2506
Conversation
Source/Utils/WarpXUtil.H
Outdated
* \param x Real value to cast | ||
* \param real_name String, the name of the variable being casted to use in the error message | ||
*/ | ||
int | ||
safeCastToInt(amrex::Real x, const std::string& real_name); | ||
template <typename int_type = int> |
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.
I would prefer if we maybe use long
/amrex::Long
by default here.
Otherwise we start carrying more and more basic functionality into headers - which included in every translation unit that uses it will increase compile time.
One way to solve this if you need two types is also: we can use this template construct inside the WarpXUtil.cpp
file and add a safeCastToInt
and safeCastToLong
function declaration here in the header.
This looks Ok. Though, note that there are already duplicated routines for float and double. Perhaps follow the same model and create duplicate routines for int and long instead of using templates? |
This reverts commit 9573bb3.
Thanks for your comments. I agree with you that it's better to avoid the significant increase in compile time associated to these templates. One thing we could do is to use explicit template instantiation: declare the templates in Otherwise, we can just duplicate the functions for
Looks like there may be a third option, but I don't think I understand it 😄. If we declare the functions with two different names, then we can no longer define them using templates, can we? |
Thanks, sounds great!
Yes just to clarify, we want to avoid to establish a pattern that is compile-time intensive. Here the small change would not change the world (yet).
As long as the body is implemented in the (I also experimented with
Yes, let me elaborate. You can write two "stub" declarations in the header file // .H
int safeCastToInt(...); // ... is the args as before
int safeCastToLong(...);
// .cpp
namespace detail {
template< typename T >
AMREX_FORCE_INLINE
T safeCastTo( ... ) {
// ... body as before
}
} // namespace detail
int safeCastToInt( ... ) { return detail::safeCastTo< amrex::Int >( ... ); }
int safeCastToLong( ... ) { return detail::safeCastToLong< amrex::Long >( ... ); } That way, you have the best of those worlds: saved compile time and general code reuse. |
@ax3l Ok thanks, I understand your solution, that would work as well.
No, I'm talking about an explicit template instantiation where I put the body of the templated functions in the .cpp file, so that the other files cannot compile these functions. Basically, with your notations, that would look like:
I was wondering if there was an issue in doing that? (I have checked and at least the code compiles). In the end, both solutions are very similar. There may be fewer intermediate functions with an explicit template instantiation, but I'm definitely fine with both. |
You are right, for fully specialized templates this totally works. You just cannot inline them, but that's not an issue here at all. |
Sounds good, I will do that. Putting [WIP] until that's done. |
@ax3l @dpgrote Sorry I had forgotten about this PR. It should now be ready for review. Looks like in the meantime the |
Source/Utils/WarpXUtil.H
Outdated
@@ -197,6 +197,17 @@ void getCellCoordinates (int i, int j, int k, | |||
int | |||
safeCastToInt(amrex::Real x, const std::string& real_name); | |||
|
|||
/** | |||
* \brief Do a safe cast of a real to aa long |
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.
Typo "aa long"
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.
Thanks for catching this, this is fixed now.
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.
This looks good, thanks! I have the one minor comment.
* Use parser for input parameters of type long * Revert "Use parser for input parameters of type long" This reverts commit 9573bb3. * Use parser for inputs of type long * add safeCasttoLong function * Fix typo in comment
Two integer input parameters (
<species_name>.npart
for a gaussian_beam injection_style and<laser_name>.min_particles_per_mode
) are currently not read by the math parser because they are of typelong
.So that they could be read (and more generally that
long
input parameters can be read), I've made the following modifications:queryWithParser
functions that read integers are now templated so that they can be reused forlong
.safeCastToInt
function that is used by the above functions is also templated, withint
as the default template parameter.WarpXUtil.H
because they need functions declared in that file. That probably makes the PR harder to read, sorry about that.@dpgrote Do you think that this is a good solution to parse
long
input parameters?