Module Cuda.Deviceptr

This module introduces the type of pointers into on-device global memory, and stream-independent memory management functions. All functions from this module run synchronously. See: Memory Management.

type t

A pointer to a memory location on a device. See CUdeviceptr.

type region = {
  1. base : t;
  2. offset_bytes : int;
}

A non-owning borrow of a sub-region of a device allocation. The base Deviceptr.t retains ownership and its finalizer will free the allocation; the region is never freed independently and carries no finalizer. offset_bytes is the byte offset into base; offset 0 addresses the same location as base itself. Use offset to construct a region and region_of to inject a bare Deviceptr.t at offset 0.

val offset : t -> bytes:int -> region

offset ptr ~bytes:n returns a region starting n bytes into ptr's allocation. ptr retains ownership; the region is a non-owning borrow. bytes is in bytes. Offset 0 reproduces the same address as ptr.

val region_of : t -> region

region_of ptr is offset ptr ~bytes:0: a region at byte offset 0, i.e. the same address as ptr. Useful when a consuming site requires a region.

val sexp_of_t : t -> Sexplib0.Sexp.t
val equal : t -> t -> bool

Compares the pointer values for equality.

val hash : t -> int

Converts the pointer to an OCaml int using Unsigned.UInt64.to_int (truncating bits as needed).

val string_of : t -> string

Hexadecimal representation of the pointer.

val mem_alloc : size_in_bytes:int -> t

The memory is aligned, is not cleared. See cuMemAlloc.

The pointer is finalized using cuMemFree. This is safe without needing to set the proper context.

val mem_free : t -> unit

Double-freeing is prevented by a flag: multiple calls on the same Deviceptr.t are safe. See cuMemFree.

val memcpy_H_to_D_unsafe : dst:t -> src:unit Ctypes.ptr -> size_in_bytes:int -> unit
val memcpy_H_to_D : ?host_offset:int -> ?length:int -> ?dst_offset:int -> dst:t -> src:('a, 'b, 'c) Stdlib.Bigarray.Genarray.t -> unit -> unit

Copies the bigarray (or its interval) into the device memory. host_offset and length are in numbers of elements. dst_offset is a device-side byte offset into dst (default 0), letting the copy target a sub-region of a larger allocation; when length is not given the copied size is reduced by dst_offset so it does not write past the end of dst. See memcpy_H_to_D_unsafe.

val alloc_and_memcpy : ('a, 'b, 'c) Stdlib.Bigarray.Genarray.t -> t

Combines mem_alloc and memcpy_H_to_D.

val memcpy_D_to_H_unsafe : dst:unit Ctypes.ptr -> src:t -> size_in_bytes:int -> unit
val memcpy_D_to_H : ?host_offset:int -> ?length:int -> ?src_offset:int -> dst:('a, 'b, 'c) Stdlib.Bigarray.Genarray.t -> src:t -> unit -> unit

Copies from the device memory into the bigarray (or its interval). host_offset and length are in numbers of elements: length is the count of elements copied (not an end index), filling dst.(host_offset .. host_offset + length) and mirroring how memcpy_H_to_D reads src.(host_offset .. host_offset + length). src_offset is a device-side byte offset into src (default 0), letting the copy read from a sub-region of a larger allocation; when length is not given the copied size is reduced by src_offset so it does not read past the end of src. See memcpy_D_to_H_unsafe.

val memcpy_D_to_D : ?kind:('a, 'b) Stdlib.Bigarray.kind -> ?length:int -> ?size_in_bytes:int -> ?dst_offset:int -> ?src_offset:int -> dst:t -> src:t -> unit -> unit

Copies between two memory positions on the same device. The size to copy can optionally be provided in numbers of elements via kind and length. Provide either both kind and length, or just size_in_bytes. dst_offset and src_offset are device-side byte offsets into dst and src respectively (default 0), letting either end target a sub-region of a larger allocation. See cuMemcpyDtoD.

val memcpy_peer : ?kind:('a, 'b) Stdlib.Bigarray.kind -> ?length:int -> ?size_in_bytes:int -> ?dst_offset:int -> ?src_offset:int -> dst:t -> dst_ctx:Context.t -> src:t -> src_ctx:Context.t -> unit -> unit

Copies between memory positions on two different devices. The size to copy can optionally be provided in numbers of elements via kind and length. Provide either both kind and length, or just size_in_bytes. dst_offset and src_offset are device-side byte offsets into dst and src respectively (default 0), letting either end target a sub-region of a larger allocation. See cuMemcpyPeer.

val memset_d8 : ?offset:int -> t -> Unsigned.uchar -> length:int -> unit

offset is a device-side byte offset into t (default 0), letting the memset target a sub-region of a larger allocation. See cuMemsetD8.

val memset_d16 : ?offset:int -> t -> Unsigned.ushort -> length:int -> unit

length is in number of elements. offset is a device-side byte offset into t (default 0). See cuMemsetD16.

val memset_d32 : ?offset:int -> t -> Unsigned.uint32 -> length:int -> unit

length is in number of elements. offset is a device-side byte offset into t (default 0). See cuMemsetD32.