GPU意为图形处理器,也一般被称之为显卡。
GPU性能参数
- 核心数:用于计算的计算单元
- GPU显存容量:GPU用于保存数据的地方
- GPU计算峰值:理论上可以达到最大的计算能力
- 显存带宽:传输速度的大小
GPU和CPU的结构差异
主要的结构差异如图:
CUDA中的hello world程序
#include <stdio.h>
#include"cuda_runtime.h"
#include"device_launch_parameters.h"
__global__ void hello_from_gpu()
{
printf("Hello World from the the GPU\n");
}
int main(void)
{
hello_from_gpu<<<1, 1>>>();
cudaDeviceSynchronize();
return 0;
}
核函数(kernel function)
- 核函数在GPU并行执行
- 使用限定词
__global__
修饰,函数的返回值必须为void
基本形式为:
__global__ void hello_from_gpu()
{
printf("Hello World from the the GPU\n");
}
注意事项
- 核函数只能有GPU访问
- 核函数不能使用变长参数
- 核函数不能使用静态变量
- 核函数不能使用函数指针
- 核函数有异步性