//cublas计算矩阵乘法(sgemm)
#include<cuda_runtime.h>
#include<device_launch_parameters.h>
#include<cublas_v2.h>
#include<curand.h>
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<time.h>
#include<math.h>
void verify_solution(float *a, float*b, float*c, int n) {
float temp;
float epsilon = 0.001;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp = 0;
for (int k = 0; k < n; k++) {
temp += a[k*n + i] * b[j*n + k];
}
assert(fabs(c[j*n + i] - temp) < epsilon);
}
}
}
int main() {
int n = 1 << 10;
size_t bytes = n * n * sizeof(float);
float*h_a, *h_b, *h_c;
float*d_a, *d_b, *d_c;
h_a = (float*)malloc(bytes);
h_b = (float*)malloc(bytes);
h_c = (float*)malloc(bytes);
cudaMalloc(&d_a, bytes);
cudaMalloc(&d_b, bytes);
cudaMalloc(&d_c, bytes);
curandGenerator_t prng;
curandCreateGenerator(&prng, CURAND_RNG_PSEUDO_DEFAULT);//随机数生成器
curandSetPseudoRandomGeneratorSeed(prng, (unsigned long long)clock());
//设备上使用随机数填充矩阵
curandGenerateUniform(prng, d_a, n*n);
curandGenerateUniform(prng, d_b, n*n);
cublasHandle_t handle;
cublasCreate(&handle);
//标量
float alpha = 1.0f;
float beta = 0.0f;
//相当于核函数
cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, n, n, n, &alpha, d_a, n, d_b, n, &beta, d_c, n);
cudaMemcpy(h_a, d_a, bytes, cudaMemcpyDeviceToHost);
cudaMemcpy(h_b, d_b, bytes, cudaMemcpyDeviceToHost);
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);
//验证解决方案
verify_solution(h_a, h_b, h_c, n);
printf("conpleted successfully\n");
return 0;
}
//此程序使用了curand函数 需要在库里添加curand.lib