-
Notifications
You must be signed in to change notification settings - Fork 294
Implement cuda::sincos
#6742
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
Open
davebayer
wants to merge
7
commits into
NVIDIA:main
Choose a base branch
from
davebayer:sincos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+371
−22
Open
Implement cuda::sincos
#6742
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfb128c
Implement `cuda::sincos`
davebayer c3bc030
fix clang
davebayer dd5f712
fix example
davebayer 26334ad
use sincos in hyperbolic complex functions
davebayer 30cc222
use sincos in more places
davebayer dd73201
fix review, improve performance with fp16 and bf16
davebayer 90694fe
guard ext fp types
davebayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| .. _libcudacxx-extended-api-math-sincos: | ||
|
|
||
| ``cuda::sincos`` | ||
| ==================================== | ||
|
|
||
| Defined in the ``<cuda/cmath>`` header. | ||
|
|
||
| .. code:: cuda | ||
|
|
||
| namespace cuda { | ||
|
|
||
| template <class T> | ||
| struct sincos_result | ||
| { | ||
| T sin; | ||
| T cos; | ||
| }; | ||
|
|
||
| template </*floating-point-type*/ T> | ||
| [[nodiscard]] __host__ __device__ | ||
| sincos_result<T> sincos(T value) noexcept; // (1) | ||
|
|
||
| template <class Integral> | ||
| [[nodiscard]] __host__ __device__ | ||
| sincos_result<double> sincos(Integral value) noexcept; // (2) | ||
|
|
||
| } // namespace cuda | ||
|
|
||
| Computes :math:`\sin value` and :math:`\cos value` at the same time using more efficient algorithms than if operations were computed separately. | ||
|
|
||
| **Parameters** | ||
|
|
||
| - ``value``: The input value. | ||
|
|
||
| **Return value** | ||
|
|
||
| - ``cuda::sincos_result`` object with both values set to ``NaN`` if the input value is :math:`\pm\infty` or ``NaN`` and to results of :math:`\sin value` and :math:`\cos value` otherwise. (1) | ||
| - if ``T`` is an integral type, the input value is treated as ``double``. (2) | ||
|
|
||
| **Constraints** | ||
|
|
||
| - ``T`` is an arithmetic type. | ||
|
|
||
| **Performance considerations** | ||
|
|
||
| - If available, the functionality is implemented by compiler builtins, otherwise fallbacks to ``cuda::std::sin(value)`` and ``cuda::std::cos(value)``. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| .. code:: cuda | ||
|
|
||
| #include <cuda/cmath> | ||
| #include <cuda/std/cassert> | ||
|
|
||
| __global__ void sincos_kernel() { | ||
| auto [sin_pi, cos_pi] = cuda::sincos(0.f); | ||
| assert(sin_pi == 0.f); | ||
| assert(cos_pi == 1.f); | ||
| } | ||
|
|
||
| int main() { | ||
| sincos_kernel<<<1, 1>>>(); | ||
| cudaDeviceSynchronize(); | ||
| return 0; | ||
| } | ||
|
|
||
| `See it on Godbolt 🔗 <https://godbolt.org/z/99PP9s1z6>`__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of libcu++, the C++ Standard Library for your entire system, | ||
| // under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _CUDA___CMATH_SINCOS_H | ||
| #define _CUDA___CMATH_SINCOS_H | ||
|
|
||
| #include <cuda/std/detail/__config> | ||
|
|
||
| #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) | ||
| # pragma GCC system_header | ||
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) | ||
| # pragma clang system_header | ||
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) | ||
| # pragma system_header | ||
| #endif // no system header | ||
|
|
||
| #include <cuda/std/__cmath/trigonometric_functions.h> | ||
| #include <cuda/std/__concepts/concept_macros.h> | ||
| #include <cuda/std/__type_traits/conditional.h> | ||
| #include <cuda/std/__type_traits/is_extended_arithmetic.h> | ||
| #include <cuda/std/__type_traits/is_integral.h> | ||
| #include <cuda/std/__type_traits/is_same.h> | ||
|
|
||
| #include <cuda/std/__cccl/prologue.h> | ||
|
|
||
| #if _CCCL_HAS_BUILTIN(__builtin_sincosf) || _CCCL_COMPILER(GCC) | ||
| # define _CCCL_BUILTIN_SINCOSF(...) __builtin_sincosf(__VA_ARGS__) | ||
fbusato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif // _CCCL_HAS_BUILTIN(__builtin_sincosf) || _CCCL_COMPILER(GCC) | ||
|
|
||
| #if _CCCL_HAS_BUILTIN(__builtin_sincos) || _CCCL_COMPILER(GCC) | ||
| # define _CCCL_BUILTIN_SINCOS(...) __builtin_sincos(__VA_ARGS__) | ||
| #endif // _CCCL_HAS_BUILTIN(__builtin_sincos) || _CCCL_COMPILER(GCC) | ||
|
|
||
| #if _CCCL_HAS_BUILTIN(__builtin_sincosl) || _CCCL_COMPILER(GCC) | ||
| # define _CCCL_BUILTIN_SINCOSL(...) __builtin_sincosl(__VA_ARGS__) | ||
| #endif // _CCCL_HAS_BUILTIN(__builtin_sincosl) || _CCCL_COMPILER(GCC) | ||
|
|
||
| // clang-cuda crashes if these builtins are used. | ||
| #if _CCCL_CUDA_COMPILER(CLANG) | ||
| # undef _CCCL_BUILTIN_SINCOSF | ||
| # undef _CCCL_BUILTIN_SINCOS | ||
| # undef _CCCL_BUILTIN_SINCOSL | ||
| #endif // _CCCL_CUDA_COMPILER(CLANG) | ||
|
|
||
| _CCCL_BEGIN_NAMESPACE_CUDA | ||
|
|
||
| //! @brief Type returned by \c cuda::sincos. | ||
| template <class _Tp> | ||
| struct _CCCL_TYPE_VISIBILITY_DEFAULT sincos_result | ||
| { | ||
| _Tp sin; //!< The sin result. | ||
| _Tp cos; //!< The cos result. | ||
| }; | ||
|
|
||
| //! @brief Computes sin and cos operation of a value. | ||
| //! | ||
| //! @param __v The value. | ||
| //! | ||
| //! @return The \c cuda::sincos_result with the results of sin and cos operations. | ||
| _CCCL_TEMPLATE(class _Tp) | ||
| _CCCL_REQUIRES(::cuda::std::__is_extended_arithmetic_v<_Tp>) | ||
| [[nodiscard]] _CCCL_API auto sincos(_Tp __v) noexcept | ||
| -> sincos_result<::cuda::std::conditional_t<::cuda::std::is_integral_v<_Tp>, double, _Tp>> | ||
fbusato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if constexpr (::cuda::std::is_integral_v<_Tp>) | ||
| { | ||
| return ::cuda::sincos(static_cast<double>(__v)); | ||
| } | ||
| else | ||
| { | ||
| [[maybe_unused]] sincos_result<_Tp> __ret{}; | ||
| #if defined(_CCCL_BUILTIN_SINCOSF) | ||
| if constexpr (::cuda::std::is_same_v<_Tp, float>) | ||
| { | ||
| _CCCL_BUILTIN_SINCOSF(__v, &__ret.sin, &__ret.cos); | ||
| return __ret; | ||
| } | ||
| #endif // _CCCL_BUILTIN_SINCOSF | ||
| #if defined(_CCCL_BUILTIN_SINCOS) | ||
| if constexpr (::cuda::std::is_same_v<_Tp, double>) | ||
| { | ||
| _CCCL_BUILTIN_SINCOS(__v, &__ret.sin, &__ret.cos); | ||
| return __ret; | ||
| } | ||
| #endif // _CCCL_BUILTIN_SINCOS | ||
| #if _CCCL_HAS_LONG_DOUBLE() && defined(_CCCL_BUILTIN_SINCOSL) | ||
| if constexpr (::cuda::std::is_same_v<_Tp, long double>) | ||
| { | ||
| _CCCL_BUILTIN_SINCOSL(__v, &__ret.sin, &__ret.cos); | ||
| return __ret; | ||
| } | ||
| #endif // _CCCL_HAS_LONG_DOUBLE() && _CCCL_BUILTIN_SINCOSL | ||
|
|
||
| _CCCL_IF_NOT_CONSTEVAL_DEFAULT | ||
| { | ||
| if constexpr (::cuda::std::is_same_v<_Tp, float>) | ||
| { | ||
| NV_IF_TARGET(NV_IS_DEVICE, (::sincosf(__v, &__ret.sin, &__ret.cos); return __ret;)) | ||
| } | ||
| if constexpr (::cuda::std::is_same_v<_Tp, double>) | ||
| { | ||
| NV_IF_TARGET(NV_IS_DEVICE, (::sincos(__v, &__ret.sin, &__ret.cos); return __ret;)) | ||
| } | ||
| #if _LIBCUDACXX_HAS_NVFP16() | ||
| if constexpr (::cuda::std::is_same_v<_Tp, ::__half>) | ||
| { | ||
| const auto __result_float = ::cuda::sincos(::__half2float(__v)); | ||
| return {::__float2half(__result_float.sin), ::__float2half(__result_float.cos)}; | ||
| } | ||
| #endif // _LIBCUDACXX_HAS_NVFP16() | ||
| #if _LIBCUDACXX_HAS_NVBF16() | ||
| if constexpr (::cuda::std::is_same_v<_Tp, ::__nv_bfloat16>) | ||
| { | ||
| const auto __result_float = ::cuda::sincos(::__bfloat162float(__v)); | ||
| return {::__float2bfloat16(__result_float.sin), ::__float2bfloat16(__result_float.cos)}; | ||
| } | ||
| #endif // _LIBCUDACXX_HAS_NVBF16() | ||
| } | ||
| return {::cuda::std::sin(__v), ::cuda::std::cos(__v)}; | ||
| } | ||
| } | ||
|
|
||
| _CCCL_END_NAMESPACE_CUDA | ||
|
|
||
| #include <cuda/std/__cccl/epilogue.h> | ||
|
|
||
| #endif // _CUDA___CMATH_SINCOS_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we can be more precise about the types where the optimization applies
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.
What exactly do you have in mind? Right now, the implementation uses the buitlins for all types if available