windows7 cuda java_CUDA编程入门

CUDA是一个并行计算框架.用于计算加速.是nvidia家的产品.广泛地应用于现在的深度学习加速.

一句话描述就是:cuda帮助我们把运算从cpu放到gpu上做,gpu多线程同时处理运算,达到加速效果.

从一个简单例子说起:

#include

#include

// function to add the elements of two arrays

void add(int n, float *x, float *y)

{

for (int i = 0; i < n; i++)

y[i] = x[i] + y[i];

}

int main(void)

{

int N = 1<<20; // 1M elements

float *x = new float[N];

float *y = new float[N];

// initialize x and y arrays on the host

for (int i = 0; i < N; i++) {

x[i] = 1.0f;

y[i] = 2.0f;

}

// Run kernel on 1M elements on the CPU

add(N, x, y);

// Check for errors (all values should be 3.0f)

float maxError = 0.0f;

for (int i = 0; i < N; i++)

maxError = fmax(maxError, fabs(y[i]-3.0f));

std::cout << "Max error: " << maxError << std::endl;

// Free memory

delete [] x;

delete [] y;

return 0;

}

这段代码很简单,对两个数组对应位置元素相加.数组很大,有100万个元素.

62de37d4c14ad988d535280d89162d0c.png

代码运行时间在0.075s.

改写代码使之运行于gpu

gpu上能够运算的函数,在cuda中我们称之为kernel.由nvcc将其编译为可以在GPU上运行的格式.

#include

#include

// Kernel function to add the elements of two arrays

__global__

void add(int n, float *x, float *y)

{

for (int i = 0; i < n; i++)

y[i] = x[i] + y[i];

}

int main(void)

{

int N = 1<<20;

float *x, *y;

// Allocate Unified Memory – accessible from CPU or GPU

cudaMallocManaged(&x, N*sizeof(float));

cudaMallocManaged(&y, N*sizeof(float));

// initialize x and y arrays on the host

for (int i = 0; i < N; i++) {

x[i] = 1.0f;

y[i] = 2.0f;

}

// Run kernel on 1M elements on the GPU

add<<<1, 1>>>(N, x, y);

// Wait for GPU to finish before accessing on host

cudaDeviceSynchronize();

// Check for errors (all values should be 3.0f)

float maxError = 0.0f;

for (int i = 0; i < N; i++)

maxError = fmax(maxError, fabs(y[i]-3.0f));

std::cout << "Max error: " << maxError << std::endl;

// Free memory

cudaFree(x);

cudaFree(y);

return 0;

}

nvcc编译的文件的后缀为.cu

cuda中定义kernel在函数前加上__global声明就可以了.

在显存上分配内存使用cudaMallocManaged

调用一个函数使用<<< >>>符号.比如对add的函数的调用使用`add<<<1, 1>>>(N, x, y);`,关于其中参数的意义,后文再做解释.

需要cudaDeviceSynchronize()让cpu等待gpu上的计算做完再执行cpu上的操作

3ad26ed52cb3fae47cfecc0ea1e775d8.png

可以用nvprof做更详细的性能分析.

注意用sudo 否则可能报错.

sudo /usr/local/cuda/bin/nvprof ./add_cuda

03f03412accb9d49137a2304f27badcb.png

gpu上add用了194ms.

这里,我们注意到,跑在gpu反而比cpu更慢了.因为我们这段代码里`add<<<1, 1>>>(N, x, y);`并没有发挥gpu并行运算的优势,反而因为多了一些cpu与gpu的交互使得程序变慢了.

用GPU threads加速运算

重点来了

CUDA GPUS有多组Streaming Multiprocessor(SM).每个SM可以运行多个thread block. 每一个thread block有多个thread.

如下图所示:

3dad72ce6de4bac17cf24381defa9b0b.png

注意几个关键变量:

blockDim.x 表明了一个thread block内含有多少个thread

threadIdx.x 表明了当前thread在该thread blcok内的index

blockIdx.x 表明了当前是第几个thread block

我们要做的就是把计算分配到所有的thread上去.这些thread上并行地做运算,从而达到加速的目的.

前面我们说到在cuda内调用一个函数(称之为kernel)的用法为<<>>,比如`add<<<1, 1>>>(N, x, y);` 第一个参数的含义即为thread block的数量,第二个参数的含义为block内参与运算的thread数量.

现在来改写一下代码:

#include

#include

#include

// Kernel function to add the elements of two arrays

__global__

void add(int n, float *x, float *y)

{

int index = threadIdx.x;

int stride = blockDim.x;

printf("index=%d,stride=%d\n",index,stride);

for (int i = index; i < n; i+=stride)

{

y[i] = x[i] + y[i];

if(index == 0)

{

printf("i=%d,blockIdx.x=%d,thread.x=%d\n",i,blockIdx.x,threadIdx.x);

}

}

}

int main(void)

{

int N = 1<<20;

float *x, *y;

// Allocate Unified Memory – accessible from CPU or GPU

cudaMallocManaged(&x, N*sizeof(float));

cudaMallocManaged(&y, N*sizeof(float));

// initialize x and y arrays on the host

for (int i = 0; i < N; i++) {

x[i] = 1.0f;

y[i] = 2.0f;

}

// Run kernel on 1M elements on the GPU

add<<<1, 256>>>(N, x, y);

// Wait for GPU to finish before accessing on host

cudaDeviceSynchronize();

// Check for errors (all values should be 3.0f)

float maxError = 0.0f;

for (int i = 0; i < N; i++)

maxError = fmax(maxError, fabs(y[i]-3.0f));

std::cout << "Max error: " << maxError << std::endl;

// Free memory

cudaFree(x);

cudaFree(y);

return 0;

}

注意add的写法,我们把0,256,512...放到thread1计算,把1,257,...放到thread2计算,依次类推.调用的时候,add<<<1, 256>>>(N, x, y);表明我们只把计算分配到了thread block1内的256个thread去做.

编译这个程序(注意把代码里的printf注释掉,因为要统计程序运行时间):nvcc add_block.cu -o add_cuda_blcok -I/usr/local/cuda-9.0/include/ -L/usr/local/cuda-9.0/lib64

fae9fa63ea6cc98a0c14f8dc75cc8021.png

可以看到add的gpu时间仅仅用了2.87ms

e45e50861282b4ccea617f31a5f38807.png

程序的整体运行时间为0.13s,主要是cudaMallocManaged,cudaDeviceSynchronize之类的操作耗费了比较多的时间.

再一次改写代码

这一次我们用更多的thread block.

int blockSize = 256;

int numBlocks = (N + blockSize - 1) / blockSize;

add<<>>(N, x, y);

// Kernel function to add the elements of two arrays

__global__

void add(int n, float *x, float *y)

{

int index = blockIdx.x * blockDim.x + threadIdx.x;

int stride = blockDim.x * gridDim.x;

for (int i = index; i < n; i+=stride)

{

y[i] = x[i] + y[i];

//printf("i=%d,blockIdx.x=%d\n",i,blockIdx.x);

}

}

编译:nvcc add_grid.cu -o add_cuda_grid -I/usr/local/cuda-9.0/include/ -L/usr/local/cuda-9.0/lib64

统计性能:

b06eb60171e2bc3f07719d40b1874be2.png

可以看出来,gpu上add所用的时间进一步缩小到1.8ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值