Skip to content

[FEA]: Add memory resource-based overloads to CUB device-level algorithms #3855

Closed

Description

@gevtushenko

Is this a duplicate?

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_items
  • int* 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_LAUNCH mode
  • 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

  1. bernhardmgruber commented on Feb 19, 2025

    @bernhardmgruber
    Contributor

    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:

    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);

    should have an additional overload:

    template <typename ExecutionPolicy, typename IteratorT, typename NumItemsT>
    cudaError_t ExclusiveSum(ExecutionPolicy policy, // new new new
                             void *d_temp_storage,
                             size_t &temp_storage_bytes,
                             IteratorT d_data,
                             NumItemsT num_items,
                             cudaStream_t stream = 0);

    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 MemoryResourceT parameter, 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.

  2. miscco commented on Feb 19, 2025

    @miscco
    Contributor

    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

  3. bernhardmgruber commented on Feb 19, 2025

    @bernhardmgruber
    Contributor

    Putting the memory resource as execution policy as first argument, would side-step this issue.

  4. miscco commented on Feb 19, 2025

    @miscco
    Contributor

    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

  5. moved this from Todo to In Review in CCCLon Jul 2, 2025
  6. gonidelis commented on Aug 22, 2025

    @gonidelis
    Member

    @gevtushenko Would we like to close this in favor of #5635 ?

  7. gevtushenko commented on Aug 24, 2025

    @gevtushenko
    CollaboratorAuthor

    @gonidelis definitely!

  8. moved this from In Review to Done in CCCLon Aug 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions