CUDA从入门到精通(五):线程并行

来源:互联网 发布:proc sql定义变量长度 编辑:程序博客网 时间:2024/06/06 15:00

多线程我们应该都不陌生,在操作系统中,进程是资源分配的基本单元,而线程是CPU时间调度的基本单元(这里假设只有1个CPU)。

将线程的概念引申到CUDA程序设计中,我们可以认为线程就是执行CUDA程序的最小单元,前面我们建立的工程代码中,有个核函数概念不知各位童鞋还记得没有,在GPU上每个线程都会运行一次该核函数。

但GPU上的线程调度方式与CPU有很大不同。CPU上会有优先级分配,从高到低,同样优先级的可以采用时间片轮转法实现线程调度。GPU上线程没有优先级概念,所有线程机会均等,线程状态只有等待资源和执行两种状态,如果资源未就绪,那么就等待;一旦就绪,立即执行。当GPU资源很充裕时,所有线程都是并发执行的,这样加速效果很接近理论加速比;而GPU资源少于总线程个数时,有一部分线程就会等待前面执行的线程释放资源,从而变为串行化执行。

 

代码还是用上一节的吧,改动很少,再贴一遍:

#include "cuda_runtime.h"//CUDA运行时API#include "device_launch_parameters.h"#include <stdio.h>cudaError_t addWithCuda(int *c, const int *a, const int *b, size_t size);__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;int num = 0;cudaDeviceProp prop;cudaStatus = cudaGetDeviceCount(&num);for(int i = 0;i<num;i++){cudaGetDeviceProperties(&prop,i);}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]);    // cudaThreadExit must be called before exiting in order for profiling and    // tracing tools such as Nsight and Visual Profiler to show complete traces.    cudaStatus = cudaThreadExit();    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaThreadExit failed!");        return 1;    }    return 0;}// 重点理解这个函数cudaError_t addWithCuda(int *c, const int *a, const int *b, size_t size){    int *dev_a = 0;//GPU设备端数据指针    int *dev_b = 0;    int *dev_c = 0;    cudaError_t cudaStatus;//状态指示    // Choose which GPU to run on, change this on a multi-GPU system.    cudaStatus = cudaSetDevice(0);//选择运行平台    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");        goto Error;    }    // 分配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));    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaMalloc failed!");        goto Error;    }    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaMalloc failed!");        goto Error;    }    // 拷贝数据到GPU    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaMemcpy failed!");        goto Error;    }    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaMemcpy failed!");        goto Error;    }    // 运行核函数    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);    // cudaThreadSynchronize waits for the kernel to finish, and returns    // any errors encountered during the launch.    cudaStatus = cudaThreadSynchronize();//同步线程    if (cudaStatus != cudaSuccess) {        fprintf(stderr, "cudaThreadSynchronize 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);//释放GPU设备端内存    cudaFree(dev_a);    cudaFree(dev_b);        return cudaStatus;}

红色部分即启动核函数的调用过程,这里看到调用方式和C不太一样。<<<>>>表示运行时配置符号,里面1表示只分配一个线程组(又称线程块、Block),size表示每个线程组有size个线程(Thread)。本程序中size根据前面传递参数个数应该为5,所以运行的时候,核函数在5个GPU线程单元上分别运行了一次,总共运行了5次。这5个线程是如何知道自己“身份”的?是靠threadIdx这个内置变量,它是个dim3类型变量,接受<<<>>>中第二个参数,它包含x,y,z 3维坐标,而我们传入的参数只有一维,所以只有x值是有效的。通过核函数中int i = threadIdx.x;这一句,每个线程可以获得自身的id号,从而找到自己的任务去执行。

 

下节我们介绍块并行。

原创粉丝点击