Special Algorithms for Strided Matrix Multiplications (MatMuls)
Strided matrix multiplications (MatMuls) arise in many applications, including batched operations, submatrix computations, and GPU kernels. Specialized algorithms are required to handle non-contiguous memory access efficiently. Below are key algorithms and optimizations for strided MatMuls:
1. Blocking (Tiling)
Concept: Divide matrices into smaller blocks that fit in cache to maximize data reuse and minimize memory access.
Strided Adaptation:
Advantages:
- Reduces cache misses by reusing loaded blocks.
- Works well with hierarchical memory (registers → L1 → L2 → RAM).
2. Register Blocking
Concept: Use CPU/GPU registers to hold small blocks of data for fast access.
Strided Adaptation:
Advantages:
- Eliminates memory access latency for small blocks.
- Critical for GPU performance (e.g., in cuBLAS).
3. Cache-Aware Scheduling
Concept: Reorder computations to maximize cache hits.
Strided Adaptation:
- Schedule computations to reuse loaded strided blocks.
- Example: For
C = A × B with strides, loop over blocks of B while reusing blocks of A.
Advantages:
- Minimizes redundant memory loads.
- Works well with out-of-order execution in modern CPUs.
4. Prefetching
Concept: Load data into caches before it is needed to hide memory latency.
Strided Adaptation:
Advantages:
- Reduces stalls due to memory latency.
- Especially useful for large strides.
5. Packing (Copying)
Concept: Copy strided data into contiguous buffers before computation.
Strided Adaptation:
Advantages:
- Simplifies memory access patterns.
- Enables use of standard MatMul algorithms on packed data.
Disadvantages:
- Requires extra memory and copy overhead.
6. Vectorization
Concept: Use SIMD (Single Instruction, Multiple Data) instructions to process multiple elements in parallel.
Strided Adaptation:
Advantages:
- Improves throughput by processing multiple elements per instruction.
- Works well with modern CPUs/GPUs.
7. Winograd’s Minimal Filtering
Concept: Reduce the number of multiplications in convolution-like operations.
Strided Adaptation:
- Apply Winograd’s algorithm to strided MatMuls by treating them as convolutions.
- Example: For a strided MatMul with stride
s, transform the operation into a convolution with stride s.
Advantages:
- Reduces computational complexity.
- Useful in deep learning (e.g., for strided convolutions).
8. Sparse Strided MatMuls
Concept: Exploit sparsity in strided matrices to skip unnecessary computations.
Strided Adaptation:
Advantages:
- Reduces memory and compute requirements for sparse strided matrices.
Example: Strided MatMul with Blocking in C
void strided_matmul_blocked(
float* A, int sA, float* B, int sB, float* C, int sC,
int m, int n, int k, int block_size
) {
for (int i = 0; i < m; i += block_size) {
for (int j = 0; j < n; j += block_size) {
for (int l = 0; l < k; l += block_size) {
// Load blocks of A, B, and C with strides
for (int ii = i; ii < min(i + block_size, m); ii++) {
for (int jj = j; jj < min(j + block_size, n); jj++) {
float sum = 0.0f;
for (int ll = l; ll < min(l + block_size, k); ll++) {
sum += A[ii * sA + ll] * B[ll * sB + jj];
}
C[ii * sC + jj] += sum;
}
}
}
}
}
}
Summary of Algorithms
| Algorithm | Key Idea | Best For | Limitations |
|---|
| Blocking | Divide into cache-friendly blocks | General-purpose | Requires tuning block sizes |
| Register Blocking | Use registers for small blocks | GPUs, small matrices | Limited by register size |
| Cache-Aware Scheduling | Reorder for cache reuse | Large matrices | Complex to implement |
| Prefetching | Load data ahead of time | Large strides | Hardware-dependent |
| Packing | Copy to contiguous buffers | Repeated operations | Memory overhead |
| Vectorization | Use SIMD instructions | Modern CPUs | Requires aligned access |
| Winograd | Reduce multiplications | Convolution-like ops | Numerical stability |
| Sparse | Skip zero elements | Sparse matrices | Overhead for non-sparse |
Libraries Implementing These Algorithms
- BLIS: Uses blocking, packing, and cache-aware scheduling.
- cuBLAS: Uses register blocking and vectorization for GPUs.
- Intel MKL: Uses vectorization and prefetching for CPUs.
- TensorFlow/PyTorch: Use Winograd and sparse algorithms for deep learning.
For further reading, refer to: