Table of Contents

Struct NDIterState

Namespace
NumSharp.Backends.Iteration
Assembly
NumSharp.dll

Core iterator state with dynamically allocated arrays for both dimensions and operands.

NUMSHARP DIVERGENCE: Unlike NumPy's fixed NPY_MAXDIMS=64 and NPY_MAXARGS=64, NumSharp supports unlimited dimensions AND unlimited operands. All arrays are allocated dynamically based on actual NDim and NOp values.

public struct NDIterState
Inherited Members
Extension Methods

Fields

ArrayWritebackPtrs

Array positions at buffer start, used for writeback. Stored separately from ResetDataPtrs which is the base for GotoIterIndex. Layout: [op0_ptr, op1_ptr, ...] Size = NOp.

public long* ArrayWritebackPtrs

Field Value

long*

BaseOffsets

Base offsets for each operand. Size = NOp.

public long* BaseOffsets

Field Value

long*

BufFlushed

1 when the current fill's WRITE operands have been flushed back to their arrays (or no fill is pending). Lets flush be idempotent so iternext, the single-inner-loop fast path, and Dispose can all call it safely. Matches the role of NumPy's "buffers written" tracking in npyiter_copy_from_buffers.

public byte BufFlushed

Field Value

byte

BufIterEnd

Current buffer iteration end.

public long BufIterEnd

Field Value

long

BufStrides

Buffer strides (always element size for contiguous buffers). Size = NOp.

public long* BufStrides

Field Value

long*

BufTransferSize

Number of elements in the current buffer fill (the inner-loop count the kernel must process). Matches NumPy's NBF_SIZE. For the non-reduce buffered path, BufIterEnd = fill-start IterIndex + BufTransferSize.

public long BufTransferSize

Field Value

long

BufferSize

Buffer size (elements per buffer).

public long BufferSize

Field Value

long

Buffers

Buffer pointers for each operand. Size = NOp.

public long* Buffers

Field Value

long*

Coords

Current coordinates. Dynamically allocated: size = NDim.

public long* Coords

Field Value

long*

CoreOffset

Offset into core (for partial buffer fills).

public long CoreOffset

Field Value

long

CorePos

Current position within core [0, CoreSize). Reset to 0 when advancing to next outer iteration. Used by IsFirstVisit - returns true only when CorePos = 0.

public long CorePos

Field Value

long

CoreSize

Inner loop size (number of inputs per output element). When reducing, Size is set to CoreSize and we iterate ReduceOuterSize times.

public long CoreSize

Field Value

long

DType

Legacy: primary dtype.

public NPTypeCode DType

Field Value

NPTypeCode

DataPtrs

Current data pointers for each operand. Size = NOp.

public long* DataPtrs

Field Value

long*

ElementSizes

Element sizes for each operand (based on buffer dtype). Size = NOp.

public int* ElementSizes

Field Value

int*

FlatIndex

Flat index for C_INDEX or F_INDEX tracking. Updated by Advance() when HASINDEX flag is set.

public long FlatIndex

Field Value

long

InnerStrides

Inner strides for each operand (gathered from main Strides array for fast access). Layout: [op0_inner_stride, op1_inner_stride, ...] Size = NOp.

public long* InnerStrides

Field Value

long*

IsCIndex

True if tracking C-order index, false for F-order. Only meaningful when HASINDEX flag is set.

public bool IsCIndex

Field Value

bool

ItFlags

Iterator flags (NDIterFlags bitmask).

public uint ItFlags

Field Value

uint

IterEnd

Range end for ranged iteration.

public long IterEnd

Field Value

long

IterIndex

Current iteration index.

public long IterIndex

Field Value

long

IterSize

Total number of iterations.

public long IterSize

Field Value

long

IterStart

Range start for ranged iteration.

public long IterStart

Field Value

long

MaskOp

Mask operand index (-1 if none).

public int MaskOp

Field Value

int

NDim

Number of dimensions after coalescing.

public int NDim

Field Value

int

NOp

Number of operands.

public int NOp

Field Value

int

OpDTypes

Buffer/target dtypes for each operand. Size = NOp.

public byte* OpDTypes

Field Value

byte*

OpItFlags

Per-operand flags. Size = NOp.

public ushort* OpItFlags

Field Value

ushort*

OpSrcDTypes

Source array dtypes for each operand (used for casting). Size = NOp.

public byte* OpSrcDTypes

Field Value

byte*

OuterDim

Which dimension is the reduce outer dimension. Used for stride calculation.

public int OuterDim

Field Value

int

Perm

Axis permutation (maps iterator axis to original axis). Dynamically allocated: size = NDim.

