XArith
mu::vdma::VpeContext Class Reference

Vector Processing Engine Context. More...

#include <vpe_context.hpp>

Classes

struct  Dot2Result
 Pair of scalar dot-product results returned by dot2(). More...
 

Public Member Functions

 VpeContext (VpeIdStrategy strategy=VpeIdStrategy::ByThreadId, DataType dataType=DataType::Fp32)
 Constructor with default strategy (ByThreadId) and element type (Fp32) More...
 
 VpeContext (VpeIdStrategy strategy, DataType dataType, volatile uint32_t *lockAddr)
 Constructor with an explicit lock address. More...
 
 ~VpeContext ()
 Destructor - automatically frees all allocated buffers. More...
 
 VpeContext (const VpeContext &)=delete
 
VpeContext & operator= (const VpeContext &)=delete
 
 VpeContext (VpeContext &&)=delete
 
VpeContext & operator= (VpeContext &&)=delete
 
Buffer Management

Allocation works in bytes.

A buffer of b bytes occupies ceil(b / 512) contiguous 512 B chunks; buffers of different sizes coexist freely. Use bytesForDimension() to size a buffer for a given element count.

uint64_t allocateBuffer (uint64_t byteSize)
 Allocate a buffer in SRAM. More...
 
bool tryAllocateBuffers (uint32_t count, uint64_t byteSize, uint64_t *outBuffers)
 Atomically allocate count equally-sized SRAM buffers (all-or-nothing) More...
 
bool tryAllocateBuffersWithSizes (uint32_t count, const uint64_t *byteSizes, uint64_t *outBuffers)
 Atomically allocate count SRAM buffers of per-buffer byte sizes. More...
 
uint64_t waitAllocateBuffer (uint64_t byteSize)
 Allocate a buffer, waiting until it fits. More...
 
bool waitAllocateBuffers (uint32_t count, uint64_t byteSize, uint64_t *outBuffers)
 Allocate count equally-sized buffers, waiting until they fit (all-or-nothing) More...
 
bool waitAllocateBuffersWithSizes (uint32_t count, const uint64_t *byteSizes, uint64_t *outBuffers)
 Allocate count buffers of per-buffer sizes, waiting until they fit (all-or-nothing) More...
 
void freeBuffer (uint64_t buffer)
 Free a previously allocated buffer. More...
 
void freeBuffers (uint32_t count, const uint64_t *buffers)
 Free multiple previously allocated buffers in a single atomic operation. More...
 
Data Transfer
void load (const void *srcDram, uint64_t dstBuffer, dim_t dimension)
 Load vector data from DRAM to SRAM buffer (synchronous). More...
 
void store (uint64_t srcBuffer, void *dstDram, dim_t dimension)
 Store vector data from SRAM buffer to DRAM (synchronous). More...
 
void copy (uint64_t src, uint64_t dst, dim_t dimension)
 Copy a vector between two SRAM buffers (synchronous). More...
 
void fillZero (uint64_t dst, dim_t dimension)
 Fill an SRAM buffer with the value zero (synchronous). More...
 
void fillOne (uint64_t dst, dim_t dimension)
 Fill an SRAM buffer with the value one (synchronous). More...
 
Vector Operations

Every operation processes dimension elements; the referenced buffers must each have capacity for at least dimension elements (bytesForDimension(dimension) bytes).

float dot (uint64_t src1, uint64_t src2, dim_t dimension)
 Compute dot product of two vectors (synchronous) More...
 
Dot2Result dot2 (uint64_t shared, uint64_t src1, uint64_t src2, dim_t dimension)
 Compute two dot products that share one operand in a single fused operation (synchronous). More...
 
void add (uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
 Element-wise addition: dst = src1 + src2 (synchronous) More...
 
void sub (uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
 Element-wise subtraction: dst = src1 - src2 (synchronous) More...
 
void mul (uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
 Element-wise multiplication: dst = src1 * src2 (synchronous) More...
 
void div (uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
 Element-wise division: dst = src1 / src2 (synchronous) More...
 
void square (uint64_t src, uint64_t dst, dim_t dimension)
 Element-wise square: dst = src * src (synchronous) More...
 
void bitwiseXor (uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
 Element-wise bitwise XOR: dst = src1 ^ src2 (synchronous) More...
 
Reduce Operations
float addReduce (uint64_t src1, uint64_t src2, dim_t dimension)
 Sum of element-wise addition: result = Σ(src1[i] + src2[i]) (synchronous) More...
 
float subReduce (uint64_t src1, uint64_t src2, dim_t dimension)
 Sum of element-wise subtraction: result = Σ(src1[i] - src2[i]) (synchronous) More...
 
float divReduce (uint64_t src1, uint64_t src2, dim_t dimension)
 Sum of element-wise division: result = Σ(src1[i] / src2[i]) (synchronous) More...
 
Scalar-Vector Operations

Element-wise ops between a broadcast scalar and a vector, in the hardware's native order dst[i] = scalar OP src[i] (the scalar is the LHS operand).

The caller supplies the scalar in SPB. The mx1p VPER has no scalar- immediate field – it reads the scalar from the first element of an SPB buffer. So before calling, load the scalar into a (>= 1 element) buffer yourself – e.g. load(scalarDram, scalar, 1) from your own DRAM, or point scalar at a prior op's SPB output. These ops do NOT stage, convert, or allocate; each binds scalar as the LHS and runs. scalar must hold the value in the context's element type (a half for an FP16 context; raw mask bits for scalarXor()), and must not alias dst.

Scalar-LHS only. Only scalar OP src[i] is provided. For the reversed src[i] OP scalar order, pre-transform the scalar you load: src - s is scalarAdd() with -s loaded; src / s is scalarMul() with 1/s loaded.

void scalarAdd (uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
 Add a scalar to every element: dst = scalar + src (synchronous) More...
 
void scalarSub (uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
 Subtract each element from a scalar: dst = scalar - src (synchronous) More...
 
void scalarMul (uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
 Multiply every element by a scalar: dst = scalar * src (synchronous) More...
 
void scalarDiv (uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
 Divide a scalar by each element: dst = scalar / src (synchronous) More...
 
void scalarXor (uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
 Bitwise XOR every element with a scalar mask: dst = scalar ^ src (synchronous) More...
 
float scalarDot (uint64_t scalar, uint64_t src, dim_t dimension)
 Scalar-weighted sum: result = Σ(scalar * src[i]) (synchronous) More...
 
Comparison Operations
bool equals (uint64_t src1, uint64_t src2, dim_t dimension)
 Check if two vectors are equal element-wise (synchronous) More...
 
bool isAllZero (uint64_t src, dim_t dimension)
 Check if all elements in a vector are zero (synchronous) More...
 
Accessors
uint64_t bytesForDimension (dim_t dimension) const
 Bytes occupied by dimension elements of this context's DataType. More...
 
DataType getDataType () const
 Get the element data type configured for this context. More...
 

Static Public Member Functions

Internal / Experimental
static void _reset ()
 [DANGEROUS] Reset the entire SRAM allocator state for the calling thread's execution unit More...
 

Static Public Attributes

static constexpr uint32_t MAX_TRACKED_BUFFERS = 160
 
static constexpr uint64_t INVALID_BUFFER = static_cast<uint64_t>(-1)
 

Detailed Description

Vector Processing Engine Context.

VpeContext integrates SRAM buffer allocation and vector functions into a single easy-to-use interface. It manages SRAM buffer allocation and provides synchronous vector operations (dot product, arithmetic, reduction, comparison).

This class provides automatic resource management with RAII semantics: all allocated buffers are automatically freed when the context is destroyed.

Two unit conventions, deliberately separated:

  • Allocation and free work in bytes. allocateBuffer()/tryAllocateBuffers() take a byte size, which the allocator rounds up to whole 512 B chunks. Buffers of different sizes coexist freely in one context. The context records each buffer's chunk count, so freeBuffer() needs no size argument.
  • Operations work in elements (dimension). Every load/store/op takes an explicit dimension and processes dimension * elementSize(dataType) bytes, which must fit within the buffer's allocated bytes. The DataType, fixed at construction, is the single source of truth for the element width and has no per-call override.

Use bytesForDimension() to size an allocation for a given element count.

Note
Thread-safety: VpeContext is task-local by design and must be constructed on the owning task's stack – it was never intended to be shared across threads. The allocator state is shared across all VpeContext instances on the same VPE and is guarded by an internal lock. The local allocatedOffsets_ tracker, however, is not synchronized; sharing one VpeContext between threads will race on the tracker even though the shared allocator state itself stays consistent.
Warning
Do not mix FP16 and FP32 contexts on the same VPE. All contexts sharing a VPE must use the same DataType.
Note
Buffer capacity: Each VpeContext shares a per-VPE allocator with a fixed 512-byte chunk granularity and MAX_TRACKED_BUFFERS (160) chunks total (80 KB). A buffer of b bytes occupies ceil(b / 512) contiguous chunks, so the upper bound on simultaneous buffers is the chunk pool divided by the per-buffer chunk count (160 buffers for payloads <= 512 B). Attempting to allocate beyond that returns INVALID_BUFFER.
Warning
All buffer offsets must be obtained via allocateBuffer(). Passing arbitrary values will cause undefined behavior.

Usage example:

auto query = ctx.allocateBuffer(ctx.bytesForDimension(dimension));
auto target = ctx.allocateBuffer(ctx.bytesForDimension(dimension));
auto diff = ctx.allocateBuffer(ctx.bytesForDimension(dimension));
ctx.load(queryDram, query, dimension);
ctx.load(targetDram, target, dimension);
ctx.sub(target, query, diff, dimension);
float result = ctx.dot(diff, diff, dimension);
// A shorter prefix over the same buffers -- just pass a smaller dimension.
float partial = ctx.dot(query, target, prefixDim);
// Destructor automatically frees all buffers
VpeContext(VpeIdStrategy strategy=VpeIdStrategy::ByThreadId, DataType dataType=DataType::Fp32)
Constructor with default strategy (ByThreadId) and element type (Fp32)
@ ByClusterId
spreads work from different clusters across engines
See also
VpeIdStrategy for VPE assignment strategy options

Definition at line 82 of file vpe_context.hpp.

Constructor & Destructor Documentation

◆ VpeContext() [1/4]

mu::vdma::VpeContext::VpeContext ( VpeIdStrategy  strategy = VpeIdStrategy::ByThreadId,
DataType  dataType = DataType::Fp32 
)
explicit

Constructor with default strategy (ByThreadId) and element type (Fp32)

Parameters
strategyVPE assignment strategy (default: ByThreadId)
dataTypeElement data type (default: Fp32). Fixed for the lifetime of the context: it is the per-element width used by every operation and by bytesForDimension(), and has no per-call override.
Postcondition
Context is ready for buffer allocation and operations
Note
Uses the default allocator lock for synchronization
Warning
All contexts sharing a VPE must use the same DataType. Do not mix FP16 and FP32 contexts on one VPE.
See also
VpeIdStrategy for strategy selection guidance

◆ VpeContext() [2/4]

mu::vdma::VpeContext::VpeContext ( VpeIdStrategy  strategy,
DataType  dataType,
volatile uint32_t *  lockAddr 
)

Constructor with an explicit lock address.

Parameters
strategyVPE assignment strategy
dataTypeElement data type
lockAddrCustom lock address (if nullptr, uses the default)
Postcondition
Context is ready for buffer allocation and operations
Note
Advanced usage: lets callers supply their own allocator lock for specialized scenarios. Most callers should use the other constructor.

◆ ~VpeContext()

mu::vdma::VpeContext::~VpeContext ( )

Destructor - automatically frees all allocated buffers.

◆ VpeContext() [3/4]

mu::vdma::VpeContext::VpeContext ( const VpeContext &  )
delete

◆ VpeContext() [4/4]

mu::vdma::VpeContext::VpeContext ( VpeContext &&  )
delete

Member Function Documentation

◆ _reset()

static void mu::vdma::VpeContext::_reset ( )
static

[DANGEROUS] Reset the entire SRAM allocator state for the calling thread's execution unit

Clears all allocator state owned by the execution unit that the calling thread runs on, marking every buffer it tracks as free. The reset covers the full unit, not just the caller's own VPE.

Postcondition
All buffers managed by that unit are marked as free
Warning
MUST ONLY be called from a dedicated reset/init kernel. Calling this while another kernel is using VpeContext buffers will corrupt allocator state and cause undefined behavior.
This is not a normal operational API. It exists solely for host-driven initialization between kernel invocations.

◆ add()

void mu::vdma::VpeContext::add ( uint64_t  src1,
uint64_t  src2,
uint64_t  dst,
dim_t  dimension 
)

Element-wise addition: dst = src1 + src2 (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
src1, src2, dst must be valid offsets from allocateBuffer()
Postcondition
dst[i] = src1[i] + src2[i] for all i in [0, dimension)
Note
dst can be the same as src1 or src2 for in-place operation
This operation blocks until computation completes
See also
addReduce() for sum-reduction variant
sub() for subtraction

◆ addReduce()

float mu::vdma::VpeContext::addReduce ( uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Sum of element-wise addition: result = Σ(src1[i] + src2[i]) (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dimensionElement count
Returns
Sum of element-wise additions across all dimensions
Precondition
src1 and src2 must be valid offsets from allocateBuffer()
Postcondition
Returns scalar sum, buffers remain unchanged
Note
This operation blocks until computation completes
Equivalent to: float sum = 0; for (i) sum += src1[i] + src2[i];
See also
add() for element-wise addition without reduction

◆ allocateBuffer()

uint64_t mu::vdma::VpeContext::allocateBuffer ( uint64_t  byteSize)

Allocate a buffer in SRAM.

Parameters
byteSizeBuffer size in bytes; must be > 0. Rounded up to whole 512 B chunks. Use bytesForDimension() to size for an element count.
Returns
Buffer offset (SRAM byte address), or INVALID_BUFFER (-1ULL) on failure
Postcondition
If successful, returns a valid buffer offset that can be used in operations
If failed, returns INVALID_BUFFER (byteSize is 0, no free run of the required chunk count, or the payload exceeds the 80 KB chunk pool)
Warning
Check for INVALID_BUFFER return before using the buffer offset
Note
Internal locking protects the shared per-VPE allocator. Each task must use its own VpeContext; do not share this instance across tasks.
Buffers of different sizes can coexist; each occupies only the chunks its own byte size needs. The context tracks each buffer's size internally, so freeBuffer() needs no size argument.
See also
freeBuffer() to release allocated buffers
bytesForDimension() to size a buffer for an element count

◆ bitwiseXor()

void mu::vdma::VpeContext::bitwiseXor ( uint64_t  src1,
uint64_t  src2,
uint64_t  dst,
dim_t  dimension 
)

Element-wise bitwise XOR: dst = src1 ^ src2 (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
src1, src2, dst must be valid offsets from allocateBuffer()
Postcondition
dst[i] = src1[i] ^ src2[i] (bitwise XOR) for all i in [0, dimension)
Note
dst can be the same as src1 or src2 for in-place operation
This operation blocks until computation completes
Operates on the raw bits of each element regardless of DataType

◆ bytesForDimension()

uint64_t mu::vdma::VpeContext::bytesForDimension ( dim_t  dimension) const
inline

Bytes occupied by dimension elements of this context's DataType.

Parameters
dimensionElement count
Returns
dimension * elementSize(getDataType()). Pass the result to allocateBuffer()/tryAllocateBuffers() to size a buffer for that many elements.

Definition at line 918 of file vpe_context.hpp.

◆ copy()

void mu::vdma::VpeContext::copy ( uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Copy a vector between two SRAM buffers (synchronous).

Copies dimension * elementSize(getDataType()) bytes from src to dst entirely within SRAM (no DRAM round-trip). The source buffer is left unchanged – this is a copy, not a move of ownership.

Parameters
srcSource buffer offset (from allocateBuffer()).
dstDestination buffer offset (from allocateBuffer()).
dimensionNumber of elements to copy.
Precondition
src and dst must be valid offsets from allocateBuffer(), each with capacity for at least dimension elements (bytesForDimension(dimension) bytes).
The two buffers must not overlap.
Postcondition
dst holds a copy of the first dimension elements of src; src is unchanged.
Note
Blocks until the copy completes.
See also
load()/store() for the DRAM<->SRAM transfers.

◆ div()

void mu::vdma::VpeContext::div ( uint64_t  src1,
uint64_t  src2,
uint64_t  dst,
dim_t  dimension 
)

Element-wise division: dst = src1 / src2 (synchronous)

Parameters
src1First source buffer offset (dividend)
src2Second source buffer offset (divisor)
dstDestination buffer offset
dimensionElement count
Precondition
src1, src2, dst must be valid offsets from allocateBuffer()
src2[i] != 0 for all i (division by zero is undefined behavior)
Postcondition
dst[i] = src1[i] / src2[i] for all i in [0, dimension)
Note
dst can be the same as src1 or src2 for in-place operation
This operation blocks until computation completes
Warning
Division by zero produces undefined results
See also
divReduce() for sum-reduction variant
mul() for multiplication

◆ divReduce()

float mu::vdma::VpeContext::divReduce ( uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Sum of element-wise division: result = Σ(src1[i] / src2[i]) (synchronous)

Parameters
src1First source buffer offset (dividend)
src2Second source buffer offset (divisor)
dimensionElement count
Returns
Sum of element-wise divisions across all dimensions
Precondition
src1 and src2 must be valid offsets from allocateBuffer()
src2[i] != 0 for all i (division by zero is undefined behavior)
Postcondition
Returns scalar sum, buffers remain unchanged
Note
This operation blocks until computation completes
Equivalent to: float sum = 0; for (i) sum += src1[i] / src2[i];
Warning
Division by zero produces undefined results
See also
div() for element-wise division without reduction

◆ dot()

float mu::vdma::VpeContext::dot ( uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Compute dot product of two vectors (synchronous)

Parameters
src1First vector buffer offset
src2Second vector buffer offset
dimensionElement count
Returns
Dot product result: Σ(src1[i] * src2[i])
Precondition
src1 and src2 must be valid offsets from allocateBuffer()
Postcondition
Returns the computed dot product, buffers remain unchanged
Note
This operation blocks until computation completes
Commonly used for distance calculations: distance = dot(diff, diff) for L2
See also
sub() to compute difference vectors first

◆ dot2()

Dot2Result mu::vdma::VpeContext::dot2 ( uint64_t  shared,
uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Compute two dot products that share one operand in a single fused operation (synchronous).

Computes both shared.src1 and shared.src2 in a single fused operation. Behaves like the other sync ops – blocks until done, returns both scalars – but yields roughly 2x the throughput of two separate dot products since one operation produces both reductions.

Parameters
sharedBuffer offset of the operand shared by both products
src1Buffer offset of the first non-shared operand
src2Buffer offset of the second non-shared operand
dimensionElement count shared by all three operands
Returns
Dot2Result {r1, r2} with r1 = shared . src1, r2 = shared . src2
Precondition
All three buffers must be valid offsets from allocateBuffer()
Postcondition
Returns both dot products, buffers remain unchanged
Note
Use this whenever two dot products share an operand – e.g. matching one vector against two candidates in batched search, or any 2-way reduction with a common factor.
Operates synchronously from the caller's view; the underlying path issues the operation asynchronously and inline-drains both completions, so callers must not have other in-flight async ops on this context.
See also
dot() for the single-product variant

◆ equals()

bool mu::vdma::VpeContext::equals ( uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Check if two vectors are equal element-wise (synchronous)

Parameters
src1First buffer offset
src2Second buffer offset
dimensionElement count
Returns
true if src1[i] == src2[i] for all i in [0, dimension), false otherwise
Precondition
src1 and src2 must be valid offsets from allocateBuffer()
Postcondition
Returns comparison result, buffers remain unchanged
Note
This operation blocks until computation completes
Uses exact floating-point equality (beware of precision issues)
Useful for validating computation results in tests
See also
isAllZero() to check if a vector is all zeros

◆ fillOne()

void mu::vdma::VpeContext::fillOne ( uint64_t  dst,
dim_t  dimension 
)

Fill an SRAM buffer with the value one (synchronous).

Writes the numeric value 1 to dimension elements starting at dst, using the context DataType as the element width: 1.0f for FP32 (bit pattern 0x3F800000) and FP16 1.0 (0x3C00). This is a numeric fill, not an all-ones bit pattern – the hardware Set1 function writes the element value 1 (confirmed on device). Useful for initializing a ones vector or a running accumulator.

Parameters
dstDestination buffer offset (from allocateBuffer()).
dimensionNumber of elements to fill.
Precondition
dst must be a valid offset from allocateBuffer() with capacity for at least dimension elements.
Postcondition
The first dimension elements of dst each equal 1.
Note
Blocks until the fill completes.
See also
fillZero() for the value-zero counterpart.

◆ fillZero()

void mu::vdma::VpeContext::fillZero ( uint64_t  dst,
dim_t  dimension 
)

Fill an SRAM buffer with the value zero (synchronous).

Writes the numeric value 0 to dimension elements starting at dst, using the context DataType as the element width (0.0f for FP32, FP16 +0.0). Equivalently, all-zero bytes over dimension * elementSize(getDataType()) bytes.

Parameters
dstDestination buffer offset (from allocateBuffer()).
dimensionNumber of elements to clear.
Precondition
dst must be a valid offset from allocateBuffer() with capacity for at least dimension elements.
Postcondition
The first dimension elements of dst each equal 0.
Note
Blocks until the fill completes.
See also
fillOne() for the value-one counterpart.

◆ freeBuffer()

void mu::vdma::VpeContext::freeBuffer ( uint64_t  buffer)

Free a previously allocated buffer.

Parameters
bufferBuffer offset returned by allocateBuffer()
Postcondition
If this context tracks buffer, it is returned to the available pool; otherwise the call is a no-op (see note)
Note
Internal locking protects the shared per-VPE allocator. Each task must use its own VpeContext; do not share this instance across tasks.
The buffer's chunk count is looked up in the context's local tracker; offsets this context does not track (already freed, INVALID_BUFFER, or foreign) are ignored
See also
allocateBuffer()

◆ freeBuffers()

void mu::vdma::VpeContext::freeBuffers ( uint32_t  count,
const uint64_t *  buffers 
)

Free multiple previously allocated buffers in a single atomic operation.

Parameters
countNumber of buffers to free
buffersArray of buffer offsets to free
Postcondition
Every listed offset this context tracks is returned to the available pool; untracked entries are skipped (see note)
Note
Holds the shared per-VPE allocator lock once across all frees. Each task must use its own VpeContext; do not share this instance across tasks.
More efficient than calling freeBuffer() multiple times
Untracked offsets (already freed, INVALID_BUFFER, foreign) are ignored
See also
tryAllocateBuffers()

◆ getDataType()

DataType mu::vdma::VpeContext::getDataType ( ) const
inline

Get the element data type configured for this context.

Definition at line 926 of file vpe_context.hpp.

◆ isAllZero()

bool mu::vdma::VpeContext::isAllZero ( uint64_t  src,
dim_t  dimension 
)

Check if all elements in a vector are zero (synchronous)

Parameters
srcBuffer offset
dimensionElement count
Returns
true if every element of src compares bit-equal to zero, false otherwise
Precondition
src must be a valid offset from allocateBuffer()
Postcondition
Returns comparison result, buffer remains unchanged
Note
This operation blocks until computation completes
Uses exact floating-point equality with zero
Useful for checking initialization or clearing operations
See also
equals() to compare two vectors

◆ load()

void mu::vdma::VpeContext::load ( const void *  srcDram,
uint64_t  dstBuffer,
dim_t  dimension 
)

Load vector data from DRAM to SRAM buffer (synchronous).

Transfers dimension * elementSize(getDataType()) bytes. The context's DataType is the single source of truth for the per-element width, so there is no element-typed overload – the DRAM endpoint is a raw byte pointer.

Parameters
srcDramSource buffer holding at least dimension elements of the context's DataType.
dstBufferDestination buffer offset (from allocateBuffer()).
dimensionNumber of elements to transfer.
Precondition
srcDram points to at least dimension * elementSize(getDataType()) readable bytes. The bytes are interpreted as the context's DataType without conversion. The API does not check the DRAM buffer length; a smaller buffer can be read out of bounds.
dstBuffer must be a valid offset from allocateBuffer() with capacity for at least dimension elements (bytesForDimension(dimension) bytes).
Postcondition
dstBuffer contains a copy of the data from srcDram.
Note
Blocks until the transfer completes.
See also
store() for the reverse operation.

◆ mul()

void mu::vdma::VpeContext::mul ( uint64_t  src1,
uint64_t  src2,
uint64_t  dst,
dim_t  dimension 
)

Element-wise multiplication: dst = src1 * src2 (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
src1, src2, dst must be valid offsets from allocateBuffer()
Postcondition
dst[i] = src1[i] * src2[i] for all i in [0, dimension)
Note
dst can be the same as src1 or src2 for in-place operation
This operation blocks until computation completes
See also
square() for src * src operation
div() for division

◆ operator=() [1/2]

VpeContext& mu::vdma::VpeContext::operator= ( const VpeContext &  )
delete

◆ operator=() [2/2]

VpeContext& mu::vdma::VpeContext::operator= ( VpeContext &&  )
delete

◆ scalarAdd()

void mu::vdma::VpeContext::scalarAdd ( uint64_t  scalar,
uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Add a scalar to every element: dst = scalar + src (synchronous)

Parameters
scalarSPB buffer the caller has pre-loaded with the scalar (element 0)
srcSource vector buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
scalar, src, dst are valid offsets from allocateBuffer(); scalar holds the scalar (see the group note on loading it)
Postcondition
dst[i] = scalar + src[i] for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes

◆ scalarDiv()

void mu::vdma::VpeContext::scalarDiv ( uint64_t  scalar,
uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Divide a scalar by each element: dst = scalar / src (synchronous)

A true per-element hardware divide (not a reciprocal-multiply), so it has no reciprocal-precision caveat.

Parameters
scalarSPB buffer the caller has pre-loaded with the scalar (element 0)
srcSource vector buffer offset (the per-element divisor)
dstDestination buffer offset
dimensionElement count
Precondition
scalar, src, dst are valid offsets from allocateBuffer(); scalar holds the scalar (see the group note on loading it)
src[i] != 0 for all i (per-element division by zero is undefined)
Postcondition
dst[i] = scalar / src[i] for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes
For the reversed order src / scalar, load 1/scalar and use scalarMul().
Warning
Any src[i] == 0 produces an infinity/NaN in dst[i]

◆ scalarDot()

float mu::vdma::VpeContext::scalarDot ( uint64_t  scalar,
uint64_t  src,
dim_t  dimension 
)

Scalar-weighted sum: result = Σ(scalar * src[i]) (synchronous)

Parameters
scalarSPB buffer the caller has pre-loaded with the scalar (element 0)
srcSource vector buffer offset
dimensionElement count
Returns
Σ(scalar * src[i]) over all elements (equivalently scalar · Σsrc[i])
Precondition
scalar and src are valid offsets from allocateBuffer(); scalar holds the scalar (see the group note on loading it)
Postcondition
Returns the scalar-weighted sum; src remains unchanged
Note
This operation blocks until computation completes
See also
scalarMul() for the non-reducing element-wise variant

◆ scalarMul()

void mu::vdma::VpeContext::scalarMul ( uint64_t  scalar,
uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Multiply every element by a scalar: dst = scalar * src (synchronous)

Parameters
scalarSPB buffer the caller has pre-loaded with the scalar (element 0)
srcSource vector buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
scalar, src, dst are valid offsets from allocateBuffer(); scalar holds the scalar (see the group note on loading it)
Postcondition
dst[i] = scalar * src[i] for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes

◆ scalarSub()

void mu::vdma::VpeContext::scalarSub ( uint64_t  scalar,
uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Subtract each element from a scalar: dst = scalar - src (synchronous)

Parameters
scalarSPB buffer the caller has pre-loaded with the scalar (element 0)
srcSource vector buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
scalar, src, dst are valid offsets from allocateBuffer(); scalar holds the scalar (see the group note on loading it)
Postcondition
dst[i] = scalar - src[i] for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes
For the reversed order src - scalar, load -scalar and use scalarAdd().

◆ scalarXor()

void mu::vdma::VpeContext::scalarXor ( uint64_t  scalar,
uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Bitwise XOR every element with a scalar mask: dst = scalar ^ src (synchronous)

Parameters
scalarSPB buffer the caller has pre-loaded with the raw mask bits (element 0)
srcSource vector buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
scalar, src, dst are valid offsets from allocateBuffer(); scalar holds the mask (see the group note on loading it)
Postcondition
dst[i] = scalar ^ src[i] (bitwise, on the raw element bits) for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes
Bitwise: the mask is used exactly as its raw element bits – load a 16-bit mask for an FP16 context, a 32-bit mask for FP32.

◆ square()

void mu::vdma::VpeContext::square ( uint64_t  src,
uint64_t  dst,
dim_t  dimension 
)

Element-wise square: dst = src * src (synchronous)

Parameters
srcSource buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
src and dst must be valid offsets from allocateBuffer()
Postcondition
dst[i] = src[i] * src[i] for all i in [0, dimension)
Note
dst can be the same as src for in-place operation
This operation blocks until computation completes
More efficient than mul(src, src, dst)
See also
mul() for general multiplication

◆ store()

void mu::vdma::VpeContext::store ( uint64_t  srcBuffer,
void *  dstDram,
dim_t  dimension 
)

Store vector data from SRAM buffer to DRAM (synchronous).

Transfers dimension * elementSize(getDataType()) bytes, using the context's DataType as the single source of truth for the per-element width (no element-typed overload).

Parameters
srcBufferSource buffer offset (from allocateBuffer()).
dstDramDestination buffer with space for at least dimension elements of the context's DataType.
dimensionNumber of elements to transfer.
Precondition
dstDram points to at least dimension * elementSize(getDataType()) writable bytes. The bytes use the context's DataType without conversion. The API does not check the DRAM buffer length; a smaller buffer can be written out of bounds.
See also
load() for the reverse operation.

◆ sub()

void mu::vdma::VpeContext::sub ( uint64_t  src1,
uint64_t  src2,
uint64_t  dst,
dim_t  dimension 
)

Element-wise subtraction: dst = src1 - src2 (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dstDestination buffer offset
dimensionElement count
Precondition
src1, src2, dst must be valid offsets from allocateBuffer()
Postcondition
dst[i] = src1[i] - src2[i] for all i in [0, dimension)
Note
dst can be the same as src1 or src2 for in-place operation
This operation blocks until computation completes
Commonly used to compute difference vectors for distance metrics
See also
subReduce() for sum-reduction variant
add() for addition
dot() to compute squared distance after subtraction

◆ subReduce()

float mu::vdma::VpeContext::subReduce ( uint64_t  src1,
uint64_t  src2,
dim_t  dimension 
)

Sum of element-wise subtraction: result = Σ(src1[i] - src2[i]) (synchronous)

Parameters
src1First source buffer offset
src2Second source buffer offset
dimensionElement count
Returns
Sum of element-wise subtractions across all dimensions
Precondition
src1 and src2 must be valid offsets from allocateBuffer()
Postcondition
Returns scalar sum, buffers remain unchanged
Note
This operation blocks until computation completes
Equivalent to: float sum = 0; for (i) sum += src1[i] - src2[i];
See also
sub() for element-wise subtraction without reduction

◆ tryAllocateBuffers()

bool mu::vdma::VpeContext::tryAllocateBuffers ( uint32_t  count,
uint64_t  byteSize,
uint64_t *  outBuffers 
)

Atomically allocate count equally-sized SRAM buffers (all-or-nothing)

Attempts to reserve count buffers under a single lock acquisition. Either all buffers are allocated or none are – preventing partial allocation that can lead to deadlock when multiple tasks compete for limited SRAM.

Parameters
countNumber of buffers to allocate, up to MAX_TRACKED_BUFFERS (the chunk pool bounds how many actually fit). 0 is accepted and returns true immediately (nothing to reserve).
byteSizeByte size for every buffer in this batch; must be > 0
[out]outBuffersArray of at least count elements to receive buffer offsets
Returns
true if all buffers were allocated, false if not enough free chunks
Postcondition
If true, outBuffers[0..count-1] contain valid buffer offsets
If false, no buffers are allocated (caller holds nothing)
Note
Internal locking protects the shared per-VPE allocator. Each task must use its own VpeContext; do not share this instance across tasks.
Preferred over multiple allocateBuffer() calls to avoid deadlock
See also
tryAllocateBuffersWithSizes() for per-buffer sizes
freeBuffers() to release multiple buffers atomically

◆ tryAllocateBuffersWithSizes()

bool mu::vdma::VpeContext::tryAllocateBuffersWithSizes ( uint32_t  count,
const uint64_t *  byteSizes,
uint64_t *  outBuffers 
)

Atomically allocate count SRAM buffers of per-buffer byte sizes.

Like tryAllocateBuffers(count, byteSize, outBuffers), but each buffer gets its own size from byteSizes[i] – e.g. one query buffer plus two smaller candidate buffers, reserved all-or-nothing.

Parameters
countNumber of buffers, up to MAX_TRACKED_BUFFERS. 0 is accepted and returns true immediately (nothing to reserve).
byteSizesPer-buffer byte sizes; every entry must be > 0
[out]outBuffersArray of at least count buffer offsets
Returns
true if all buffers were allocated, false otherwise (no change)
Note
Internal locking protects the shared per-VPE allocator. Each task must use its own VpeContext; do not share this instance across tasks.
See also
tryAllocateBuffers() when every buffer is the same size

◆ waitAllocateBuffer()

uint64_t mu::vdma::VpeContext::waitAllocateBuffer ( uint64_t  byteSize)

Allocate a buffer, waiting until it fits.

The blocking counterpart of allocateBuffer(). Where a caller would otherwise write while ((b = allocateBuffer(n)) == INVALID_BUFFER) {}, this waits between attempts instead of retrying flat out.

That matters for more than throughput. A flat-out retry loop issues allocator atomics continuously, and on MX1 those are served by the same L3 ALUs the buffer holders need to run and free – a full pool plus enough spinners can starve the holders badly enough that nobody makes progress. Backing off removes that feedback loop.

Parameters
byteSizeBuffer size in bytes; must be > 0. Rounded up to whole 512 B chunks.
Returns
Buffer offset once the allocation succeeds, or INVALID_BUFFER if the request can never be satisfied (see warning)
Warning
Returns INVALID_BUFFER only for a request that would not fit even in a completely empty pool (byteSize is 0 or exceeds the 80 KB chunk pool). Any request that could eventually fit is waited on indefinitely – this call does not time out. Size your task count against the pool (or use tryAllocate* and handle the failure) if "eventually" is not acceptable.
Note
Zero added cost when the pool has room: the wait only happens after a failed attempt. How long it waits is an internal, tuned property of the allocator, not a caller-visible knob.
See also
allocateBuffer() for the non-blocking form

◆ waitAllocateBuffers()

bool mu::vdma::VpeContext::waitAllocateBuffers ( uint32_t  count,
uint64_t  byteSize,
uint64_t *  outBuffers 
)

Allocate count equally-sized buffers, waiting until they fit (all-or-nothing)

The blocking counterpart of tryAllocateBuffers(); replaces the while (!tryAllocateBuffers(...)) {} idiom. Each attempt is all-or-nothing, so the caller holds nothing while waiting and the loop cannot deadlock against its peers.

Parameters
countNumber of buffers to allocate, up to MAX_TRACKED_BUFFERS. 0 is accepted and returns immediately (nothing to reserve).
byteSizeByte size for every buffer in this batch; must be > 0
[out]outBuffersArray of at least count elements to receive buffer offsets
Returns
true once all buffers are allocated; false if the request can never be satisfied (see warning)
Warning
Returns false only for a request that could not fit even in a completely empty pool (count above MAX_TRACKED_BUFFERS, byteSize is 0, or the batch exceeds the chunk pool). count == 0 is a vacuous success – it returns true, matching tryAllocateBuffers(). Any request that could eventually fit is waited on indefinitely – this call does not time out.
See also
tryAllocateBuffers() for the non-blocking form
waitAllocateBuffersWithSizes() for per-buffer sizes

◆ waitAllocateBuffersWithSizes()

bool mu::vdma::VpeContext::waitAllocateBuffersWithSizes ( uint32_t  count,
const uint64_t *  byteSizes,
uint64_t *  outBuffers 
)

Allocate count buffers of per-buffer sizes, waiting until they fit (all-or-nothing)

Like waitAllocateBuffers(), but each buffer gets its own size from byteSizes[i].

Parameters
countNumber of buffers, up to MAX_TRACKED_BUFFERS. 0 is accepted and returns immediately (nothing to reserve).
byteSizesPer-buffer byte sizes; every entry must be > 0
[out]outBuffersArray of at least count buffer offsets
Returns
true once all buffers are allocated; false if the request can never be satisfied
Warning
Same rejection and indefinite-wait contract as waitAllocateBuffers(), including count == 0 returning true.
See also
tryAllocateBuffersWithSizes() for the non-blocking form

Member Data Documentation

◆ INVALID_BUFFER

constexpr uint64_t mu::vdma::VpeContext::INVALID_BUFFER = static_cast<uint64_t>(-1)
staticconstexpr

Definition at line 92 of file vpe_context.hpp.

◆ MAX_TRACKED_BUFFERS

constexpr uint32_t mu::vdma::VpeContext::MAX_TRACKED_BUFFERS = 160
staticconstexpr

Definition at line 91 of file vpe_context.hpp.


The documentation for this class was generated from the following file: