XArith
vpe_context.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2026 XCENA Inc.
3 
4 #pragma once
5 
6 #include <stdint.h>
7 
8 #include "half.hpp"
9 #include "vpe_types.hpp"
10 
11 namespace mu
12 {
13 namespace vdma
14 {
15 
83 {
84 public:
85  // Worst-case depth of the local allocation tracker, sized to match
86  // the per-VPE chunk capacity (160) so the destructor can always clean
87  // up even when every buffer occupies a single chunk. Larger buffers (spanning
88  // N chunks) reduce the achievable count to `floor(160 / N)` -- the
89  // tracker stays sized for the worst case so destructor cleanup never
90  // overflows.
91  static constexpr uint32_t MAX_TRACKED_BUFFERS = 160;
92  static constexpr uint64_t INVALID_BUFFER = static_cast<uint64_t>(-1);
93 
97  struct Dot2Result
98  {
99  float r1;
100  float r2;
101  };
102 
120  DataType dataType = DataType::Fp32);
121 
133  VpeContext(VpeIdStrategy strategy, DataType dataType, volatile uint32_t* lockAddr);
134 
139 
140  // Non-copyable, non-movable
141  VpeContext(const VpeContext&) = delete;
142  VpeContext& operator=(const VpeContext&) = delete;
143  VpeContext(VpeContext&&) = delete;
145 
148 
165  static void _reset();
166 
168 
176 
201  uint64_t allocateBuffer(uint64_t byteSize);
202 
227  bool tryAllocateBuffers(uint32_t count, uint64_t byteSize, uint64_t* outBuffers);
228 
246  bool tryAllocateBuffersWithSizes(uint32_t count, const uint64_t* byteSizes, uint64_t* outBuffers);
247 
280  uint64_t waitAllocateBuffer(uint64_t byteSize);
281 
308  bool waitAllocateBuffers(uint32_t count, uint64_t byteSize, uint64_t* outBuffers);
309 
330  bool waitAllocateBuffersWithSizes(uint32_t count, const uint64_t* byteSizes, uint64_t* outBuffers);
331 
348  void freeBuffer(uint64_t buffer);
349 
366  void freeBuffers(uint32_t count, const uint64_t* buffers);
367 
369 
372 
398  void load(const void* srcDram, uint64_t dstBuffer, dim_t dimension);
399 
418  void store(uint64_t srcBuffer, void* dstDram, dim_t dimension);
419 
441  void copy(uint64_t src, uint64_t dst, dim_t dimension);
442 
461  void fillZero(uint64_t dst, dim_t dimension);
462 
483  void fillOne(uint64_t dst, dim_t dimension);
484 
486 
493 
511  float dot(uint64_t src1, uint64_t src2, dim_t dimension);
512 
542  Dot2Result dot2(uint64_t shared, uint64_t src1, uint64_t src2, dim_t dimension);
543 
561  void add(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension);
562 
582  void sub(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension);
583 
601  void mul(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension);
602 
623  void div(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension);
624 
641  void square(uint64_t src, uint64_t dst, dim_t dimension);
642 
658  void bitwiseXor(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension);
659 
661 
664 
682  float addReduce(uint64_t src1, uint64_t src2, dim_t dimension);
683 
701  float subReduce(uint64_t src1, uint64_t src2, dim_t dimension);
702 
723  float divReduce(uint64_t src1, uint64_t src2, dim_t dimension);
724 
726 
745 
761  void scalarAdd(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension);
762 
779  void scalarSub(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension);
780 
796  void scalarMul(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension);
797 
819  void scalarDiv(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension);
820 
839  void scalarXor(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension);
840 
858  float scalarDot(uint64_t scalar, uint64_t src, dim_t dimension);
859 
861 
864 
883  bool equals(uint64_t src1, uint64_t src2, dim_t dimension);
884 
903  bool isAllZero(uint64_t src, dim_t dimension);
904 
906 
909 
918  uint64_t bytesForDimension(dim_t dimension) const
919  {
920  return dimension * elementSize(dataType_);
921  }
922 
927  {
928  return dataType_;
929  }
930 
932 
933 private:
934  void initialize(VpeIdStrategy strategy, DataType dataType, volatile uint32_t* lockAddr);
935  uint64_t getNextTag();
936 
937  // Contiguous 512 B chunks a payload of `byteSize` occupies.
938  uint32_t chunksForBytes(uint64_t byteSize) const;
939 
940  // All-or-nothing reservation of `count` runs of chunks[i] chunks each.
941  bool tryAllocateChunks(uint32_t count, const uint32_t* chunks, uint64_t* outBuffers);
942 
943  // True when the request could be satisfied against a completely empty
944  // bitmap. A request that fails this can never succeed no matter how long
945  // the caller waits, so the blocking wrappers reject it instead of
946  // spinning forever, and tryAllocateChunks() uses it to bail before
947  // paying for the lock.
948  bool requestFitsEmptyPool(uint32_t count, const uint32_t* chunks) const;
949 
950  // Shared body of the waitAllocate* overloads: retry tryAllocateChunks()
951  // under the allocator's internal backoff policy until it succeeds. False
952  // only for a request that requestFitsEmptyPool() rejects.
953  bool waitAllocateChunks(uint32_t count, const uint32_t* chunks, uint64_t* outBuffers);
954 
955  // Append (offset, chunks) to the local allocation tracker; no-op when the
956  // tracker is already at MAX_TRACKED_BUFFERS capacity. Not thread-safe.
957  void trackAllocation(uint64_t offset, uint32_t chunks);
958 
959  // Swap-remove `offset` from the local allocation tracker and return its
960  // chunk count, or 0 if the offset is not currently tracked. Not thread-safe.
961  uint32_t untrackAllocation(uint64_t offset);
962 
963  // Debug-only sanity check: a per-call dimension must not push an
964  // operation past the chunks `buffer` was actually allocated for, or the
965  // op would read/write into the neighbouring buffer's chunks with the
966  // bitmap none the wiser. Looks `buffer` up in the local tracker; if it
967  // is tracked, asserts the requested dimension fits. Untracked offsets
968  // are left to the caller's "valid offset" contract. Compiles to nothing
969  // in release builds (NDEBUG).
970  void debugCheckDimFits(dim_t dimension, uint64_t buffer) const;
971 
972  DataType dataType_;
973  uint64_t vpeId_;
974  volatile int32_t* lock_;
975 
976  // Tracker: byte offsets of buffers this VpeContext currently owns and the
977  // chunk count each occupies (the single source of truth for how many bits
978  // to clear on free). The destructor releases them from the shared
979  // allocator; not synchronized, so a single VpeContext must be owned by a
980  // single thread.
981  uint64_t allocatedOffsets_[MAX_TRACKED_BUFFERS];
982  uint16_t allocatedChunks_[MAX_TRACKED_BUFFERS];
983  int allocatedCount_;
984  uint64_t currentTag_;
985 };
986 
987 } // namespace vdma
988 } // namespace mu
Vector Processing Engine Context.
Definition: vpe_context.hpp:83
void store(uint64_t srcBuffer, void *dstDram, dim_t dimension)
Store vector data from SRAM buffer to DRAM (synchronous).
void add(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
Element-wise addition: dst = src1 + src2 (synchronous)
void scalarSub(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
Subtract each element from a scalar: dst = scalar - src (synchronous)
VpeContext & operator=(VpeContext &&)=delete
DataType getDataType() const
Get the element data type configured for this context.
void freeBuffers(uint32_t count, const uint64_t *buffers)
Free multiple previously allocated buffers in a single atomic operation.
static constexpr uint32_t MAX_TRACKED_BUFFERS
Definition: vpe_context.hpp:91
bool isAllZero(uint64_t src, dim_t dimension)
Check if all elements in a vector are zero (synchronous)
float dot(uint64_t src1, uint64_t src2, dim_t dimension)
Compute dot product of two vectors (synchronous)
void scalarAdd(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
Add a scalar to every element: dst = scalar + src (synchronous)
float subReduce(uint64_t src1, uint64_t src2, dim_t dimension)
Sum of element-wise subtraction: result = Σ(src1[i] - src2[i]) (synchronous)
~VpeContext()
Destructor - automatically frees all allocated buffers.
bool equals(uint64_t src1, uint64_t src2, dim_t dimension)
Check if two vectors are equal element-wise (synchronous)
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)
void mul(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
Element-wise multiplication: dst = src1 * src2 (synchronous)
bool tryAllocateBuffers(uint32_t count, uint64_t byteSize, uint64_t *outBuffers)
Atomically allocate count equally-sized SRAM buffers (all-or-nothing)
void square(uint64_t src, uint64_t dst, dim_t dimension)
Element-wise square: dst = src * src (synchronous)
VpeContext(VpeIdStrategy strategy=VpeIdStrategy::ByThreadId, DataType dataType=DataType::Fp32)
Constructor with default strategy (ByThreadId) and element type (Fp32)
void fillOne(uint64_t dst, dim_t dimension)
Fill an SRAM buffer with the value one (synchronous).
VpeContext(VpeContext &&)=delete
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)
uint64_t bytesForDimension(dim_t dimension) const
Bytes occupied by dimension elements of this context's DataType.
void scalarDiv(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
Divide a scalar by each element: dst = scalar / src (synchronous)
bool tryAllocateBuffersWithSizes(uint32_t count, const uint64_t *byteSizes, uint64_t *outBuffers)
Atomically allocate count SRAM buffers of per-buffer byte sizes.
void copy(uint64_t src, uint64_t dst, dim_t dimension)
Copy a vector between two SRAM buffers (synchronous).
float scalarDot(uint64_t scalar, uint64_t src, dim_t dimension)
Scalar-weighted sum: result = Σ(scalar * src[i]) (synchronous)
void load(const void *srcDram, uint64_t dstBuffer, dim_t dimension)
Load vector data from DRAM to SRAM buffer (synchronous).
VpeContext & operator=(const VpeContext &)=delete
bool waitAllocateBuffers(uint32_t count, uint64_t byteSize, uint64_t *outBuffers)
Allocate count equally-sized buffers, waiting until they fit (all-or-nothing)
void sub(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
Element-wise subtraction: dst = src1 - src2 (synchronous)
VpeContext(const VpeContext &)=delete
void scalarMul(uint64_t scalar, uint64_t src, uint64_t dst, dim_t dimension)
Multiply every element by a scalar: dst = scalar * src (synchronous)
void div(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
Element-wise division: dst = src1 / src2 (synchronous)
float addReduce(uint64_t src1, uint64_t src2, dim_t dimension)
Sum of element-wise addition: result = Σ(src1[i] + src2[i]) (synchronous)
uint64_t allocateBuffer(uint64_t byteSize)
Allocate a buffer in SRAM.
VpeContext(VpeIdStrategy strategy, DataType dataType, volatile uint32_t *lockAddr)
Constructor with an explicit lock address.
float divReduce(uint64_t src1, uint64_t src2, dim_t dimension)
Sum of element-wise division: result = Σ(src1[i] / src2[i]) (synchronous)
void freeBuffer(uint64_t buffer)
Free a previously allocated buffer.
void fillZero(uint64_t dst, dim_t dimension)
Fill an SRAM buffer with the value zero (synchronous).
static void _reset()
[DANGEROUS] Reset the entire SRAM allocator state for the calling thread's execution unit
uint64_t waitAllocateBuffer(uint64_t byteSize)
Allocate a buffer, waiting until it fits.
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).
void bitwiseXor(uint64_t src1, uint64_t src2, uint64_t dst, dim_t dimension)
Element-wise bitwise XOR: dst = src1 ^ src2 (synchronous)
static constexpr uint64_t INVALID_BUFFER
Definition: vpe_context.hpp:92
constexpr uint64_t elementSize(DataType dt)
Byte size of one element of the given DataType.
Definition: vpe_types.hpp:34
VpeIdStrategy
VPE assignment strategy for load balancing.
Definition: vpe_types.hpp:77
uint32_t dim_t
Element count for a vector operation.
Definition: vpe_types.hpp:50
DataType
Element data type for VPE operations.
Definition: vpe_types.hpp:22
@ ByThreadId
default; spreads concurrent threads in one unit across engines
@ Fp32
32-bit IEEE 754 single-precision float (4 bytes per element)
Main header file for half-precision functionality.
Definition: half.hpp:7919
Pair of scalar dot-product results returned by dot2().
Definition: vpe_context.hpp:98