GPU加速原理分析

本文基于英伟达NVIDIA公司CUDA的SDK

首先,CPU与GPU适合计算什么样的数据;

GPU的计算单元远比CPU多,这就决定了GPU适合大量简单,精度要求低的计算,CPU则适合复杂的,精度要求高的计算。

在编写GPU调用程序的时候,我们需要明白以下概念:

CPU为host,即主机,GPU为device,即设备,GPU与CPU中执行的函数是不一样的,所以在函数声明时,必须指出该函数是在host,还是在device上执行。

__global__    执行:device   调用:host
__device__    执行:device   调用:device
__global__    执行:host     调用:host

//eg:host调用的在device上执行的函数
//即 ,将host的数据传输到device中,在device中完成运算,将结果返回到host
//这是最常用 的GPU用法
__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}

下面将详细分析一段在GPU完成运算的代码,主要流程:

cuda程序: 
(1)host数据传到device上:cudaMemcpy 
(2)device处理:global 和device函数 
(3)device数据传回host上: cudaMemcpy,别忘了free cudamalloc的空间 

这段代码实现了[1,2,3,4,5]与[10,20,30,40,50]两个矩阵的并行相加,即同时对进行5次 c[i] = a[i] + b[i];而不是串行时的一个for循环。 

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>
//包含库
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);

//host调用的在device上执行的函数
__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;//线程标识符
    c[i] = a[i] + b[i];
}

int main()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };
    //声明函数
    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    //保证调用成功
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return 1;
    }

    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    //初始化设备
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }

    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
//参数:c输出地址。a,b两个输入,size 矩阵大小
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    //指定某一块GPU运算,只有一张显卡,设置为0
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)
    //分配GPU的缓存,用于存放计算内存
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));//输出的缓存
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));//输入a的缓存
    //开辟出空间类似malloc
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));//输入b的缓存
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    //将输入从CPU中导入GPU中 host->device  input:a
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    //将host的数据传到device变量上
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }
    //将输入从CPU中导入GPU中 host->device  input:b
    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    //在GPU上启动一个内核,每个元素都有一个线程。
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);
    //<<<1, size>>>一个内核,size个线程

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);
    //释放cudamalloc的内存
    return cudaStatus;
}

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值