Module Cuda_api.Memory

type 'a buffer = {
  1. ptr : Cuda_types.cu_deviceptr;
  2. size : int;
  3. elem_size : int;
  4. device : Device.t;
}
val alloc : Device.t -> int -> ('a, 'b) Stdlib.Bigarray.kind -> 'c buffer
val alloc_custom : Device.t -> size:int -> elem_size:int -> 'a buffer

Allocate buffer for custom types with explicit element size in bytes

val free : 'a buffer -> unit
val host_to_device : src:('a, 'b, Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array1.t -> dst:'c buffer -> unit
val device_to_host : src:'a buffer -> dst:('b, 'c, Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array1.t -> unit
val host_ptr_to_device : src_ptr:unit Ctypes_static.ptr -> byte_size:int -> dst:'a buffer -> unit

Transfer from raw pointer to device buffer (for custom types)

val device_to_host_ptr : src:'a buffer -> dst_ptr:unit Ctypes_static.ptr -> byte_size:int -> unit

Transfer from device buffer to raw pointer (for custom types)

val device_to_device : src:'a buffer -> dst:'b buffer -> unit
val memset : 'a buffer -> int -> unit

Pinned (page-locked) host memory

Page-locked host buffers let the driver DMA straight to/from the device without staging through an internal pageable bounce buffer, roughly doubling H2D/D2H bandwidth on PCIe-class links, and are the hard prerequisite for true async transfers (a pageable cuMemcpy*Async silently degrades to synchronous). Two shapes are exposed:

type pinned_host = {
  1. host_ptr : unit Ctypes.ptr;
  2. bytes : int;
}
val alloc_host : int -> pinned_host

Allocate bytes of page-locked host memory. Must be released with free_host, never Stdlib/free.

val free_host : pinned_host -> unit
val register_host : 'a Ctypes.ptr -> int -> unit

Page-lock bytes at an existing host pointer (flags = 0: portable, non-mapped). Pair with unregister_host.

val unregister_host : 'a Ctypes.ptr -> unit