cublas example
- User
cublas example
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
cublas example
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a cuBLAS example for matrix multiplication (dgemm) using strided matrices with non-contiguous memory layouts. This example demonstrates handling row-major data with custom strides in NVIDIA's cuBLAS:
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#define CHECK_CUDA(call) { \
cudaError_t err = call; \
if (err != cudaSuccess) { \
printf("CUDA Error at %s:%d - %s\n", __FILE__, __LINE__, cudaGetErrorString(err)); \
exit(1); \
} \
}
#define CHECK_CUBLAS(call) { \
cublasStatus_t status = call; \
if (status != CUBLAS_STATUS_SUCCESS) { \
printf("cuBLAS Error at %s:%d - %d\n", __FILE__, __LINE__, status); \
exit(1); \
} \
}
int main() {
// Matrix dimensions
const int M = 2; // Rows of A and C
const int N = 2; // Columns of B and C
const int K = 3; // Columns of A / Rows of B
// Strides (elements between rows)
const int strideA = 4; // A has 2 rows, stride=4
const int strideB = 5; // B has 3 rows, stride=5
const int strideC = 3; // C has 2 rows, stride=3
// Host data with padding (row-major)
double h_A[] = {
1, 3, 5, 0, // Row 0
2, 4, 6, 0, // Row 1
0, 0, 0, 0 // Padding (unused)
};
double h_B[] = {
7, 10, 0, 0, 0, // Row 0
8, 11, 0, 0, 0, // Row 1
9, 12, 0, 0, 0, // Row 2
0, 0, 0, 0, 0 // Padding
};
double h_C[strideC * N] = {0}; // Output matrix
// Device pointers
double *d_A, *d_B, *d_C;
CHECK_CUDA(cudaMalloc(&d_A, strideA * M * sizeof(double)));
CHECK_CUDA(cudaMalloc(&d_B, strideB * K * sizeof(double)));
CHECK_CUDA(cudaMalloc(&d_C, strideC * M * sizeof(double)));
// Copy data to device
CHECK_CUDA(cudaMemcpy(d_A, h_A, strideA * M * sizeof(double), cudaMemcpyHostToDevice));
CHECK_CUDA(cudaMemcpy(d_B, h_B, strideB * K * sizeof(double), cudaMemcpyHostToDevice));
CHECK_CUDA(cudaMemset(d_C, 0, strideC * M * sizeof(double)));
// Create cuBLAS handle
cublasHandle_t handle;
CHECK_CUBLAS(cublasCreate(&handle));
// cuBLAS parameters
const double alpha = 1.0;
const double beta = 0.0;
// Perform C = alpha * A * B + beta * C
// Note: cuBLAS uses column-major by default, so we use row-major trick:
// Compute C^T = B^T * A^T
CHECK_CUBLAS(cublasDgemm(
handle,
CUBLAS_OP_N, // B is not transposed (but appears transposed in row-major)
CUBLAS_OP_N, // A is not transposed
N, // Columns of B^T (rows of B)
M, // Rows of A^T (columns of A)
K, // Common dimension (columns of A^T/rows of B^T)
&alpha,
d_B, // B^T in row-major = original B in column-major
strideB, // Leading dimension of B (stride between rows)
d_A, // A^T in row-major = original A in column-major
strideA, // Leading dimension of A
&beta,
d_C, // C^T in row-major
strideC // Leading dimension of C
));
// Copy result back to host
CHECK_CUDA(cudaMemcpy(h_C, d_C, strideC * M * sizeof(double), cudaMemcpyDeviceToHost));
// Print result (row-major)
printf("C = \n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%6.1f ", h_C[i * strideC + j]);
}
printf("\n");
}
// Cleanup
CHECK_CUBLAS(cublasDestroy(handle));
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C);
return 0;
}
Key points explained:
Matrix Layout:
h_A (2×3): Stride 4 (padded row length)[1, 3, 5, 0]
[2, 4, 6, 0]
h_B (3×2): Stride 5 (padded row length)[7, 10, 0, 0, 0]
[8, 11, 0, 0, 0]
[9, 12, 0, 0, 0]
cuBLAS Row-Major Trick:
To Compile & Run:
nvcc cublas_dgemm_strided.cu -lcublas -o dgemm
./dgemm
Common Variations:
Column-Major Input:
cublasDgemm(
handle,
CUBLAS_OP_N, CUBLAS_OP_N,
M, N, K, // Original dimensions
&alpha,
d_A, strideA, // Actual column stride
d_B, strideB,
&beta,
d_C, strideC
);
Transposed Operations:
CUBLAS_OP_T/CUBLAS_OP_N for explicit transpositionThis example demonstrates how to handle arbitrary matrix strides in cuBLAS while maintaining high performance. The stride values can be larger than the matrix dimensions to accommodate padding or submatrix views.
transa = CUBLAS_OP_N (B not transposed)transb = CUBLAS_OP_N (A not transposed)n = N, m = M, k = KLeading Dimensions:
lda = strideB (5) for Bldb = strideA (4) for Aldc = strideC (3) for CExpected Output:
C =
76.0 103.0
100.0 136.0
Memory Handling: