-
Notifications
You must be signed in to change notification settings - Fork 418
cuda::isclose()
#9577
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
fbusato
wants to merge
16
commits into
NVIDIA:main
Choose a base branch
from
fbusato:isclose
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.
+607
−0
Open
cuda::isclose()
#9577
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f5a8506
draft
fbusato f89bda2
simplifications
fbusato 3f07bfd
unit test
fbusato 1b063b9
Update libcudacxx/include/cuda/__numeric/isclose.h
fbusato d7f286b
a few improvements
fbusato 31935cd
draft
fbusato c8f401a
simplifications
fbusato 765a138
unit test
fbusato 1d50332
Update libcudacxx/include/cuda/__numeric/isclose.h
fbusato c5657f4
a few improvements
fbusato b22956c
improve test, docs
fbusato 5a86c05
Merge branch 'isclose' of https://github.com/fbusato/cccl into isclose
fbusato 0ba5143
delete temp
fbusato 1aa4485
documentation fixes
fbusato 0fed909
Merge branch 'main' into isclose
fbusato 5ed5965
doc: annotate exec space
fbusato 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
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,102 @@ | ||
| .. _libcudacxx-extended-api-numeric-isclose: | ||
|
|
||
| ``cuda::isclose`` | ||
| ================= | ||
|
|
||
| Defined in ``<cuda/numeric>`` header. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| namespace cuda { | ||
|
|
||
| template <class T> | ||
| [[nodiscard]] __host__ __device__ __tile__ constexpr | ||
| bool isclose(T lhs, T rhs) noexcept; | ||
|
|
||
| template <class T> | ||
| [[nodiscard]] __host__ __device__ __tile__ constexpr | ||
| bool isclose(T lhs, T rhs, float relative_tol) noexcept; | ||
|
|
||
| template <class T> | ||
| [[nodiscard]] __host__ __device__ __tile__ constexpr | ||
| bool isclose(T lhs, T rhs, float relative_tol, T absolute_tol) noexcept; | ||
|
|
||
| template <class Complex> | ||
| [[nodiscard]] __host__ __device__ constexpr | ||
| bool isclose(const Complex& lhs, const Complex& rhs) noexcept; | ||
|
|
||
| template <class Complex> | ||
| [[nodiscard]] __host__ __device__ constexpr | ||
| bool isclose(const Complex& lhs, const Complex& rhs, float relative_tol) noexcept; | ||
|
|
||
| template <class Complex, class AbsTol> | ||
| [[nodiscard]] __host__ __device__ constexpr | ||
| bool isclose(const Complex& lhs, const Complex& rhs, | ||
| float relative_tol, | ||
| AbsTol absolute_tol) noexcept; | ||
|
|
||
| } // namespace cuda | ||
|
|
||
| ``cuda::isclose`` checks whether two values are approximately equal using the weak symmetric comparison in a similar manner to `PEP 485 <https://peps.python.org/pep-0485/>`_: | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| abs(lhs - rhs) <= max(absolute_tol, relative_tol * max(abs(lhs), abs(rhs))) | ||
|
|
||
| - The overloads without ``absolute_tol`` use ``absolute_tol == 0``. | ||
| - The overloads without ``relative_tol`` use a default relative tolerance based on half of available digits of accuracy. | ||
|
|
||
| **Parameters** | ||
|
|
||
| - ``lhs``: The first value to compare. | ||
| - ``rhs``: The second value to compare. | ||
| - ``relative_tol``: The relative tolerance. Passing ``0`` performs a purely absolute tolerance check when ``absolute_tol`` is non-zero. | ||
| - ``absolute_tol``: The absolute tolerance. This is useful for comparisons near zero. | ||
|
|
||
| **Precision** | ||
|
|
||
| - ``relative_tol``:Must be in the range [0.0, 1.0]. | ||
| - ``absolute_tol``: Must be finite and non-negative. | ||
|
|
||
| **Return value** | ||
|
|
||
| - Returns ``true`` if ``lhs`` and ``rhs`` are close to each other, otherwise returns ``false``. | ||
|
|
||
| **Constraints** | ||
|
|
||
| - Scalar overloads require ``lhs``, ``rhs``, ``absolute_tol`` to have the same arithmetic type (integer or floating point). | ||
| - Complex overloads accept ``cuda::std::complex<T>``, ``cuda::complex<T>``, ``std::complex<T>`` operands. | ||
| - ``AbsTol`` must be the same type as the complex value type. | ||
|
|
||
| **Special values** | ||
|
|
||
| - ``NaN`` is never close to any value, including another ``NaN``. | ||
| - Infinity and negative infinity are only close to themselves. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| .. code:: cuda | ||
|
|
||
| #include <cuda/numeric> | ||
| #include <cuda/std/cassert> | ||
| #include <cuda/std/complex> | ||
|
|
||
| __global__ void kernel() | ||
| { | ||
| assert(cuda::isclose( 1.0, 1.0 + 5e-10)); | ||
| assert(!cuda::isclose(1.0, 1.0 + 5e-8)); | ||
|
|
||
| assert(!cuda::isclose(0.0, 1e-12)); | ||
| assert(cuda::isclose( 0.0, 1e-12, 0.0, 1e-12)); | ||
|
|
||
| cuda::std::complex<double> z1{1.0, 1.0}; | ||
| cuda::std::complex<double> z2{2.0, 0.0}; | ||
| assert(cuda::isclose(z1, z2, 0.75)); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| kernel<<<1, 1>>>(); | ||
| cudaDeviceSynchronize(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.