CUDA编程基础——并行矩阵乘法

CUDA编程首先呢是分配thread以及block

#include
#include
#include   //cuda运行时间接口
#define Thread_Num 256     //每一block包含的线程数
#define Matrix_Size 10
const int block_num=(Matrix_Size+Thread_Num-1)/Thread_Num;

然后是两个基本的函数:
//打印设备信息

void printDeviceProp(const cudaDeviceProp &prop)
{
    printf("Device Name : %s.\n", prop.name);
    printf("totalGlobalMem : %d.\n", prop.totalGlobalMem);
    printf("sharedMemPerBlock : %d.\n", prop.sharedMemPerBlock);
    printf("regsPerBlock : %d.\n", prop.regsPerBlock);
    printf("warpSize : %d.\n", prop.warpSize);
    printf("memPitch : %d.\n", prop.memPitch);
    printf("maxThreadsPerBlock : %d.\n", prop.maxThreadsPerBlock);
    printf("maxThreadsDim[0 - 2] : %d %d %d.\n", prop.maxThreadsDim[0],    prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
    printf("maxGridSize[0 - 2] : %d %d %d.\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
    printf("totalConstMem : %d.\n", prop.totalConstMem);
    printf("major.minor : %d.%d.\n", prop.major, prop.minor);
    printf("clockRate : %d.\n", prop.clockRate);
    printf("textureAlignment : %d.\n", prop.textureAlignment);
    printf("deviceOverlap : %d.\n", prop.deviceOverlap);
    printf("multiProcessorCount : %d.\n", prop.multiProcessorCount);
}

//初始化cuda

bool InitCUDA()
{
    int count;
    cudaGetDeviceCount(&count);
    if(count==0){
        fprintf(stderr,"three is no device.\n");
        return false;
    }
    int i;
    for(i=0;i=1){break;}
        }
    }
    if (i == count) {
        fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
        return false;
    }
    cudaSetDevice(i);
    return true;
}

//接着随机生成两个矩阵

void matGen(float* a, int n)
{
    int i,j;
    for(i=0;i

//并行矩阵乘法函数,最主要的一部分

__global__ static void matMultCuda(const float* a,const float* b,float* c,int n,clock_t* time)
{
    const int tid=threadIdx.x;
    const int bid=blockIdx.x;

    //从 bid 和 tid 计算出这个 thread 应该计算的 row 和 column
    const int idx = bid * Thread_Num + tid;
    const int row = idx / n;
    const int column = idx % n;

    int i;
    //clock_t start;
    
    //每个block开始时记录
    if(tid==0) time[bid]=clock();

    //计算矩阵乘法
    if(row < n && column < n)
    {
        float t=0;
        for(i=0;i

//运算完后打印出矩阵

void printMatrix(const float *A, const int n) {
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            printf("%.2f" ,A[i*n+j]);
            printf(" ");
        }
        printf("\n");
    }
    printf("\n");
}

//最后我们来看一下主函数

int main()
{
    if(!InitCUDA()) return 0;
    float *a,*b,*c;
    int n=Matrix_Size;

    //分配内存
    a=(float*)malloc(sizeof(float)*n*n);
    b=(float*)malloc(sizeof(float)* n*n);
    c=(float*)malloc(sizeof(float)* n*n);

    //设置随机种子
    srand(0);

    //随机生成两个矩阵
    matGen(a,n);
    matGen(b,n);

    float *cuda_a,*cuda_b,*cuda_c;
    clock_t* time;

    //cudaMalloc 获取一块显卡内存
    cudaMalloc((void**)&cuda_a, sizeof(float)* n*n);
    cudaMalloc((void**)&cuda_b, sizeof(float)* n*n);
    cudaMalloc((void**)&cuda_c, sizeof(float)* n*n);
    cudaMalloc((void**)&time, sizeof(clock_t)* block_num*2);

    //cudaMemcpy 将产生的矩阵复制到显卡内存中
    //cudaMemcpyHostToDevice - 从内存复制到显卡内存
    //cudaMemcpyDeviceToHost - 从显卡内存复制到内存
    cudaMemcpy(a,cuda_a,sizeof(float)* n*n,cudaMemcpyHostToDevice);
    cudaMemcpy(b,cuda_b,sizeof(float)* n*n,cudaMemcpyHostToDevice);

    //printMatrix(cuda_a, n);
    //printMatrix(cuda_b, n);

    // 在CUDA 中执行函数 语法:函数名称<<>>(参数...);
    matMultCuda<<>>(cuda_a,cuda_b,cuda_c,n,time);

    //把结果复制回内存中
    clock_t time_use[block_num*2];

    cudaMemcpy(c,cuda_c,sizeof(float)* n*n,cudaMemcpyDeviceToHost);
    cudaMemcpy(&time_use, time, sizeof(clock_t)* block_num * 2, cudaMemcpyDeviceToHost);

    printMatrix(a, n);
    printMatrix(b, n);
    printMatrix(c, n);

    //释放资源
    cudaFree(cuda_a);
    cudaFree(cuda_b);
    cudaFree(cuda_c);
    cudaFree(time);

    //把每个 block 最早的开始时间,和最晚的结束时间相减,取得总运行时间
    clock_t min_start, max_end;
    min_start = time_use[0];
    max_end = time_use[block_num];
    for (int i = 1; i < block_num; i++) 
    {
        if (min_start > time_use[i]) min_start = time_use[i];
        if (max_end < time_use[i + block_num]) max_end = time_use[i + block_num];
    }

    //核函数运行时间
    clock_t final_time = max_end - min_start;
    printf("gputime: %d\n", final_time);
    return 0;
}

你可能感兴趣的:(CUDA编程基础——并行矩阵乘法)