public sbyte* Perm

Field Value

sbyte*

ReduceOuterPtrs

Reset pointers for outer loop iteration. After completing inner loop, we advance these by ReduceOuterStrides. Layout: [op0_ptr, op1_ptr, ...] Size = NOp.

public long* ReduceOuterPtrs

Field Value

long*

ReduceOuterSize

Size of reduce outer loop (transfersize / CoreSize). Number of times to iterate the reduce dimension within buffer.

public long ReduceOuterSize

Field Value

long

ReduceOuterStrides

Outer strides for reduction (stride per reduce outer iteration). Layout: [op0_reduce_stride, op1_reduce_stride, ...] When stride is 0, the operand is a reduction target for that axis. Size = NOp.

public long* ReduceOuterStrides

Field Value

long*

ReducePos

Current position in reduce outer loop [0, ReduceOuterSize). Used by IsFirstVisit for buffered reduction.

public long ReducePos

Field Value

long

ResetDataPtrs

Reset data pointers (base + offset). Size = NOp.

public long* ResetDataPtrs

Field Value

long*

Shape

Shape after coalescing. Dynamically allocated: size = NDim.

public long* Shape

Field Value

long*

SrcElementSizes

Source element sizes for each operand (based on source dtype). Size = NOp.

public int* SrcElementSizes

Field Value

int*

StackAllocThreshold

Threshold for using stackalloc vs heap allocation for temporary buffers. Arrays with more dimensions than this will use heap allocation.

public const int StackAllocThreshold = 64

Field Value

int

Strides

Strides for each operand along each axis. Dynamically allocated: size = NDim * NOp. Layout: [op0_axis0, op0_axis1, ..., op1_axis0, op1_axis1, ...] Access: Strides[operand * NDim + axis]

Note: Unlike fixed layout which uses MaxDims spacing, dynamic layout packs strides contiguously based on actual NDim.

public long* Strides

Field Value

long*

StridesNDim

Allocated NDim for the Strides array. Used to compute correct offsets when NDim changes (e.g., after coalescing). Strides array maintains its original allocation size for safety.

public int StridesNDim

Field Value

int

Properties

Flags

Legacy: flags (lower bits of ItFlags).

public NDIterFlags Flags { readonly get; set; }

Property Value

NDIterFlags

IsContiguousCopy

Check if this is a contiguous copy operation (legacy).

public readonly bool IsContiguousCopy { get; }

Property Value

bool

Size

Legacy: total size (alias for IterSize).

public long Size { readonly get; set; }

Property Value

long

Methods

Advance()

Advance iterator by one position using ripple carry.

public void Advance()

AllocateDimArrays(int, int, int)

Allocate all dynamic arrays for given ndim and nop. Must be called before using any pointer fields. Initializes Perm to identity permutation [0, 1, 2, ...].

public void AllocateDimArrays(int ndim, int nop, int stridesNDim = -1)

Parameters

ndim int
nop int
stridesNDim int

BufferedReduceAdvance()

Buffered reduce iteration advance. Implements NumPy's double-loop pattern for efficient buffered reduction.

Returns:

  • 1: More elements in current buffer (inner or outer loop)
  • 0: Buffer exhausted, need to refill
  • -1: Iteration complete

Reference: numpy/_core/src/multiarray/nditer_templ.c.src lines 131-210

public int BufferedReduceAdvance()

Returns

int

FreeDimArrays()

Free all dynamically allocated arrays. Must be called before freeing the state itself.

public void FreeDimArrays()

GetArrayWritebackPtr(int)

Get array writeback pointer for operand.

public void* GetArrayWritebackPtr(int op)

Parameters

op int

Returns

void*

GetBufStride(int)

Get buffer stride for operand.

public long GetBufStride(int op)

Parameters

op int

Returns

long

GetBuffer(int)

Get buffer pointer for operand.

public void* GetBuffer(int op)

Parameters

op int

Returns

void*

GetCoordsPointer()

Get pointer to Coords array.

public long* GetCoordsPointer()

Returns

long*

GetDataPointer(int)

Get data pointer (legacy interface).

public readonly nint GetDataPointer(int operand)

Parameters

operand int

Returns

nint

GetDataPtr(int)

Get current data pointer for operand.

public void* GetDataPtr(int op)

Parameters

op int

Returns

void*

GetElementSize(int)

Get element size for operand.

public int GetElementSize(int op)

Parameters

op int

Returns

int

GetInnerStrideArray()

Get inner stride array pointer - returns contiguous array of inner strides for all operands. Layout: [op0_inner_stride, op1_inner_stride, ...]

