Repository navigation
Description
Is this a duplicate?
- I confirmed there appear to be no duplicate issues for this request and that I agree to the Code of Conduct
Area
CUB
Is your feature request related to a problem? Please describe.
As discussed in #2523, we need MR-based overloads for CUB device-level algorithms.
Describe the solution you'd like
For any given device-level CUB algorithm that accepts void *d_temp_storage, size_t &temp_storage_bytes:
template <typename IteratorT, typename NumItemsT>
cudaError_t ExclusiveSum(void *d_temp_storage,
size_t &temp_storage_bytes,
IteratorT d_data,
NumItemsT num_items,
cudaStream_t stream = 0);we want to add an accompanying overload:
template <typename IteratorT, typename NumItemsT, typename MemoryResourceT = default_resource_t>
cudaError_t ExclusiveSum(IteratorT d_data,
NumItemsT num_items,
cudaStream_t stream = 0,
MemoryResourceT resource = {});which should query and allocate temporary storage internally, something along the following lines:
{
try {
void *d_temp_storage = nullptr;
size_t temp_storage_bytes = 0;
cudaError_t err = CubDebug(ExclusiveSum(d_temp_storage, temp_storage_bytes, d_data, num_items, stream));
if (err != cudaSuccess) {
return err;
}
d_temp_storage = mr.allocate_async(temp_storage_bytes, stream);
err = CubDebug(ExclusiveSum(d_temp_storage, temp_storage_bytes, d_data, num_items, stream));
if (err != cudaSuccess) {
return err;
}
mr.deallocate_async(d_temp_storage, temp_storage_bytes, stream);
} catch (...) {
return cudaErrorMemoryAllocation;
}
return cudaSuccess;
}This overload should take stream first, then memory resource. The default memory resource should be stateful and be equivalent to cudaMallocAsync, cudaFreeAsync. The memory resource type should be coming from a template parameter. Since memory resources are behind experimental macro, we might want to hide the new overload behind LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE as well. As we add new overloads, we should be cautious not to cause ambiguity. For the case above, consider the following invocation:
int *data{};
size_t num_items = 0;
size_t temp_storage_bytes = 0;
char *d_temp_storage = nullptr;
ExclusiveSum(data, num_items, 0, resource_t{});This call can be interpreted as:
void* d_temp_storage, size_t& temp_storage_bytes, int d_data, resource_t num_itemsint* d_data, size_t num_items, cudaStream_t stream, resource_t mr
So as we add new overloads, we should carefuly constrain new and existing ones. For the case above, we might want to constrain the existing overload as:
template <typename IteratorT, typename NumItemsT>
std::enable_if_t<std::is_integral_v<NumItemsT>, cudaError_t>
ExclusiveSum(void *d_temp_storage,
size_t &temp_storage_bytes,
IteratorT d_data,
NumItemsT num_items,
cudaStream_t stream = 0);We'll also want to constrain the new overloads on MemoryResourceT concept. To test new facilities, we can consider tweaking existing TEST_LAUNCH mode. We already test everything with a level of indirection that excludes temporary storage:
device_exclusive_sum(d_in_it, d_out_it, num_items);If we add another backend for test launcher, it could as well use memory resources API. That'd allows us to easily add tests for new overloads. But I'd postpone this a bit. Since overall improvement would result in a considerable amount of work, I suggest we split it into multiple steps:
- Add a single MR-based overload for prefix sum
- Properly test new MR-based prefix sum overload
- File a draft PR adding a single MR-based prefix sum overload and tests
- Consider abstracting out implementation details to make authoring MR-based overloads easier
- Consider abstracting out testing of MR-based overloads into another
TEST_LAUNCHmode - Create an inventory of algorithms to be complemented with MR-based overloads in a form of sub-issues
This issue can be closed once all sub-issues from the inventory are addressed.
Describe alternatives you've considered
No response
Additional context
No response
Activity
bernhardmgruber commentedon Feb 19, 2025
This is awesome! Here is another aspect: Have we considered how this design interplays with adding an execution policy to specify tunings etc. in the future?
This:
should have an additional overload:
eventually.
The only question I have is, whether we would have more ambiguities to sort out, or whether the memory resource should actually be part of the execution policy, which is established practice in Thrust.
We could also generalize the new
MemoryResourceTparameter, and allow users in the future to not only pass a memory resource, but also an execution policy with memory resource (we can disambiguite the case based on the data type internally). So another question is: should we separate memory resources and execution policies into dedicated arguments or combine them.In any case, I want us to be aware of adding execution policies in the future and make sure the design is sound.
miscco commentedon Feb 19, 2025
I am slightly worrying that the memory resource is the final argument after the stream.
While we generally suggest using asynchronous allocations, there are a lot of use cases where the user probably does not want to provide a stream but only a resource
bernhardmgruber commentedon Feb 19, 2025
Putting the memory resource as execution policy as first argument, would side-step this issue.
miscco commentedon Feb 19, 2025
I think we are having the same discussion as with the experimental container work.
In the end I believe we should have an environment that encapsulates different information like stream / memory resource and execution policy similar to what senders do
gonidelis commentedon Aug 22, 2025
@gevtushenko Would we like to close this in favor of #5635 ?
gevtushenko commentedon Aug 24, 2025
@gonidelis definitely!