Table of Contents

Class NDIterBufferManager

Namespace
NumSharp.Backends.Iteration
Assembly
NumSharp.dll

Buffer management for NDIter. Handles allocation, copy-in, and copy-out of iteration buffers.

public static class NDIterBufferManager
Inheritance
NDIterBufferManager
Inherited Members

Fields

Alignment

Required alignment for SIMD operations.

public const int Alignment = 64

Field Value

int

DefaultBufferSize

Built-in default buffer size (number of elements) — NumPy's NPY_BUFSIZE. Used whenever neither an explicit nditer buffersize nor the flow's override (setbufsize(long)) applies.

public const long DefaultBufferSize = 8192

Field Value

long

Properties

CurrentBufferSize

The current default NDIter/ufunc buffer size for the caller's flow — exactly the value getbufsize() returns (its whole body), and what the engine reads where NumPy's C code reads the extobj's buffer size. Equals the last setbufsize(long) value on this thread, or DefaultBufferSize (8192) when none was set.

public static long CurrentBufferSize { get; }

Property Value

long

Remarks

Changing it changes the buffer and chunk size of buffered iteration, and with it every answer that follows NumPy's inner-loop calls, as it does in NumPy: the summation order of a buffered flat sum, which call of the min / max reductions decides a tie or a NaN's bits, the fills a cast reduction or an out= round trip runs in, np.power's zero-exponent-step test.

Methods

AllocateAligned(long, NPTypeCode)

Allocate aligned buffer for an operand.

public static void* AllocateAligned(long elements, NPTypeCode dtype)

Parameters

elements long
dtype NPTypeCode

Returns

void*

AllocateAligned(long, int)

Allocates an aligned buffer of elements elements of elementSize bytes — the form every buffer of the iterator uses, because a fixed-width string's element size is its descriptor's (<U5 is 20 bytes) and its storage lane names none.

public static void* AllocateAligned(long elements, int elementSize)

Parameters

elements long

The element count.

elementSize int

The bytes per element.

Returns

void*

The buffer (free it with FreeAligned(void*)).

Exceptions

OutOfMemoryException

The allocation failed.

AllocateBuffers(ref NDIterState, long)

NumPy's npyiter_allocate_buffers: one buffer of BufferSize elements for every operand the buffering setup did not mark BUFNEVER; an operand that already has its buffer keeps it. A StringDType buffer starts zeroed (every cell the empty string, owning nothing — NumPy's NPY_NEEDS_INIT memset) with an allocator of its own.

public static bool AllocateBuffers(ref NDIterState state, long bufferSize)

Parameters

state NDIterState

The iterator state, after FindBufferingSetup(ref NDIterState, long) (which decides who is buffered and how many elements a fill covers).

bufferSize long

The element count for a state the setup never sized (a caller outside the iterator's own construction): used only when BufferSize is not positive, and then recorded there (0 or less picks DetermineBufferSize(ref NDIterState, long)'s heuristic).

Returns

bool

True when every buffer exists; false when an allocation failed (every buffer allocated so far is freed again, as NumPy's failure path does).

Remarks

The strides an inner loop sees are the setup's (BufStrides): a buffered operand's buffer element size, an unbuffered operand's own array stride. This method only provides the memory. A buffer is allocated for at least one element, so an empty iteration (a zero-element fill) still owns valid pointers.

CopyFromBuffer(ref NDIterState, int, long)

Copy data from buffer to operand (contiguous to strided). If operand needs casting, performs type conversion during copy. Runtime dtype dispatch version - handles any NumSharp dtype.

public static void CopyFromBuffer(ref NDIterState state, int op, long count)

Parameters

state NDIterState
op int
count long

CopyFromBufferWithCast(ref NDIterState, int, long)

Copy data from buffer to operand with type conversion.

public static void CopyFromBufferWithCast(ref NDIterState state, int op, long count)

Parameters

state NDIterState
op int
count long

CopyFromBuffer<T>(ref NDIterState, int, long)

Copy data from buffer to operand (contiguous to strided).

public static void CopyFromBuffer<T>(ref NDIterState state, int op, long count) where T : unmanaged

Parameters

state NDIterState
op int
count long

Type Parameters

T

CopyToBuffer(ref NDIterState, int, long)

Copy data from operand to buffer (strided to contiguous). If operand needs casting, performs type conversion during copy. Runtime dtype dispatch version - handles any NumSharp dtype.

public static void CopyToBuffer(ref NDIterState state, int op, long count)

Parameters

state NDIterState
op int
count long

CopyToBufferIfNeeded(ref NDIterState, int, long)

Copy data to buffer only if not reusable. Returns true if copy was performed, false if buffer was reused.

public static bool CopyToBufferIfNeeded(ref NDIterState state, int op, long count)

Parameters

state NDIterState
op int
count long

Returns

bool

CopyToBufferWithCast(ref NDIterState, int, long)

Copy data from operand to buffer with type conversion.

public static void CopyToBufferWithCast(ref NDIterState state, int op, long count)

Parameters

state NDIterState
op int
count long

CopyToBuffer<T>(ref NDIterState, int, long)

Copy data from operand to buffer (strided to contiguous).

public static void CopyToBuffer<T>(ref NDIterState state, int op, long count) where T : unmanaged

Parameters

state NDIterState
op int
count long

Type Parameters

T

DetermineBufferSize(ref NDIterState, long)

Determine optimal buffer size based on array sizes and cache.

public static long DetermineBufferSize(ref NDIterState state, long requestedSize)

Parameters

state NDIterState
requestedSize long

Returns

long

FreeAligned(void*)

Free aligned buffer.

public static void FreeAligned(void* buffer)

Parameters

buffer void*

FreeBuffers(ref NDIterState)

Free all allocated buffers.

public static void FreeBuffers(ref NDIterState state)

Parameters

state NDIterState

InvalidateAllBuffers(ref NDIterState)

Invalidate all buffers (e.g., after Reset or GotoIterIndex).

public static void InvalidateAllBuffers(ref NDIterState state)

Parameters

state NDIterState

InvalidateBuffer(ref NDIterState, int)

Clear buffer reusable flag (contents are no longer valid). Call this when the source data or iteration position changes.

public static void InvalidateBuffer(ref NDIterState state, int op)

Parameters

state NDIterState
op int

IsBufferReusable(ref NDIterState, int)

Check if operand buffer can be reused (contents still valid).

public static bool IsBufferReusable(ref NDIterState state, int op)

Parameters

state NDIterState
op int

Returns

bool

IsOperandIterLinear(ref NDIterState, int)

True when the operand's memory positions form one arithmetic progression over the entire (coalesced) iteration space — i.e. a single 1-D walk at the inner stride covers every element in order. Contiguous operands, constant-stride 1-D views, and fully-broadcast (all-stride-0) operands qualify; genuinely multi-dimensional strided operands do not.

public static bool IsOperandIterLinear(ref NDIterState state, int op)

Parameters

state NDIterState
op int

Returns

bool

MarkBufferReusable(ref NDIterState, int)

Mark operand buffer as reusable (contents are still valid). Call this after CopyToBuffer when the source data hasn't changed.

public static void MarkBufferReusable(ref NDIterState state, int op)

Parameters

state NDIterState
op int