Lazy Evaluation & Expression Templates
Overview
PyCauset implements Lazy Evaluation using C++ Expression Templates. This allows operations like C = A + B + C to be fused into a single evaluation pass, avoiding the creation of temporary matrices for intermediate results. This significantly reduces memory bandwidth usage and allocation overhead.
Architecture
The system is built on the Curiously Recurring Template Pattern (CRTP).
Core Components
-
MatrixExpression<Derived>: The base class for all lazy expressions. It provides the interface for:get_element(i, j): Computing the value at a specific index.rows(),cols(): Dimension queries.aliases(target): Checking if the expression refers to a specific matrix (for aliasing safety).touch_operands(): Updating the LRU status of source matrices (for memory safety).
-
Expression Nodes:
MatrixRefExpression: A lightweight wrapper around aconst MatrixBase&. It is the leaf node of the expression tree.ScalarExpression: Wraps a scalar value (double).BinaryExpression<L, R, Op>: Represents a binary operation. Stores operandsLandRby value (assuming they are lightweight expression objects) to prevent dangling references to temporary objects in chained expressions.UnaryExpression<E, Op>: Represents a unary operation. Stores operandEby value.
-
Functors:
- Located in
pycauset/matrix/expression/Functors.hpp. - Stateless structs with a static
applymethod (e.g.,ops::Add::apply(a, b)).
- Located in
Evaluation Model
Evaluation is triggered only when an expression is assigned to a MatrixBase object via operator=.
// MatrixBase.hpp
template <typename E>
MatrixBase& operator=(const MatrixExpression<E>& expr) {
// 1. Check for aliasing
if (expr.aliases(this)) {
throw std::runtime_error("Aliasing detected...");
}
// 2. Touch operands (Memory Safety)
expr.touch_operands();
// 3. Evaluate
for (uint64_t i = 0; i < rows(); ++i) {
for (uint64_t j = 0; j < cols(); ++j) {
set_element_as_double(i, j, expr.get_element(i, j));
}
}
return *this;
}
Memory Safety (The "Spill" Policy)
Lazy evaluation interacts with the MemoryGovernor to ensure that source matrices are not evicted from RAM while they are being read.
touch_operands(): Before the evaluation loop begins, the assignment operator callstouch_operands()on the expression tree. This recursively callstouch()on everyMatrixBasereferenced in the tree.- LRU Update:
touch()updates the timestamp of the matrix in theMemoryGovernor, moving it to the "most recently used" position. This protects it from being chosen for eviction if a spill is triggered during evaluation (e.g., if the destination matrix needs to allocate more storage).
Windows I/O Optimization
On Windows, the IOAccelerator uses VirtualUnlock to implement discard(). This serves as a hint to the OS to remove pages from the working set, similar to madvise(MADV_DONTNEED) on Linux. This is crucial for the "RAM-First" persistence policy, allowing us to effectively free RAM without closing the file handle.
Limitations & Future Work
- Aliasing: Currently, assignments like
A = A + Bthrow an exception to prevent unsafe aliasing. Future versions should implement temporary buffering or safe in-place evaluation for elementwise ops. - Vectorization: The current evaluation loop is scalar. Future versions should implement
fill_bufferin the expression interface to allow SIMD-optimized batch evaluation. - Opaque Operations: Matrix multiplication (
A * B) is currently eager. It returns a newMatrixBaseimmediately, rather than an expression.