-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Port generic alpaka phi functions to DataFormats/Math
#47033
base: master
Are you sure you want to change the base?
Port generic alpaka phi functions to DataFormats/Math
#47033
Conversation
cms-bot internal usage |
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-47033/43155 |
A new Pull Request was created by @VourMa for master. It involves the following packages:
@cmsbuild, @jfernan2, @mandrenguyen can you please review it and eventually sign? Thanks. cms-bot commands are listed here |
@cms-sw/heterogeneous-l2 (not sure if this PR would need to be assigned to
Thinking out loud
|
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'd suggest to add unit tests.
|
||
// reduce to [-pi,pi] | ||
template <typename TAcc, typename T> | ||
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T reduceRange(TAcc const& acc, T x) { |
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 was going to comment the ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE
are not necessary because the function is constexpr
, but after looking at Alpaka implementation of e.g. abs()
and round()
https://github.com/alpaka-group/alpaka/blob/a4142d3feb7686d803e1ec5f25d7b2278337f455/include/alpaka/math/Traits.hpp#L864
https://github.com/alpaka-group/alpaka/blob/a4142d3feb7686d803e1ec5f25d7b2278337f455/include/alpaka/math/Traits.hpp#L1331
that are not constexpr
, I'm actually puzzled how this function can be constexpr
.
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.
@makortel when do we expect to switch to C++23? These functions will be constexpr in the standard/STL at that point.
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.
Indeed, you are right. Compiling and using the functions did not show any errors though, is it that constexpr
is silently ignored? If that's the case, I guess I should remove it so that it does not cause confusion.
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.
when do we expect to switch to C++23?
Based on the experience with C++20, maybe around 2027.
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.
Indeed, you are right. Compiling and using the functions did not show any errors though, is it that
constexpr
is silently ignored? If that's the case, I guess I should remove it so that it does not cause confusion.
It seems that constexpr
functions and function templates are allowed to call non-constexpr
functions as long as there is at least one set of arguments with which the function can be evaluated as a constant expression (and otherwise the program is ill-formed, no diagnostic required...).
From C++20 draft (N4849), 9.2.5.7 (.6 is also relevant)
If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function, that specialization is still a constexpr function, even though a call to such a function cannot appear in a constant expression. If no specialization of the template would satisfy the requirements for a constexpr function when considered as a non-template function, the template is ill-formed, no diagnostic required.
So while our compilers might not warn about the situation, C++20-conforming code would not use constexpr
here. Although IIUC this constraint was removed from C++23.
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.
Since C++23 seems to be quite a bit away for CMSSW, I would go with the C++20 conforming option and remove the constexpr
.
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.
Note that ALPAKA_FN_INLINE
is much stronger than inline
, as it tries to force the compiler to actually inline the function.
The regular inline
should be fine most of the time.
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 regular inline should be fine most of the time.
Should I be switching to that in this case? I am not sure whether you were suggesting that I apply this change or it was a more general comment.
// reduce to [-pi,pi] | ||
template <typename TAcc, typename T> | ||
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T reduceRange(TAcc const& acc, T x) { | ||
constexpr T o2pi = 1. / (2. * M_PI); |
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.
Now that we have C++20, how about
constexpr T o2pi = 1. / (2. * M_PI); | |
constexpr T o2pi = T{1.} / (T{2.} * std::numbers::pi_v<T>); |
?
Is it intentional to do the calculations on double and cast the result to T
?
Is T
expected to be only a floating point type?
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 think that it would be better to maintain the implementation as close as possible to the original deltaPhi.h
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.
Indeed, the implementation here is a faithful adaptation of the code in
cmssw/DataFormats/Math/interface/deltaPhi.h
Lines 17 to 24 in 2f2cacb
template <typename T> | |
constexpr T reduceRange(T x) { | |
constexpr T o2pi = 1. / (2. * M_PI); | |
if (std::abs(x) <= T(M_PI)) | |
return x; | |
T n = std::round(x * o2pi); | |
return x - n * T(2. * M_PI); | |
} |
Looking at the following lines in that file
cmssw/DataFormats/Math/interface/deltaPhi.h
Lines 26 to 32 in 2f2cacb
constexpr double deltaPhi(double phi1, double phi2) { return reduceRange(phi1 - phi2); } | |
constexpr double deltaPhi(float phi1, double phi2) { return deltaPhi(static_cast<double>(phi1), phi2); } | |
constexpr double deltaPhi(double phi1, float phi2) { return deltaPhi(phi1, static_cast<double>(phi2)); } | |
constexpr float deltaPhi(float phi1, float phi2) { return reduceRange(phi1 - phi2); } |
I suppose that the intention is that
T
is only a floating point type but I am not sure. For LST purposes (for which these functions are going to be used, at least initially), floating point type only T
is enough.
That said, your suggestion makes sense but maybe it should be propagated to interface/deltaPhi.h
as well? Not sure whether we want to do so though (it was not the initial purpose of this PR).
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.
That said, your suggestion makes sense but maybe it should be propagated to
interface/deltaPhi.h
as well? Not sure whether we want to do so though (it was not the initial purpose of this PR).
Ok, maybe it would make sense to keep both versions consistent, and maybe any changes would be beyond this PR.
I suppose that the intention is that
T
is only a floating point type but I am not sure. For LST purposes (for which these functions are going to be used, at least initially), floating point type onlyT
is enough.
I was wondering if anyone would be interested to add that as a type constraint either as
template <typename TAcc, typename T>
requires std::is_floating_point_v<T>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE T reduceRange(TAcc const& acc, T x) {
or
template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE std::floating_point auto reduceRange(TAcc const& acc, std::floating_point auto x) {
but probably that's beyond this PR as well.
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.
Note that doing the computation in double precision may be quite expensive on most GPUs. For example it is 64 times more expensive than single precision on an NVIDIA L4.
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 will go ahead and apply the suggestion by Matti to fix this:
constexpr T o2pi = T{1.} / (T{2.} * std::numbers::pi_v<T>);
is there a way to keep the dependence only via |
No. For link-time dependencies that wouldn't work. Some source-level operations, like IIRC |
Based on this and the follow up discussion, I understand that the current placement is not ideal, since the dependencies cannot be reduced. During our internal review, I had this initially placed under a new |
Sure. Do you have in mind some unit test that I can use as a template (to speed up the work and to make sure that I cover what needs to be tested)? |
IIUC, |
Ah, sorry, I misinterpreted the comment to mean that the new subpackage would be under |
I guess @makortel could confirm the preference to avoid further confusion. |
By quick look I see the existing cmssw/DataFormats/Math/test/testAtan2.cpp Lines 187 to 189 in f55e6a5
(and even there it is not immediately clear whether reduceRange() or int2phi() is the one being tested).
I'd say the primary objective would be to ensure all functions can be called for the relevant data types (I suppose both Here are a few rather simple Alpaka-using unit tests
I personally prefer the Catch2 unit test framework (used in |
I'd wait for next week before moving things around to get more feedback (after more people will presumably be back) I would not be against
|
My estimation is that it's tiny, so I will not go with that option.
I cannot really predict and I don't have a use case right now, but I can see people needing, e.g., |
dbf0fbf
to
99036e0
Compare
-code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-47033/43221 Code check has found code style and quality issues which could be resolved by applying following patch(s)
|
99036e0
to
c043336
Compare
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-47033/43223 |
@cmsbuild please test I'm not sure why this was left unattended from the review side for two days |
On my side, because I am busy with other tasks. |
// reduce to [-pi,pi] | ||
template <typename TAcc, typename T> | ||
requires std::is_floating_point_v<T> | ||
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE T reduceRange(TAcc const& acc, T x) { |
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.
reduceRange
is not a good name, because it doesn't convey any relation to [-π, π].
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.
maybe reducePhiRange
would be better ?
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 agree, and this was brought up by Andres as well during our internal discussion. However, in this PR, I tried to stick to the current CMSSW convention which uses reduceRange
in multiple places for keeping φ within [-π, π]:
https://github.com/search?q=repo%3Acms-sw%2Fcmssw%20reduceRange&type=code
Do we want to change just for this version of this function?
Maybe a global renaming to something clearer (reducePhiRange
sounds good to me) would make sense. If at least some of us think it's useful and it doesn't turn out to be a rabbithole, I could try to propose it in a different PR for wider review. But I wouldn't connect it to this PR.
+1 Size: This PR adds an extra 16KB to repository Comparison SummarySummary:
|
@fwyzard |
During the review of the LST integration PR #45117 it was identified that some of the math functions rewritten in the alpaka framework for usage in the LST algorithm could be made more widely available in CMSSW by porting them to a more central package:
This PR follows up on those comments. After the initial PR review, the choice made was to port these functions under
Heterogeneous/AlpakaMath
. The function logic is as close as possible to the non-alpaka equivalentDataFormats/Math/interface/deltaPhi.h
file.This PR only introduces the new function to CMSSW + the relevant unit tests. A follow-up PR will be made to actually use these functions in the LST algorithm and delete the LST-specific implementations.