01GPU简介

11 阅读1分钟

GPU意为图形处理器,也一般被称之为显卡。

GPU性能参数

  • 核心数:用于计算的计算单元
  • GPU显存容量:GPU用于保存数据的地方
  • GPU计算峰值:理论上可以达到最大的计算能力
  • 显存带宽:传输速度的大小

GPU和CPU的结构差异

主要的结构差异如图:

image.png

- GPU不能单独计算,需要和CPU一起使用,组成异构计算结构 - CPU主要为控制作用,一般称之为主机(host) - GPU为主要计算结构,称之为设备(device) - 使用pcle总线连接 ## CUDA运行使用的api结构

1719396499658.png

- 驱动的api和运行的时候的api基本没有差异,一般选择使用运行时候的api

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访问
  • 核函数不能使用变长参数
  • 核函数不能使用静态变量
  • 核函数不能使用函数指针
  • 核函数有异步性