[SYCL] Add support for SYCL math functions
This PR adds support for SYCL math functions. Math functions in the std
namespace cannot be used inside SYCL kernels and those in the sycl
namespace should be used instead.
It follows the discussions in #1132 and the PR #1285 that introduced USM support for SYCL.
The macro EIGEN_USING_STD
has been split in EIGEN_USING_STD
for non-math functions and EIGEN_USING_STD_MATH
for math functions.
This change has effects only when EIGEN_USE_SYCL
is defined and allows to use math functions in the sycl
namespace.
When EIGEN_USE_SYCL
is NOT defined, nothing changes wrt before.
In some places using std::...
was used directly instead of EIGEN_USING_STD
. These as well have been replaced with EIGEN_USING_STD_MATH
.
Merge request reports
Activity
added 2 commits
I understand the pragmatic reasons for this, but I am not a fan of this MR for the following reason: After this is merged, any contributor will have to reason about whether to use EIGEN_USING_STD or EIGEN_USING_MATH_STD. It is bad enough that we need EIGEN_USING_STD in the first place, because cuda/HIP does/did not implement C++11.
@cantonios @chuckyschluz what do you think?
Edited by Rasmus Munk LarsenIt is bad enough that we need EIGEN_USING_STD in the first place, because cuda/HIP does/did not implement C++11.
Could you elaborate on what you mean ?
Like any other library, CUDA is not allowed by the C++ standard to define any functions in the
std
namespace.Another option is to force sycl to specialize
numext::*
for all their types.SYCL does not use or define any special types, the SYCL math functions operate on
float
,double
, etc.I mean that most standard math functions are defined in the std namespace since C++11, see https://en.cppreference.com/w/cpp/header/cmath
They could certainly implement the C++ Standard Library for their device in the std namespace, in full or in parts, and I believe they have done so partially in recent years. I don't recall the reason for not doing so originally, but perhaps having to comply with the standard in terms of error handling was imposing constraints that would harm performance.
Edited by Rasmus Munk LarsenI can think of a few reasons (compatibility with the standard library used by the host compiler, licensing, avoiding ambiguity, etc.) but of course I do not know what the actual ones were.
It's also kind of beside the point... as a user of CUDA, HIP, SYCL and Eigen, and need a way for them to play well together :-)
I think these math functions should point to
numext
whenever possibleIs this suggestion to replace all uses (for example) of
using std::abs;
or
EIGEN_USING_STD(pow);
with explicit use of
numext::abs
andnumext::pow
?@AuroraPerego any update?
Hi @cantonios, I managed to replace
using std::*
andEIGEN_USING_STD(*)
with methods from thenumext
namespace. I ran into an error related topow_impl
(MathFunctions.h
, line 539) becausepow
was called with a double for the base and an integer for the exponent (StableNorm.h
, line 143). This led to an error with theresult_type
due toScalarBinaryOpTraits
not having a specialization forScalarX=double
andScalarY=int
. I solved it withstd::common_type<ScalarX, ScalarY>::type
, but let me know if you prefer another solution.The specializations are:
ScalarBinaryOpTraits<T, T> ScalarBinaryOpTraits<T, typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real> ScalarBinaryOpTraits<typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real, T> ScalarBinaryOpTraits<T, void> ScalarBinaryOpTraits<void, T> ScalarBinaryOpTraits<void, void>
added 189 commits
-
7af65e4a...23f6c268 - 187 commits from branch
libeigen:master
- 1483e988 - move using std::* and EIGEN_USING_STD(*) to using numext::*
- b1cb28d4 - fix error on pow
-
7af65e4a...23f6c268 - 187 commits from branch