Skip to content

Implement interface for external allocators - #2665

Open
sbaldu wants to merge 1 commit into
alpaka-group:developfrom
sbaldu:feature/caching-allocator-interface
Open

Implement interface for external allocators#2665
sbaldu wants to merge 1 commit into
alpaka-group:developfrom
sbaldu:feature/caching-allocator-interface

Conversation

@sbaldu

@sbaldu sbaldu commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

This PR adds to the allocation functions the possibility of passing an externally defined allocator like a caching allocator.
Since there is not a universal convention on the interface of device caching allocators, users are expected to wrap their allocators in a class respecting the concepts::CachingAllocator concept.
For example:

template <typename CachingAllocator>
class AllocatorWrapper {
private:
  std::reference_wrapper<CachingAllocator> m_allocator;
  typename CachingAllocator::Queue m_queue;

public:
  AllocatorWrapper(CachingAllocator& allocator, typename CachingAllocator::Queue queue)
      : m_allocator(allocator), m_queue(std::move(queue)) {}

  void* allocate(std::size_t bytes, std::size_t /* align */) {
    return m_allocator.get().allocate(bytes, m_queue);
  }

  void deallocate(void* ptr) { m_allocator.get().free(ptr); }
};

The allocator wrapper can then be constructed and passed to the allocation functions:

AllocatorWrapper<Alloc> wrapper(cachingAlloc, queue);
auto buf = alpaka::allocAsyncBuf<float, Idx>(queue, extent, wrapper);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant