C++ · External Memory

External Memory

#include <numkong/memory.hpp> 8.0.0-rc.1 · b1f866ab

Every kernel takes plain pointers, so any CPU-accessible memory works: mmap, pinned buffers, CUDA unified memory, custom arenas. C++ views wrap any pointer without ownership. Owning containers accept any C++ Allocator.

template <typename T>
struct cuda_allocator {
    using value_type = T;
    T *allocate(std::size_t n) { T *p;
        cudaMallocManaged(&p, n * sizeof(T), cudaMemAttachGlobal); 
        return p; }
    void deallocate(T *p, std::size_t) noexcept { cudaFree(p); }
};

nk_dot_f32_best(cuda_managed_ptr, cuda_managed_ptr, 1024, &dot, capabilities, NULL); // C ABI, any pointer
auto view = nk::tensor_view<nk::f32_t>(mmap_ptr, rows, columns);                      // non-owning view
auto v = nk::vector<float, cuda_allocator<float>>::zeros(1024);                       // allocator-aware owning

API

Types

aligned_allocatorCache-aligned allocator with non-throwing allocation.
memory.hpp:30 ↗
template <typename value_type_,
          std::size_t alignment_ = nk_default_alignment_k>
struct aligned_allocator

Allocation returns nullptr on failure instead of throwing. It is stateless and always compares equal.

Fields

alignmentstd::size_t
allocatorAdapts a C allocation policy and caller-owned stream to the container allocator interface.
memory.hpp:71 ↗
template <typename value_type_>
struct allocator

Fields