mod tensor

module tensor

DLPack tensor helpers for creating, freeing, and validating tensors.

This module provides the bridge between rgpot’s C API and the DLPack tensor exchange format. Two categories of tensors are supported:

  • Borrowed (non-owning): wraps an existing raw pointer. The deleter frees only the DLManagedTensorVersioned metadata, not the data.

  • Owned: wraps a Vec<T>. The deleter frees both metadata and data.

All exported extern "C" functions are collected by cbindgen into rgpot.h.

Functions

unsafe extern C fn rgpot_tensor_cpu_f64_2d(data: *mut f64, rows: i64, cols: i64) -> *mut DLManagedTensorVersioned

Create a non-owning 2-D f64 tensor on CPU wrapping an existing buffer.

The returned tensor borrows data — the caller must keep data alive for the lifetime of the tensor. Call rgpot_tensor_free when done.

Safety data must point to at least rows * cols contiguous f64 values.

unsafe extern C fn rgpot_tensor_cpu_f64_matrix3(data: *mut f64) -> *mut DLManagedTensorVersioned

Create a non-owning 2-D f64 tensor on CPU for a 3x3 matrix.

Convenience wrapper — equivalent to rgpot_tensor_cpu_f64_2d(data, 3, 3).

Safety data must point to at least 9 contiguous f64 values.

unsafe extern C fn rgpot_tensor_cpu_i32_1d(data: *mut c_int, len: i64) -> *mut DLManagedTensorVersioned

Create a non-owning 1-D i32 tensor on CPU wrapping an existing buffer.

Safety data must point to at least len contiguous c_int values.

unsafe extern C fn rgpot_tensor_data(tensor: *const DLManagedTensorVersioned) -> *const c_void

Get the raw data pointer of a DLPack tensor.

Safety tensor must be a valid, non-null DLManagedTensorVersioned*.

unsafe extern C fn rgpot_tensor_device(tensor: *const DLManagedTensorVersioned) -> DLDevice

Get the device of a DLPack tensor.

Safety tensor must be a valid, non-null DLManagedTensorVersioned*.

unsafe extern C fn rgpot_tensor_free(tensor: *mut DLManagedTensorVersioned)

Free a DLPack tensor by invoking its deleter.

If tensor is NULL, this is a no-op.

Safety tensor must have been obtained from one of the rgpot_tensor_* creation functions, or be a valid DLManagedTensorVersioned with a deleter.

unsafe extern C fn rgpot_tensor_owned_cpu_f64_2d(data: *const f64, rows: i64, cols: i64) -> *mut DLManagedTensorVersioned

Create an owning 2-D f64 tensor on CPU by copying data.

The returned tensor owns a copy of the data — the caller may free the original buffer after this call. Call rgpot_tensor_free when done.

Safety data must point to at least rows * cols contiguous f64 values.

unsafe extern C fn rgpot_tensor_shape(tensor: *const DLManagedTensorVersioned, ndim_out: *mut i32) -> *const i64

Get the shape array and number of dimensions of a DLPack tensor.

Writes the number of dimensions to *ndim_out and returns a pointer to the shape array (length *ndim_out).

Safety Both tensor and ndim_out must be valid, non-null pointers.