C · External Memory

External Memory

#include <numkong/memory.h>#include <numkong/types.h> 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

nk_allocate_tAllocation callbacks retain the caller's context and use its device stream.
types.h:1448 ↗
typedef void *(*) nk_allocate_t(nk_size_t bytes, void *handle, nk_stream_t stream);
nk_allocator_tAllocation policy; the caller owns the handle and any stream passed to its callbacks.
types.h:1452 ↗
struct nk_allocator_t

Fields

allocatenk_allocate_t
freenk_free_t
handlevoid *

Functions

nk_allocator_init_heapHeap allocations with power-of-two alignment; invalid alignment preserves the policy.
memory.h:17 ↗
nk_status_t nk_allocator_init_heap(
    nk_allocator_t *allocator,
    nk_size_t alignment);
nk_allocator_init_arenaA fixed-capacity arena; invalid storage or alignment preserves both the policy and buffer.
memory.h:20 ↗
nk_status_t nk_allocator_init_arena(
    nk_allocator_t *allocator,
    void *buffer,
    nk_size_t bytes,
    nk_size_t alignment);
nk_allocator_init_unified_bestSelects host/device shared storage; failures preserve the allocator.
memory.h:24 ↗
nk_status_t nk_allocator_init_unified_best(
    nk_allocator_t *allocator,
    nk_capability_t capabilities);

Backends

  • nk_allocator_init_unifiedserial
nk_allocator_init_device_bestSelects device-local storage, or shared storage on Metal.
memory.h:27 ↗
nk_status_t nk_allocator_init_device_best(
    nk_allocator_t *allocator,
    nk_capability_t capabilities);

CPU masks have no device allocator.

nk_allocator_init_pinned_bestSelects pinned host storage on CUDA/ROCm or shared storage on Metal; unsupported on CPU.
memory.h:30 ↗
nk_status_t nk_allocator_init_pinned_best(
    nk_allocator_t *allocator,
    nk_capability_t capabilities);
nk_memory_allocate_unified_bestAllocates bytes that the host and the device of stream both address.
memory.h:73 ↗
nk_status_t nk_memory_allocate_unified_best(
    nk_size_t bytes,
    void **pointer,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_memory_allocate_unifiedserial

CUDA and ROCm hand out managed memory, CUDA from the device's managed pool in the order of stream, and Metal a shared buffer every kernel on that device binds, so the host reads what a kernel wrote once the stream is synchronized.

Parameters

bytesinThe size of the block, where zero hands out a null one.
pointeroutThe block, or null on failure. CPU storage uses nk_default_alignment_k alignment.
capabilitiesinThe group to allocate for, like nk_cuda_capabilities_enabled reports.
streaminNull on the CPU, or a stream of that group, naming the device.

Statuses

nk_memory_free_unified_bestReturns a block of nk_memory_allocate_unified_best once the work queued on stream is done with it.
memory.h:92 ↗
nk_status_t nk_memory_free_unified_best(
    void *pointer,
    nk_size_t bytes,
    nk_capability_t capabilities,
    nk_stream_t stream);

Backends

  • nk_memory_free_unifiedserial

CUDA frees blocks of its managed pool once the work queued on stream completes, without waiting; devices without concurrent managed access and ROCm wait for the device first; Metal releases the buffer once the work committed to stream so far completes.

Parameters

pointerinThe block, or null for none.
bytesinThe size it was allocated with.
capabilitiesinOne device's capabilities, like nk_cpu_capabilities_enabled reports.
streaminThe stream the block was allocated on, or null for the default.

Statuses

Macros

NUMKONG_DEFAULT_ALIGNMENTDefault allocation alignment, conservatively sized for cache interference.
types.h:982 ↗
#define NUMKONG_DEFAULT_ALIGNMENT 128