-
Notifications
You must be signed in to change notification settings - Fork 294
Extract environment boilerplate code from within the device interfaces to a separate header #6622
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
gonidelis
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
gonidelis:refactor_env_boilerplate
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||||||||||||||
| // SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||||||||||||||||||||
| // SPDX-License-Identifier: BSD-3-Clause | ||||||||||||||||||||
| #pragma once | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <cub/config.cuh> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #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 <cub/detail/device_memory_resource.cuh> | ||||||||||||||||||||
| #include <cub/detail/temporary_storage.cuh> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <cuda/__execution/tune.h> | ||||||||||||||||||||
| #include <cuda/__memory_resource/get_memory_resource.h> | ||||||||||||||||||||
| #include <cuda/__stream/get_stream.h> | ||||||||||||||||||||
| #include <cuda/std/__execution/env.h> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| CUB_NAMESPACE_BEGIN | ||||||||||||||||||||
|
|
||||||||||||||||||||
| namespace detail | ||||||||||||||||||||
| { | ||||||||||||||||||||
| //! @cond | ||||||||||||||||||||
| //! Generic environment-based algorithm dispatch wrapper | ||||||||||||||||||||
| //! | ||||||||||||||||||||
| //! Handles common boilerplate for all env-based algorithms: | ||||||||||||||||||||
| //! - Query stream, memory resource, and tuning from environment | ||||||||||||||||||||
| //! - Two-phase call (query temp storage size, then execute) | ||||||||||||||||||||
| //! - Temporary storage allocation/deallocation | ||||||||||||||||||||
| //! - Memory resource querying from environment | ||||||||||||||||||||
| //! | ||||||||||||||||||||
| //! @param env The execution environment | ||||||||||||||||||||
| //! @param determinism Pre-computed determinism type (algorithm-specific) | ||||||||||||||||||||
| //! @param algorithm_callable Callable that invokes the algorithm implementation with determinism specified | ||||||||||||||||||||
| //! @param algorithm_args Arguments to forward to the algorithm implementation | ||||||||||||||||||||
| template <typename EnvT, typename DeterminismT, typename AlgorithmCallable, typename... AlgorithmArgs> | ||||||||||||||||||||
| CUB_RUNTIME_FUNCTION static cudaError_t dispatch_with_env( | ||||||||||||||||||||
| EnvT env, DeterminismT determinism, AlgorithmCallable algorithm_callable, AlgorithmArgs&&... algorithm_args) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| // Query stream from environment | ||||||||||||||||||||
| auto stream = ::cuda::std::execution::__query_or(env, ::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Query memory resource from environment | ||||||||||||||||||||
| auto mr = | ||||||||||||||||||||
| ::cuda::std::execution::__query_or(env, ::cuda::mr::__get_memory_resource, detail::device_memory_resource{}); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Query tuning from environment | ||||||||||||||||||||
| using tuning_t = | ||||||||||||||||||||
| ::cuda::std::execution::__query_result_or_t<EnvT, ::cuda::execution::__get_tuning_t, ::cuda::std::execution::env<>>; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| void* d_temp_storage = nullptr; | ||||||||||||||||||||
| size_t temp_storage_bytes = 0; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Phase 1: Query temporary storage size | ||||||||||||||||||||
| cudaError_t error = algorithm_callable( | ||||||||||||||||||||
| tuning_t{}, | ||||||||||||||||||||
| d_temp_storage, | ||||||||||||||||||||
| temp_storage_bytes, | ||||||||||||||||||||
| ::cuda::std::forward<AlgorithmArgs>(algorithm_args)..., | ||||||||||||||||||||
| determinism, | ||||||||||||||||||||
| stream.get()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (error != cudaSuccess) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return error; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Allocate temporary storage | ||||||||||||||||||||
| error = CubDebug(detail::temporary_storage::allocate(stream, d_temp_storage, temp_storage_bytes, mr)); | ||||||||||||||||||||
| if (error != cudaSuccess) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return error; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+73
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: I got really fond of this style of error handling recently:
Suggested change
This way, we don't leak an |
||||||||||||||||||||
|
|
||||||||||||||||||||
| // Phase 2: Execute algorithm | ||||||||||||||||||||
| error = algorithm_callable( | ||||||||||||||||||||
| tuning_t{}, | ||||||||||||||||||||
| d_temp_storage, | ||||||||||||||||||||
| temp_storage_bytes, | ||||||||||||||||||||
| ::cuda::std::forward<AlgorithmArgs>(algorithm_args)..., | ||||||||||||||||||||
| determinism, | ||||||||||||||||||||
| stream.get()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Deallocate temporary storage (always attempt, even on error) | ||||||||||||||||||||
| cudaError_t deallocate_error = | ||||||||||||||||||||
| CubDebug(detail::temporary_storage::deallocate(stream, d_temp_storage, temp_storage_bytes, mr)); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Algorithm error takes precedence over deallocation error | ||||||||||||||||||||
| return (error != cudaSuccess) ? error : deallocate_error; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| //! @endcond | ||||||||||||||||||||
| } // namespace detail | ||||||||||||||||||||
|
|
||||||||||||||||||||
| CUB_NAMESPACE_END | ||||||||||||||||||||
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.
Remark: I assume we are retaining BSD-3 here, cause that's what we had in the file where we extracted the below code from.