blas for strided matrix
- User
blas for strided matrix
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
blas for strided matrix
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To use BLAS (Basic Linear Algebra Subprograms) with strided matrices (matrices stored non-contiguously in memory), you need to correctly specify the leading dimension (lda, ldb, ldc) and storage order (row/column-major). Here's how to approach it:
Leading Dimension:
The number of elements between the start of one column (column-major) or row (row-major) and the next. For strided matrices, this is not necessarily the same as the matrix dimensions.
Storage Order:
CblasRowMajor: Matrix rows are contiguous.CblasColMajor: Matrix columns are contiguous.BLAS Functions:
Use functions like cblas_dgemm (double-precision matrix multiply) and specify parameters for leading dimensions.
cblas_dgemmSuppose you have two matrices A (MxK) and B (KxN), stored in row-major order with strides stride_A and stride_B, respectively. Compute C = A * B (MxN).
#include <cblas.h>
// Matrix dimensions
int M = 2, K = 3, N = 2;
// Strides (elements between rows)
int stride_A = 4; // A is stored with padding
int stride_B = 5; // B is stored with padding
int stride_C = 3; // Output matrix C
// Data pointers (1D arrays)
double *A = (double*)malloc(10 * sizeof(double)); // Example: A has 2 rows, stride=4
double *B = (double*)malloc(15 * sizeof(double)); // Example: B has 3 rows, stride=5
double *C = (double*)calloc(6, sizeof(double)); // C has 2 rows, stride=3
// Compute C = A * B (row-major)
cblas_dgemm(
CblasRowMajor, // Storage order
CblasNoTrans, // No transpose for A
CblasNoTrans, // No transpose for B
M, // Rows of A
N, // Columns of B
K, // Columns of A / Rows of B
1.0, // alpha (scale factor)
A, // Matrix A
stride_A, // Leading dimension of A (stride)
B, // Matrix B
stride_B, // Leading dimension of B (stride)
0.0, // beta (scale factor for C)
C, // Output matrix C
stride_C // Leading dimension of C (stride)
);
CblasRowMajor indicates matrices are stored row-wise.stride_A = Number of elements between the start of one row of A and the next.stride_B = Same for B.stride_C = Stride for the result matrix C.CblasNoTrans assumes matrices are stored in row-major without transposition.If matrices are stored column-major with strides:
// A is MxK (column-major), stored with leading dimension lda=5
// B is KxN (column-major), stored with leading dimension ldb=4
cblas_dgemm(
CblasColMajor, // Storage order
CblasNoTrans, // No transpose
CblasNoTrans,
M, N, K,
1.0, A, 5, // lda=5 (stride between columns)
B, 4, // ldb=4
0.0, C, M // ldc=M (if C is MxN column-major)
);
cblas_dgemm, cblas_sgemm (single-precision).cblas_daxpy (strided vectors).cblas_dtrsm (supports strided matrices via lda).lda >= num_cols.lda >= num_rows.By correctly setting the leading dimensions and storage order, BLAS efficiently handles strided matrices.