Special algorithms for strided matrix multiplication (matmul) optimize performance by addressing non-contiguous memory access patterns. Here are key approaches and techniques:
1. Blocked Algorithms and Loop Tiling
Concept:
Divide matrices into smaller blocks that fit into CPU/GPU cache, reducing cache misses caused by strided access.
How it works:
- Split matrices into tiles (blocks) and compute partial results for each tile.
- Reuse cached blocks across multiple computations to improve locality.
- Example: BLIS (BLAS-like Library) uses a 5-loop structure (micro-kernel, block, etc.) to optimize strided access.
// Simplified blocked matmul pseudo-code
for (i = 0; i < M; i += BI) {
for (j = 0; j < N; j += BJ) {
for (k = 0; k < K; k += BK) {
// Load block A[i..i+BI, k..k+BK] (strided)
// Load block B[k..k+BK, j..j+BJ] (strided)
// Multiply blocks and accumulate into C[i..i+BI, j..j+BJ]
}
}
}
Use Case:
BLIS employs SHEM (Strided Hierarchical Memory) to handle strides by:
- Cache-aware blocking: Tiles match cache size.
- Prefetching: Load strided data into cache before use.
- Register blocking: Use registers for small matrix blocks.
2. BLAS Leading Dimension Parameters
Concept:
BLAS functions (e.g., dgemm) accept lda, ldb, ldc parameters to specify strides.
How it works:
- The leading dimension defines the stride between matrix columns (row-major) or rows (column-major).
- Optimized BLAS implementations (OpenBLAS, Intel MKL) use these parameters to handle strides.
// BLAS example: Strided matmul
cblas_dgemm(
CblasRowMajor, // Storage order
CblasNoTrans, // A not transposed
CblasNoTrans, // B not transposed
M, N, K, // Dimensions
1.0, A, lda, // A with stride lda
B, ldb, // B with stride ldb
0.0, C, ldc // C with stride ldc
);
Optimizations:
- Loop reordering: Adjust loop order (i, j, k) to match stride direction.
- Vectorization: Use SIMD instructions if strides align with vector boundaries.
3. GPU-Specific Optimizations (cuBLAS)
Concept:
NVIDIA cuBLAS optimizes strided matmul on GPUs by:
- Kernel Specialization: Precompute thread mappings for common stride values.
- Shared Memory Tiling: Cache strided data in fast GPU shared memory.
// cuBLAS strided matmul
cublasSgemm(
handle, CUBLAS_OP_N, CUBLAS_OP_N,
m, n, k,
&alpha, d_A, lda, // Stride for A
d_B, ldb, // Stride for B
&beta, d_C, ldc // Stride for C
);
Use Case:
Effective for batched operations (e.g., processing multiple strided matrices in parallel).
4. Prefetching and Memory Access Reordering
Concept:
Hide memory latency by predicting and loading data before it’s needed.
How it works:
- Software pipelining: Overlap computation with data loading.
- Stride-aware prefetching: Load blocks of strided matrices into cache in advance.
Example:
In BLIS, prefetching is integrated into the micro-kernel to load the next tile while computing the current one.
5. Cache-Oblivious Algorithms
Concept:
Design algorithms that work efficiently across all cache levels without prior knowledge of cache size.
How it works:
- Recursive blocking: Split matrices recursively until blocks fit in cache.
- Avoids stride-specific tuning: Adapts to arbitrary strides.
Use Case:
Useful for irregular or unpredictable strides.
6. Vectorization and SIMD Considerations
Concept:
Optimize for SIMD (Single Instruction, Multiple Data) units by aligning strides with vector boundaries.
How it works:
- Aligned strides: Ensure stride is a multiple of the vector width (e.g., 4 for 128-bit SSE).
- Gather instructions: For non-aligned strides, use vector gather operations (if supported).
Limitation:
Strides that disrupt vector alignment may require scalar code or reduced vectorization efficiency.
7. Strided-to-Contiguous Conversion (Fallback)
Concept:
Copy strided matrices to contiguous buffers before multiplication (if strides are too large).
Trade-off:
- Pros: Simplifies computation.
- Cons: Adds overhead for copying; only useful for small matrices.
Key Takeaways
- BLIS/SHEM: Best for CPU-based strided matmul with cache-aware blocking.
- BLAS Parameters: Use
lda, ldb, ldc for basic stride support.
- cuBLAS: Optimized for GPU strided operations via kernel tuning.
- Prefetching/Vectorization: Enhances performance for predictable strides.
For implementation, refer to libraries like BLIS, OpenBLAS, or cuBLAS, which handle these optimizations internally.