Skip to content

HMath.Tensor.Mutable

Module index · Numerical conventions

Description

Pinned CPU storage with shared mutable views. Views retain the same ForeignPtr, so garbage collection frees storage exactly once after the last viewcallback dies. Freezethaw copy; slices and buffer access are zero-copy. Values have their Storable ABI alignment. Callers synchronize concurrent writes.

On this page

API Purpose
MutableTensor Pinned CPU tensor with shared mutable views and garbage-collected ForeignPtr ownership.
newMutable Given checked dimensions then fill value, allocate pinned row-major storage filled with that value.
thawTensor Copy logical immutable tensor values to new contiguous pinned mutable storage.
freezeTensor Copy current logical view values into a new immutable tensor in row-major order.
mutableShape Return current mutable-view dimensions in axis order.
readMutable Given a mutable view then full zero-based coordinate list, read one element through the view strides.
writeMutable Given a mutable view, full coordinate list, then value, update that storage element.
modifyMutable Given view, coordinates, then transformation, read and replace an element.
sliceMutable Given axis, start, count, nonzero step, then view, return a zero-copy mutable slice retaining shared ForeignPtr ownership.
transposeMutable Reverse the order of all shape axes and strides, returning a zero-copy mutable view.
withMutableBuffer The pointer is valid only during the callback.
mutableAlignment Given a type Proxy, return the Storable ABI alignment in bytes.

Documentation

MutableTensor

data MutableTensor a Source #

Pinned CPU tensor with shared mutable views and garbage-collected ForeignPtr ownership. Freeze/thaw copy; callers synchronize concurrent writes.

newMutable

newMutable :: Storable a => [Int] -> a -> IO (Either MathError (MutableTensor a)) Source #

Given checked dimensions then fill value, allocate pinned row-major storage filled with that value. Shape/byte-size overflow returns Left MathError; allocation errors remain IO exceptions. Empty tensors are valid.

thawTensor

thawTensor :: Storable a => Tensor a -> IO (Either MathError (MutableTensor a)) Source #

Copy logical immutable tensor values to new contiguous pinned mutable storage. Byte-size overflow returns Left InvalidArgument; allocation failures remain IO exceptions. The source stays unchanged.

freezeTensor

freezeTensor :: Storable a => MutableTensor a -> IO (Either MathError (Tensor a)) Source #

Copy current logical view values into a new immutable tensor in row-major order. Return checked tensor construction errors; later mutable writes do not affect the result.

mutableShape

mutableShape :: MutableTensor a -> [Int] Source #

Return current mutable-view dimensions in axis order. Slices/transposes can change the view shape while sharing storage.

readMutable

readMutable :: Storable a => MutableTensor a -> [Int] -> IO (Either MathError a) Source #

Given a mutable view then full zero-based coordinate list, read one element through the view strides. Wrong rank or out-of-range coordinates return Left InvalidArgument. Callers synchronize concurrent writes.

writeMutable

writeMutable :: Storable a => MutableTensor a -> [Int] -> a -> IO (Either MathError ()) Source #

Given a mutable view, full coordinate list, then value, update that storage element. Invalid coordinates return Left InvalidArgument without writing; synchronize concurrent access externally.

modifyMutable

modifyMutable :: Storable a => MutableTensor a -> [Int] -> (a -> a) -> IO (Either MathError ()) Source #

Given view, coordinates, then transformation, read and replace an element. Invalid coordinates fail. This read-modify-write is not atomic across threads; callers provide synchronization.

sliceMutable

sliceMutable :: Int -> Int -> Int -> Int -> MutableTensor a -> Either MathError (MutableTensor a) Source #

Given axis, start, count, nonzero step, then view, return a zero-copy mutable slice retaining shared ForeignPtr ownership. Negative steps are supported if all reached indices stay in bounds. Invalid extent/axis or stride overflow fails.

transposeMutable

transposeMutable :: MutableTensor a -> MutableTensor a Source #

Reverse the order of all shape axes and strides, returning a zero-copy mutable view. For rank two this is a matrix transpose; storage ownership remains shared.

withMutableBuffer

withMutableBuffer :: Storable a => MutableTensor a -> (Ptr a -> [Int] -> [Int] -> IO b) -> IO b Source #

The pointer is valid only during the callback. Shape/strides describe views; callbacks must honor their extents and may not retain the borrowed pointer.

mutableAlignment

mutableAlignment :: Storable a => Proxy a -> Int Source #

Given a type Proxy, return the Storable ABI alignment in bytes. This reports the element type requirement, not a user-selectable alignment.