Skip to content

cuda::isclose()#9577

Merged
fbusato merged 22 commits into
NVIDIA:mainfrom
fbusato:isclose
Jul 16, 2026
Merged

cuda::isclose()#9577
fbusato merged 22 commits into
NVIDIA:mainfrom
fbusato:isclose

Conversation

@fbusato

@fbusato fbusato commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description

Comparing two floating-point values is a widely used functionality. Every library that use floating-point values need to compare them at some point. CCCL used it too for internal testing.

Python has math.isclose() https://peps.python.org/pep-0485/, and numpy.isclose.
Several C++ testing frameworks and libraries provide a similar utility, GoogleTest, Catch2, Boost.Math, Eigen.

This PR introduces 3 overloads for isclose() following implementation decisions of math.isclose()

bool isclose(T lhs, T rhs);
bool isclose(T lhs, T rhs, float relative_tolerance);
bool isclose(T lhs, T rhs, float relative_tolerance, T absolute_tolerance);

we could also think to add interfaces based on C++20 designed initializer.

Supported types: integers, (extended) floating-point, complex (std. cuda, cuda::std).

Requires:

@fbusato fbusato self-assigned this Jun 24, 2026
@fbusato fbusato added the libcu++ For all items related to libcu++ label Jun 24, 2026
@fbusato
fbusato requested review from a team as code owners June 24, 2026 01:29
@fbusato
fbusato requested a review from gonidelis June 24, 2026 01:29
@fbusato fbusato added this to CCCL Jun 24, 2026
@fbusato
fbusato requested a review from Jacobfaib June 24, 2026 01:29
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 24, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jun 24, 2026
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds cuda::isclose for scalar and complex approximate-equality checks, wires it into cuda/numeric, and adds matching documentation and coverage tests.

Changes

cuda::isclose numeric utility

Layer / File(s) Summary
Documentation
docs/libcudacxx/extended_api/numeric.rst, docs/libcudacxx/extended_api/numeric/isclose.rst
Adds the new reference page, documents overloads and semantics, and registers the page in the numeric navigation and availability table.
Internal helpers
libcudacxx/include/cuda/__numeric/isclose.h
Defines the comparison-type selector, default relative tolerance, overflow-safe integer difference, and the floating-point, complex, and integer implementations.
Public overloads and header wiring
libcudacxx/include/cuda/__numeric/isclose.h, libcudacxx/include/cuda/numeric
Adds the public scalar and complex overloads, closes the namespace and include guard, and includes the new header from cuda/numeric.
Test coverage
libcudacxx/test/libcudacxx/cuda/numeric/isclose/isclose.pass.cpp
Adds compile-time and runtime tests for floating-point, integral, and complex overloads, including invalid abs_tol cases and target-gated runtime coverage.

Comment @coderabbitai help to get the list of available commands.

@github-actions

This comment has been minimized.

@davebayer davebayer changed the title cuda::std::isclose() cuda::isclose() Jun 24, 2026
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/test/libcudacxx/cuda/numeric/isclose/isclose.pass.cpp
fbusato and others added 2 commits June 24, 2026 09:54
Co-authored-by: Jacob Faibussowitsch <jacob.fai@gmail.com>
@github-actions

This comment has been minimized.

@fbusato
fbusato requested review from a team as code owners June 26, 2026 19:48
@github-actions

This comment has been minimized.

Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst Outdated
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst Outdated
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
@github-actions

This comment has been minimized.

Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst Outdated
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst Outdated
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
@github-actions

This comment has been minimized.

@fbusato
fbusato requested a review from oleksandr-pavlyk July 14, 2026 17:59
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
@github-actions

This comment has been minimized.

@fbusato
fbusato enabled auto-merge (squash) July 14, 2026 22:34
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 39m: Pass: 100%/120 | Total: 1d 20h | Max: 1h 32m | Hits: 92%/399559

See results here.

Comment on lines +32 to +37
template <class Complex, class AbsTol>
[[nodiscard]] __host__ __device__
bool isclose(const Complex& lhs,
const Complex& rhs,
float relative_tol,
AbsTol absolute_tol) noexcept;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: In non-complex API we require absolute_tol to be of type T, but here it seems like it can have a different type than Complex::value_type.

I think we should rather do:

Suggested change
template <class Complex, class AbsTol>
[[nodiscard]] __host__ __device__
bool isclose(const Complex& lhs,
const Complex& rhs,
float relative_tol,
AbsTol absolute_tol) noexcept;
template <class T>
[[nodiscard]] __host__ __device__
bool isclose(const /*complex-type*/<T>& lhs,
const /*complex-type*/<T>& rhs,
float relative_tol,
T absolute_tol) noexcept;

What do you think?


- Scalar overloads require ``lhs``, ``rhs``, ``absolute_tol`` to have the same arithmetic type (integer or floating point).
- Complex overloads accept ``cuda::std::complex<T>`` and ``std::complex<T>`` operands.
- ``AbsTol`` must be the same type as the complex value type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be gone if you accept my suggestion

Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment on lines +296 to +299
_CCCL_TEMPLATE(typename _ComplexType, typename _AbsTol)
_CCCL_REQUIRES(__isclose_complex_comparison_v<_ComplexType, _AbsTol>)
[[nodiscard]] _CCCL_HOST_DEVICE_API bool
isclose(const _ComplexType& __lhs, const _ComplexType& __rhs, const float __rel_tol, const _AbsTol __abs_tol) noexcept

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_CCCL_TEMPLATE(typename _ComplexType, typename _AbsTol)
_CCCL_REQUIRES(__isclose_complex_comparison_v<_ComplexType, _AbsTol>)
[[nodiscard]] _CCCL_HOST_DEVICE_API bool
isclose(const _ComplexType& __lhs, const _ComplexType& __rhs, const float __rel_tol, const _AbsTol __abs_tol) noexcept
_CCCL_TEMPLATE(template <class> class _LhsComplex, template <class> class _RhsComplex, class _Tp)
_CCCL_REQUIRES(::cuda::std::__cccl_is_integer_v<_Tp> || ::cuda::is_floating_point_v<_Tp>)
[[nodiscard]] _CCCL_HOST_DEVICE_API bool
isclose(const _LhsComplex<_Tp>& __lhs, const _RhsComplex<_Tp>& __rhs, const float __rel_tol, const _Tp __abs_tol) noexcept

@fbusato
fbusato merged commit 37dc633 into NVIDIA:main Jul 16, 2026
140 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libcu++ For all items related to libcu++

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants