(四)Hello CUDA!

来源:互联网 发布:零基础学c语言pdf下载 编辑:程序博客网 时间:2024/06/05 08:42

1.准备makefile

为了避免每次都要键入nvcc的命令,要准备一个makefile。makefile如下:

CUFLAG = -g  -Xcompiler -v \        -gencode=arch=compute_20,code=sm_20\        -gencode=arch=compute_20,code=compute_20\        -O2IFLAGS = -I$(CUDA_DIR)/include -I$(CUDA_SDK_DIR)/C/common/inc -I../includeLFLAGS = -L$(CUDA_DIR)/lib64 -L$(CUDA_SDK_DIR)/C/libPRG = cuda_test$(PRG) : main.cu nvcc main.cu -o $(PRG) $(CUFLAG) $(IFLAGS) $(LFLAGS)

Ubuntu 12.04 下 CUDA 编程 http://www.linuxidc.com/linux/2014-06/103056.htm

Ubuntu 12.04 安装 CUDA-5.5 http://www.linuxidc.com/Linux/2013-10/91101.htm

Ubuntu 11.10 上安装CUDA开发环境 http://www.linuxidc.com/Linux/2012-04/58913.htm

Fedora 15系统下配置CUDA环境 http://www.linuxidc.com/Linux/2011-12/49874.htm

Ubuntu 11.04 安装 NVIDIA CUDA 4.0 RC2 http://www.linuxidc.com/Linux/2011-10/46304.htm

Linux Mint 13/Ubuntu 12.04 配置CUDA 4.2 & OpenCV 2.4.2 方法 http://www.linuxidc.com/Linux/2013-10/91102.htm
2异构计算(Heterogeneous Computing)

以下为几个技术名词的简单介绍:

主机(host):CPU及其内存(host memory)。
设备(device):GPU及其内存(device memory)。
主机代码(host code):运行在CPU上的(一般来说「串行执行」的)代码。
设备代码(device code):运行在GPU上的并行执行的代码。
异构计算:由主机代码(host code)和设备代码(device code)协同执行完成的计算。
宏观上看,GPU执行代码的流程如下:

将输入数据通过PCI总线从CPU内存拷贝到GPU的DRAM中。
从内存中加载需要执行的代码到GPU后。
数据和指令都就绪后,就可以执行了。注意,在执行的过程中,GPU会在片上缓存数据以提升性能。
计算完毕后,将结果从GPU的DRAM中拷回CPU的Memory中。
例1: Hello World

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>__global__ void mykernel(void) {}int main(void) { mykernel<<<1,1>>>(); printf("Hello World!\n"); return 0;}

上述代码编译后运行生成可执行文件cuda_test,运行cuda_test后将输出:

Hello World!

注意:

调用kernel时需要三个尖括号
包含必要的头文件
CUDA C/C++中引入的新关键字global所修饰的函数有以下两方面含义:

此函数代码由设备执行
此函数由主机代码调用
nvcc将源代码分为设备函数和主机函数两大类:

设备函数由NVIDA编译器编译
主机函数由主机上配置的编译器编译
三个尖括号标志着一个从主机代码调用设备代码的函数,称为“启动内核”(kernel launch)

例2: 整数相加

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>__global__ void integer_add(int * a, int * b, int * c) { *c = *a + *b;}int main(void) { int a,b,c; int * d_a, * d_b, * d_c; int size = sizeof(int); cudaMalloc((void**)&d_a,size); cudaMalloc((void**)&d_b,size); cudaMalloc((void**)&d_c,size); printf("Enter two integers with a space to separate them:\n"); scanf("%d %d",&a,&b); cudaMemcpy(d_a,&a,size,cudaMemcpyHostToDevice); cudaMemcpy(d_b,&b,size,cudaMemcpyHostToDevice); integer_add<<<1,1>>>(d_a,d_b,d_c); cudaMemcpy(&c,d_c,size,cudaMemcpyDeviceToHost); cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); printf("Sum is %d\n",c); return 0;}

global修饰的integer_add函数说明:

integer_add函数将在设备上执行
integer_add函数将被主机调用
由于integer_add函数在设备上执行,所以指针a,b,c应该指向设备内存。这说明需要在设备内存中为变量开辟内存。

设备内存和主机内存在物理上是完全分开的不同电子部件:
设备指针指向GPU内存的某个位置。设备指针可以从主机端传给设备端或者从设备端传给主机端,但是设备指针不能在主机端解引用。
主机指针指向CPU内存的某个位置。主机指针可以从设备端传给主机端或者从主机端传给设备端,但是主机指针不能在设备端解引用。
CUDA API提供的用于处理设备内存的函数有cudaMalloc, cudaFree, cudaMemcpy。语义上分别对应于C语言的malloc, free, memcpy函数。这几个函数的具体使用方法如例2所示。

3 块(Blocks)

GPU是用来实现大规模并行的,如何实现呢?将上述例子扩展一下,如果我们要实现两个向量相加:
add<<<1,1>>>() —> add<<

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N 512__global__ void vec_block_add(int * a, int * b, int * c) { c[blockIdx.x] = a[blockIdx.x] + b[blockIdx.x];}void rand_ints(int * arr, int count) { srand(time(NULL)); for(int i=0;i<count;i++) {  arr[i] = rand() % 100; }}int main(void) { int * a,* b,* c; int * d_a, * d_b, * d_c; int size = N * sizeof(int); cudaMalloc((void**)&d_a,size); cudaMalloc((void**)&d_b,size); cudaMalloc((void**)&d_c,size); a = (int *) malloc(size); rand_ints(a,N); b = (int *) malloc(size); rand_ints(b,N); c = (int *) malloc(size); cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice); cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice); vec_block_add<<<N,1>>>(d_a,d_b,d_c); cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);#if 1 for(int i=0;i<N;i++) {  printf("%-5d: a:%-5d b:%-5d c:%-5d\n",i,a[i],b[i],c[i]); }#endif cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); free(a); free(b); free(c); return 0;}

例3中最关键的代码为如下几行:

global void vec_block_add(int * a, int * b, int * c) {
c[blockIdx.x] = a[blockIdx.x] + b[blockIdx.x];
}

由于函数是并行执行的,和传统的串行程序在integer_add函数中使用循环来完成加法相比,相当于由GPU这个加速器使用硬件的方式进行了循环展开,展开后便可以并行执行了。所以在编写这段代码时,需要使用blockIdx.x来定位当前执行的是循环的哪个部分。

从硬件的角度看,相当于同时有多个块在并行执行:
块0: c[0]=a[0]+b[0]
块1: c[1]=a[1]+b[1]
块2: c[2]=a[2]+b[2]
块3: c[3]=a[3]+b[3]
….
4 线程(Threads)

简单的说就是一个块(Block)可以分割成多个线程(Threads).
所以可以将上述的多块单线程版本改成单块多线程版本。
例4: 向量相加

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N 512__global__ void vec_thread_add(int * a, int * b, int * c) {c[threadIdx.x] = a[threadIdx.x] + b[threadIdx.x];}void rand_ints(int * arr, int count) {srand(time(NULL));for(int i=0;i<count;i++) {arr[i] = rand() % 100;}}int main(void) {int * a,* b,* c;int * d_a, * d_b, * d_c;int size = N * sizeof(int);cudaMalloc((void**)&d_a,size);cudaMalloc((void**)&d_b,size);cudaMalloc((void**)&d_c,size);a = (int *) malloc(size);rand_ints(a,N);b = (int *) malloc(size);rand_ints(b,N);c = (int *) malloc(size);cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice);cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice);vec_thread_add<<<1,N>>>(d_a,d_b,d_c);cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);#if 1for(int i=0;i<N;i++) {printf("%-5d: a:%-5d b:%-5d c:%-5d\n",i,a[i],b[i],c[i]);}#endifcudaFree(d_a);cudaFree(d_b);cudaFree(d_c);free(a);free(b);free(c);return 0;}

上述代码vec_thread_add<<<1,N>>>指定向量相加操作仅有1个块完成,但是这个块可以分割成512个线程来完成这个计算。和块类似的,由于函数vec_thread_add是被多个线程并行展开循环的,所以需要根据线程编号来确定当前循环应该完成的计算部分。每个线程使用threadIdx.x来标识当前线程。

5 索引(Indexing)

有了以上基础,接下来我们就可以实现多块多线程的大规模并行程序了。实现多块多线程并行程序的核心是利用进行准确的索引计算,即准确的利用索引将某个线程和该线程要完成计算的数据对应起来。
对于多块多线程的情况,我们假设要处理的向量包含128个元素,可以设计16个块,每个块中再包含8个线程来完成计算(因为16*8=128)。对于任何一个元素,我们都应该能准确的计算出来由哪个线程来完成这个相加计算。比如对于编号为71的元素:
块号为:71/8=8
线程号为:71%8=7
所以71号元素应该由:
71 = threadIdx.x + blockIdx.x * 8 = 7 + 8 * 8
在CUDA中可以使用blockDim.x来表示每个块中的线程数量,所以以上的计算可以修改为
元素编号 = threadIdx.x + blockIdx.x * blockDim.x
接下来我们就可以编写多块多线程的向量相加程序了.
例5: 向量相加

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N (16*16)#define THREAD_PER_BLOCK 32__global__ void vec_block_thread_add(int * a, int * b, int * c) {int index = threadIdx.x + blockIdx.x * blockDim.x;c[index] = a[index] + b[index];}void rand_ints(int * arr, int count) {srand(time(NULL));for(int i=0;i<count;i++) {arr[i] = rand() % 100;}}int main(void) {int * a,* b,* c;int * d_a, * d_b, * d_c;int size = N * sizeof(int);cudaMalloc((void**)&d_a,size);cudaMalloc((void**)&d_b,size);cudaMalloc((void**)&d_c,size);a = (int *) malloc(size);rand_ints(a,N);b = (int *) malloc(size);rand_ints(b,N);c = (int *) malloc(size);cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice);cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice);vec_block_thread_add<<<N/THREAD_PER_BLOCK,THREAD_PER_BLOCK>>>(d_a,d_b,d_c);cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);#if 1for(int i=0;i<N;i++) {printf("%-5d: a:%-5d b:%-5d c:%-5d\n",i,a[i],b[i],c[i]);}#endifcudaFree(d_a);cudaFree(d_b);cudaFree(d_c);free(a);free(b);free(c);return 0;}

以上代码的缺陷是需要元素的数量能够整除每块中线程数量,稍作修改便可以实现任意元素数量的向量相加了。

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N 100#define M 32__global__ void vec_block_thread_add(int * a, int * b, int * c, int n ) {int index = threadIdx.x + blockIdx.x * blockDim.x;if(index < n) {c[index] = a[index] + b[index];}}void rand_ints(int * arr, int count) {srand(time(NULL));for(int i=0;i<count;i++) {arr[i] = rand() % 100;}}int main(void) {int * a,* b,* c;int * d_a, * d_b, * d_c;int size = N * sizeof(int);cudaMalloc((void**)&d_a,size);cudaMalloc((void**)&d_b,size);cudaMalloc((void**)&d_c,size);a = (int *) malloc(size);rand_ints(a,N);b = (int *) malloc(size);rand_ints(b,N);c = (int *) malloc(size);cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice);cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice);vec_block_thread_add<<<(N+M-1)/M,M>>>(d_a,d_b,d_c,N);cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);#if 1for(int i=0;i<N;i++) {printf("%-5d: a:%-5d b:%-5d c:%-5d\n",i,a[i],b[i],c[i]);}#endifcudaFree(d_a);cudaFree(d_b);cudaFree(d_c);free(a);free(b);free(c);return 0;}

可能有一个很显然的问题就是既然使用块能够达到并行的函数调用为什么还需要用线程机制呢?

引入线程机制的必要性有:
线程间可以实现高效的通信
线程间可以实现高效的同步
6 共享内存(Shared Memory)

现在我们考虑实现另外一个例子,称之为“模板转换”。
具体的问题描述如下:提供两个数组,输入数组in和输出数组out. 一共有n个元素,将in中的值映射到out,映射的规则是:
对于任意的i,out[i] = out[ i - radius ] + out[ i - radius + 1] + out[i-radius+2] + … + out[i+radius].
这里我们会发现同一块内的线程比如,2号线程和3号线程访问的数据将会有大量的数据重复。如果能有cache机制,将有效的降低从GPU的DRAM中加载数据所消耗的时间。
共享内存(shared memory)就是用来实现这个功能的:
在同一个块内线程通过共享内存实现数据共享或者说在同一个块内,线程通过共享内存实现线程间的通信
硬件上看,共享内存是速度极高的片上内存。而设备内存,又称之为全局内存(global memory)的速度较慢
可以将共享内存看做是用户管理的cache
使用关键字shared定义的存储空间将在共享内存上为每个块开辟空间
一个块内的共享内存数据对另外一个块的线程来说是不可见的
现在我们考虑使用Shared Memory来实现这个例子,核心就是我们在一个块内合并线程对共享数据的访问。
对每一个块来说:
需要读入2 * radius + blockDim.x 个元素的数据。
输出blockDim.x个元素数据到全局内存中.

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N 256#define RADIUS 2#define BLOCK_SIZE 32__global__ void stencil_1d(int * in, int *out) {__shared__ int temp[BLOCK_SIZE + 2 * RADIUS];int g_index = threadIdx.x + blockIdx.x * blockDim.x + RADIUS;int s_index = threadIdx.x + RADIUS;temp[s_index] = in[g_index];if(threadIdx.x < RADIUS) {temp[s_index - RADIUS] = in[g_index - RADIUS];temp[s_index + BLOCK_SIZE] = in[g_index + BLOCK_SIZE];}int result = 0;for(int offset = -RADIUS; offset <= RADIUS; offset++) {result = result + temp[s_index+offset]; }out[g_index-RADIUS] = result;}void rand_ints(int * arr, int count) {srand(time(NULL));for(int i=0;i<count;i++) {arr[i] = rand() % 100;}}int main(void) {int * in, * out;int size_in = sizeof(int) * ( N + 2 * RADIUS );in = (int *) malloc(size_in);rand_ints(in+RADIUS,N);int size_out = sizeof(int) * N;out = (int *) malloc(size_out);int * d_in, * d_out;cudaMalloc((void**)&d_in,size_in);cudaMalloc((void**)&d_out,size_out);cudaMemcpy(d_in,in,size_in,cudaMemcpyHostToDevice);stencil_1d<<<(N+BLOCK_SIZE-1)/BLOCK_SIZE,BLOCK_SIZE>>>(d_in,d_out);cudaMemcpy(out,d_out,size_out,cudaMemcpyDeviceToHost);#if 1for(int i=0;i<N+2*RADIUS;i++) {printf("%-5d ",in[i]);}printf("\n");for(int i=0;i<N;i++) {printf("%-5d ",out[i]);}printf("\n");#endifcudaFree(d_in);cudaFree(d_out);free(in);free(out);return 0;}

7 线程同步函数(_syncthreads())

上述代码因为没有考虑数据竞争可能产生错误的结果。比如第2个块中的第三个线程进入for循环开始进行相加计算了,但是它需要读的数据还没有被4号线程写入到存储空间就会产生数据竞争问题。
CUDA提供了__syncthreads函数来进行数据同步,这样就可以保证所有的数据都就绪后再开始进行计算。
使用__syncthreads修改后的代码如下:

#include<stdio.h>#include<stdlib.h>#include<cuda.h>#include<cutil.h>#include<time.h>#define N 256#define RADIUS 2#define BLOCK_SIZE 32__global__ void stencil_1d(int * in, int *out) {__shared__ int temp[BLOCK_SIZE + 2 * RADIUS];int g_index = threadIdx.x + blockIdx.x * blockDim.x + RADIUS;int s_index = threadIdx.x + RADIUS;temp[s_index] = in[g_index];if(threadIdx.x < RADIUS) {temp[s_index - RADIUS] = in[g_index - RADIUS];temp[s_index + BLOCK_SIZE] = in[g_index + BLOCK_SIZE];}__syncthreads();int result = 0;for(int offset = -RADIUS; offset <= RADIUS; offset++) {result = result + temp[s_index+offset]; }out[g_index-RADIUS] = result;}void rand_ints(int * arr, int count) {srand(time(NULL));for(int i=0;i<count;i++) {arr[i] = rand() % 100;}}int main(void) {int * in, * out;int size_in = sizeof(int) * ( N + 2 * RADIUS );in = (int *) malloc(size_in);rand_ints(in+RADIUS,N);int size_out = sizeof(int) * N;out = (int *) malloc(size_out);int * d_in, * d_out;cudaMalloc((void**)&d_in,size_in);cudaMalloc((void**)&d_out,size_out);cudaMemcpy(d_in,in,size_in,cudaMemcpyHostToDevice);stencil_1d<<<(N+BLOCK_SIZE-1)/BLOCK_SIZE,BLOCK_SIZE>>>(d_in,d_out);cudaMemcpy(out,d_out,size_out,cudaMemcpyDeviceToHost);#if 1for(int i=0;i<N+2*RADIUS;i++) {printf("%-5d ",in[i]);}printf("\n");for(int i=0;i<N;i++) {printf("%-5d ",out[i]);}printf("\n");#endifcudaFree(d_in);cudaFree(d_out);free(in);free(out);return 0;}

8 异步操作(asynchronous operations)

CUDA kernel函数可以异步启动,普通的kernel函数需要等到执行完毕后才能将控制流返回到CPU端。异步启动方式指的是,kernel启动之后控制流立即返回到CPU端。
CUDA 的cudaMemcpy函数是等待数据拷贝完毕后才返回到CPU端。CUDA提供cudaMemcpyAsync来支持异步的数据拷贝。
CUDA提供的接口函数cudaDeviceSynchronize函数将阻塞CPU直到所有的CUDA函数调用都执行完毕。

9 故障处理(handle errors)

所有的CUDA函数都会返回一个错误码,其类型为cudaError_t。错误可能是当前函数调用产生的也可能是之前发起的异步函数执行后产生的。
得到最后一个错误的函数是:cudaError_t cudaGetLastError(void);
将错误码转换为字符串描述信息的函数是:char * cudaGetErrorString(cudaError_t);

10 设备管理(managing devices)

应用程序可以查询GPU的数量或者是选择GPU.
查询设备数量的接口: cudaGetDeviceCount(int * count);
选择设备的接口: cudaSetDevice(int device);
获取设备的接口:cudaGetDevice(int * device);
获取设备属性的接口: cudaGetDeviceProperties(cudaDeviceProp * prop, int device);
多个主机线程可以共享一个设备。
单个主机线程可以管理多个GPU设备。

  1. 引用

  2. CUDA C/C++ Basics Supercomputing 2011 Tutorial, http://www.nvidia.com/docs/IO/116711/sc11-cuda-c-basics.pdf
    本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104328.htm

转载自:http://www.linuxidc.com/Linux/2014-07/104328.htm

0 0
原创粉丝点击