Repository navigation
Add cuda::minimum and cuda::maximum - #2681
Conversation
| using __result_type = _Tp; // used by valarray | ||
|
|
||
| _CCCL_EXEC_CHECK_DISABLE | ||
| constexpr _LIBCUDACXX_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const |
There was a problem hiding this comment.
std::max<T>returns aconst T&.cuda::maximum<T>::operator()returns aT.cuda::maximum<void>::operator()<T,T>return aconst T&.
That's a gratuitous difference that will cause problems eventually. Either:
cuda::maximum<T>::operator()should return aconst T&. This seems like the correct choice because it matches the return type ofstd::max<T>, and because it can be used with non-copyable types. But it results in a slight change tothrust::maximum<T>::operator(), which could in theory have cascading effects.cuda::maximum<void>::operator()<T,T>should return aTso it is consistent with the non-voidspecializations.
I'm not sure which is the better choice.
cuda::minimum has the same issue.
There was a problem hiding this comment.
Hmmm I think returning T is the best choice. Consider the following:
- All of the other functor objects (e.g.
std::plus<>,std::minus<>etc.) return by value, so havingminimumandmaximumreturn by ref would be inconsistent. - It just so happens that we implement using
cuda::std::min(). I could just as well have inlined this (and in fact, I suspect most host compiler runtimes do for compile time savings). - The functor objects are often used as part of reductions. If we return by ref, then you can easily get into subtle bug territory. Consider:
int x = 0, y = 100;
// auto&& is common in generic code, min_val = x = 0 now
auto&& min_val = cuda::minimum<>{}(x, y);
// oops, min_val = 1 now too
++x;Sure you could argue that this is a programmer mistake (using auto&& when it should just be auto, and take a copy). But if we return by-value, then this mistake is impossible.
If you agree, I will update cuda::maximum<void>::operator() to return a T. I guess a follow-on question should be:
- Take only 1 template param?
- Or take 2 template param and return
std::common_type_t?
There was a problem hiding this comment.
All of the other functor objects (e.g. std::plus<>, std::minus<> etc.) return by value
That's a compelling argument. I should have checked that myself. So yes, I agree that maximum<void> should be changed to return T.
I prefer two template parameters and returning std::common_type_t. And have the implementation be a ?: expression rather than a call to cuda::std::max.
There was a problem hiding this comment.
OK return common_type and inlined max() and min(). Let me know if I missed anything in the conversion. STL code is quite... hard on the eyes :)
There was a problem hiding this comment.
For completeness: cuda::std::identity returns T&& and thrust::identity returns const T&, so returning a T for maximum/minimum is not a must.
There was a problem hiding this comment.
Conventionally attributes should come first
| constexpr _LIBCUDACXX_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const | |
| _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp operator()(const _Tp& __x, const _Tp& __y) const |
| using __result_type = _Tp; // used by valarray | ||
|
|
||
| _CCCL_EXEC_CHECK_DISABLE | ||
| constexpr _LIBCUDACXX_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const |
There was a problem hiding this comment.
Conventionally attributes should come first
| constexpr _LIBCUDACXX_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const | |
| _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp operator()(const _Tp& __x, const _Tp& __y) const |
|
/ok to test |
miscco
left a comment
There was a problem hiding this comment.
Thanks a lot for working on this. I have pushed a few cleanups and fixes:
- I forgot to mention that we want those functions to be marked as
[[nodiscard]] - We conventionally avoid anonymous namespaces in tests, these tests will never be included anywhere else so i does not matter.
- Minor license year fix
- I believe the
test_opwas taken from comparison functors. However, in this case we want to ensure that minimum and maximum are indeed symmetric. - I have cleaned the tests a bit, so that we can test constexpr in C++14 but do not clutter them too much for C++11
|
I looked at it again and I could make it work rather nicely. Everything boiled down a lot so I dropped the header file: |
|
/ok to test |
|
/ok to test |
🟩 CI finished in 2h 41m: Pass: 100%/394 | Total: 5d 09h | Avg: 19m 46s | Max: 1h 21m | Hits: 60%/25847
|
| Project | |
|---|---|
| CCCL Infrastructure | |
| +/- | libcu++ |
| CUB | |
| +/- | Thrust |
| +/- | CUDA Experimental |
| python | |
| CCCL C Parallel Library | |
| Catch2Helper |
Modifications in project or dependencies?
| Project | |
|---|---|
| CCCL Infrastructure | |
| +/- | libcu++ |
| +/- | CUB |
| +/- | Thrust |
| +/- | CUDA Experimental |
| +/- | python |
| +/- | CCCL C Parallel Library |
| +/- | Catch2Helper |
🏃 Runner counts (total jobs: 394)
| # | Runner |
|---|---|
| 326 | linux-amd64-cpu16 |
| 28 | linux-arm64-cpu16 |
| 25 | linux-amd64-gpu-v100-latest-1 |
| 15 | windows-amd64-cpu16 |
|
Great work, thanks @Jacobfaib ! |
|
/ok to test |
…ors in associative containers
|
/ok to test |
|
/ok to test |
🟩 CI finished in 3h 10m: Pass: 100%/394 | Total: 7d 06h | Avg: 26m 37s | Max: 1h 23m | Hits: 60%/25847
|
| Project | |
|---|---|
| CCCL Infrastructure | |
| +/- | libcu++ |
| CUB | |
| +/- | Thrust |
| CUDA Experimental | |
| python | |
| CCCL C Parallel Library | |
| Catch2Helper |
Modifications in project or dependencies?
| Project | |
|---|---|
| CCCL Infrastructure | |
| +/- | libcu++ |
| +/- | CUB |
| +/- | Thrust |
| +/- | CUDA Experimental |
| +/- | python |
| +/- | CCCL C Parallel Library |
| +/- | Catch2Helper |
🏃 Runner counts (total jobs: 394)
| # | Runner |
|---|---|
| 326 | linux-amd64-cpu16 |
| 28 | linux-arm64-cpu16 |
| 25 | linux-amd64-gpu-v100-latest-1 |
| 15 | windows-amd64-cpu16 |
* Add cuda::minimum and cuda::maximum
* Add cuda::minimum and cuda::maximum
* copy pasted sample * First draft * Kernel functor and some other things * Clean up and break up long main function * Needs launch fix * Switch to copy_bytes and cleanups * Missing include * Add exception print and waive value * Adjust copy count * Add license and switch benchmark streams * Remove a function left as a mistake * Update copyright date Co-authored-by: Eric Niebler <[email protected]> * Setup cudax examples. (#2697) * Move the sample to new location and fix warning * build fixes and 0 return code on waive * Some new MSVC errors * explicit cast * Rename enable/disable peer access and separate the sample loop * Add `cuda::minimum` and `cuda::maximum` (#2681) * Add cuda::minimum and cuda::maximum * Various fixes to cub::DeviceTransform (#2709) * Workaround non-copyable iterators * Use a named constant for SMEM * Cast to raw reference 2 * Fix passing non-copy-assignable iterators to transform_kernel via kernel_arg * Make `thrust::transform` use `cub::DeviceTransform` (#2389) * Add transform benchmark requiring a stable address * Make thrust::transform use cub::DeviceTransform * Introduces address stability detection and opt-in in libcu++ * Mark lambdas in Thrust BabelStream benchmark address oblivious * Optimize prefetch cub::DeviceTransform for small problems Fixes: #2263 * Ensure that we only use the inline variable trait when it is actually available (#2712) * Ensure that we only use the inline variable trait when it is actually available * Use the right define for internal traits * [CUDAX] Rename memory resource and memory pool from async to device (#2710) * Rename the type * Update tests * Rename async memory pool * Rename the tests * Change name in the docs * Generalise the memory_pool_properties name * Fix docs --------- Co-authored-by: Michael Schellenberger Costa <[email protected]> * Update memory resource name --------- Co-authored-by: Eric Niebler <[email protected]> Co-authored-by: Allison Piper <[email protected]> Co-authored-by: Jacob Faibussowitsch <[email protected]> Co-authored-by: Bernhard Manfred Gruber <[email protected]> Co-authored-by: Michael Schellenberger Costa <[email protected]>
* copy pasted sample * First draft * Kernel functor and some other things * Clean up and break up long main function * Needs launch fix * Switch to copy_bytes and cleanups * Missing include * Add exception print and waive value * Adjust copy count * Add license and switch benchmark streams * Remove a function left as a mistake * Update copyright date Co-authored-by: Eric Niebler <[email protected]> * Setup cudax examples. (NVIDIA#2697) * Move the sample to new location and fix warning * build fixes and 0 return code on waive * Some new MSVC errors * explicit cast * Rename enable/disable peer access and separate the sample loop * Add `cuda::minimum` and `cuda::maximum` (NVIDIA#2681) * Add cuda::minimum and cuda::maximum * Various fixes to cub::DeviceTransform (NVIDIA#2709) * Workaround non-copyable iterators * Use a named constant for SMEM * Cast to raw reference 2 * Fix passing non-copy-assignable iterators to transform_kernel via kernel_arg * Make `thrust::transform` use `cub::DeviceTransform` (NVIDIA#2389) * Add transform benchmark requiring a stable address * Make thrust::transform use cub::DeviceTransform * Introduces address stability detection and opt-in in libcu++ * Mark lambdas in Thrust BabelStream benchmark address oblivious * Optimize prefetch cub::DeviceTransform for small problems Fixes: NVIDIA#2263 * Ensure that we only use the inline variable trait when it is actually available (NVIDIA#2712) * Ensure that we only use the inline variable trait when it is actually available * Use the right define for internal traits * [CUDAX] Rename memory resource and memory pool from async to device (NVIDIA#2710) * Rename the type * Update tests * Rename async memory pool * Rename the tests * Change name in the docs * Generalise the memory_pool_properties name * Fix docs --------- Co-authored-by: Michael Schellenberger Costa <[email protected]> * Update memory resource name --------- Co-authored-by: Eric Niebler <[email protected]> Co-authored-by: Allison Piper <[email protected]> Co-authored-by: Jacob Faibussowitsch <[email protected]> Co-authored-by: Bernhard Manfred Gruber <[email protected]> Co-authored-by: Michael Schellenberger Costa <[email protected]>
* Add cuda::minimum and cuda::maximum
Description
closes #2680
Add
cuda::minimumandcuda::maximumChecklist