public long* GetInnerStrideArray()

Returns

long*

GetOpDType(int)

Get operand dtype.

public NPTypeCode GetOpDType(int op)

Parameters

op int

Returns

NPTypeCode

GetOpFlags(int)

Get operand flags.

public NDIterOpFlags GetOpFlags(int op)

Parameters

op int

Returns

NDIterOpFlags

GetOpSrcDType(int)

Get source array dtype for operand.

public NPTypeCode GetOpSrcDType(int op)

Parameters

op int

Returns

NPTypeCode

GetReduceOuterPtr(int)

Get reduce outer pointer for operand.

public void* GetReduceOuterPtr(int op)

Parameters

op int

Returns

void*

GetReduceOuterStride(int)

Get reduce outer stride for operand.

public long GetReduceOuterStride(int op)

Parameters

op int

Returns

long

GetResetDataPtr(int)

Get reset data pointer for operand.

public void* GetResetDataPtr(int op)

Parameters

op int

Returns

void*

GetShapePointer()

Get pointer to Shape array.

public long* GetShapePointer()

Returns

long*

GetSrcElementSize(int)

Get source element size for operand.

public int GetSrcElementSize(int op)

Parameters

op int

Returns

int

GetStride(int, int)

Get stride for operand at axis.

public long GetStride(int axis, int op)

Parameters

axis int
op int

Returns

long

GetStridesPointer(int)

Get pointer to strides for a specific operand. Uses actual NDim (or StridesNDim if NDim changed after allocation).

public long* GetStridesPointer(int operand)

Parameters

operand int

Returns

long*

GotoIterIndex(long)

Jump to a specific iteration index.

public void GotoIterIndex(long iterindex)

Parameters

iterindex long

InitReduceOuterPtrs()

Initialize reduce outer pointers from current data pointers. Called after buffer fill to set up the outer loop start positions.

public void InitReduceOuterPtrs()

InitializeFlatIndex()

Initialize FlatIndex based on current coordinates. Should be called after HASINDEX flag is set and all axis setup is complete.

public void InitializeFlatIndex()

NeedsCast(int)

Check if operand needs casting (source dtype != buffer dtype).

public bool NeedsCast(int op)

Parameters

op int

Returns

bool

Reset()

Reset iterator to IterStart (which may be >0 for ranged iterators). Matches NumPy's NDIter_Reset + npyiter_goto_iterindex(ITERSTART) semantics.

public void Reset()

SetArrayWritebackPtr(int, void*)

Set array writeback pointer for operand.

public void SetArrayWritebackPtr(int op, void* ptr)

Parameters

op int
ptr void*

SetBufStride(int, long)

Set buffer stride for operand.

public void SetBufStride(int op, long stride)

Parameters

op int
stride long

SetBuffer(int, void*)

Set buffer pointer for operand.

public void SetBuffer(int op, void* ptr)

Parameters

op int
ptr void*

SetDataPointer(int, nint)

Set data pointer (legacy interface).

public void SetDataPointer(int operand, nint pointer)

Parameters

operand int
pointer nint

SetDataPtr(int, void*)

Set current data pointer for operand.

public void SetDataPtr(int op, void* ptr)

Parameters

op int
ptr void*

SetOpDType(int, NPTypeCode)

Set operand dtype (buffer/target dtype).

public void SetOpDType(int op, NPTypeCode dtype)

Parameters

op int
dtype NPTypeCode

SetOpFlags(int, NDIterOpFlags)

Set operand flags.

public void SetOpFlags(int op, NDIterOpFlags flags)

Parameters

op int
flags NDIterOpFlags

SetOpSrcDType(int, NPTypeCode)

Set source array dtype for operand.

public void SetOpSrcDType(int op, NPTypeCode dtype)

Parameters

op int
dtype NPTypeCode

SetReduceOuterPtr(int, void*)

Set reduce outer pointer for operand.

public void SetReduceOuterPtr(int op, void* ptr)

Parameters

op int
ptr void*

SetReduceOuterStride(int, long)

Set reduce outer stride for operand.

public void SetReduceOuterStride(int op, long stride)

Parameters

op int
stride long

SetResetDataPtr(int, void*)

Set reset data pointer for operand.

public void SetResetDataPtr(int op, void* ptr)

Parameters

op int
ptr void*

SetStride(int, int, long)

Set stride for operand at axis.

public void SetStride(int axis, int op, long value)

Parameters

axis int
op int
value long

UpdateInnerStrides()

Update the InnerStrides array from the main Strides array. Must be called after coalescing or axis removal.

public void UpdateInnerStrides()