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.
To perform matrix operations on the GPU using NVIDIA's cuBLAS library, you can follow this example for matrix multiplication (GEMM). cuBLAS is the GPU-accelerated version of BLAS, optimized for NVIDIA GPUs, and it supports strided matrices via the lda, ldb, and ldc parameters.
cublas_v2.h and cuda_runtime.h).nvcc (NVIDIA CUDA Compiler) to compile the code.This example computes:
C = α * A * B + β * C
where A is m x k, B is k x n, and C is m x n.
#include <stdio.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
int main() {
// Step 1: Define matrix dimensions
const int m = 2; // Rows of A and C
const int n = 3; // Columns of B and C
const int k = 4; // Columns of A and rows of B
// Step 2: Initialize host data
float A[m * k] = {
1, 2, 3, 4,
5, 6, 7, 8
};
float B[k * n] = {
9, 10, 11,
12, 13, 14,
15, 16, 17,
18, 19, 20
};
float C[m * n] = {0};
// Step 3: Allocate device memory
float *d_A, *d_B, *d_C;
cudaMalloc(&d_A, m * k * sizeof(float));
cudaMalloc(&d_B, k * n * sizeof(float));
cudaMalloc(&d_C, m * n * sizeof(float));
// Step 4: Copy data to device
cudaMemcpy(d_A, A, m * k * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_B, B, k * n * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_C, C, m * n * sizeof(float), cudaMemcpyHostToDevice);
// Step 5: Initialize cuBLAS handle
cublasHandle_t handle;
cublasCreate(&handle);
// Step 6: Set alpha and beta
float alpha = 1.0f;
float beta = 0.0f;
// Step 7: Perform matrix multiplication (C = A * B)
cublasSgemm(
handle,
CUBLAS_OP_N, // No transpose for A
CUBLAS_OP_N, // No transpose for B
m, n, k, // Dimensions: m x k * k x n = m x n
&alpha, // Scalar multiplier for A * B
d_A, m, // A (device pointer), leading dimension = m (row-major)
d_B, k, // B (device pointer), leading dimension = k (row-major)
&beta, // Scalar multiplier for C
d_C, m // C (device pointer), leading dimension = m (row-major)
);
// Step 8: Copy result back to host
cudaMemcpy(C, d_C, m * n * sizeof(float), cudaMemcpyDeviceToHost);
// Step 9: Print result
printf("Result C:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%f ", C[i * n + j]);
}
printf("\n");
}
// Step 10: Cleanup
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C);
cublasDestroy(handle);
return 0;
}
cuBLAS Functions:
cublasSgemm (single-precision) or cublasDgemm (double-precision) for matrix multiplication.cublasSetMatrix/cublasGetMatrix for copying data between host and device (alternative to cudaMemcpy).Strided Matrices:
lda, ldb, ldc) is the number of columns.lda to the stride value.// Example: Strided matrix with row stride of 5
cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, n, k, &alpha, d_A, 5, d_B, 5, &beta, d_C, 5);
Memory Layout:
CUBLAS_OP_T for transposed matrices.Error Handling:
cudaMalloc, cudaMemcpy, and cublas* functions.if (cudaGetLastError() != cudaSuccess) { /* handle error */ }
To compile with nvcc:
nvcc -o cublas_example cublas_example.c -lcublas
lda, ldb, and ldc. For example, if your matrix is stored with a stride of 5 (e.g., A[0], A[5], A[10], ...), set lda = 5.cublasSaxpy), triangular solves (cublasStrsm), and more.Let me know if you need an example for strided matrices or complex numbers!