cuda的第一个程序。
文件->新建->项目->NVIDIA->CUDA 5.5->CUDA 5.5 Runtime.名字设定为 my_cuda_1.0。确定后。
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int 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 = 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]);
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.cudaDeviceReset必须调用退出之前,为了分析和跟踪工具,如夜间和可视化探查显示完整的痕迹。
//cudaDeviceReset 为CUDA设备复位
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
//自己添加的。
getchar();
getchar();
return 0;
}
// Helper function for using CUDA to add vectors in parallel.助手功能使用CUDA并行向量。
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
int *dev_a = 0;
int *dev_b = 0;
int *dev_c = 0;
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.选择其中GPU上运行,改变这种多GPU系统上。
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Allocate GPU buffers for three vectors (two input, one output) .分配三个矢量的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;
}
// Copy input vectors from host memory to GPU buffers.复制输入向量从主机内存到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;
}
// Launch a kernel on the GPU with one thread for each element.启动一个内核在GPU上的每个元素的一个线程。
addKernel<<<1, size>>>(dev_c, dev_a, dev_b);
// Check for any errors launching the kernel检查启动内核的任何错误
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns cudaDeviceSynchronize等待内核来完成,并返回发射过程中遇到的任何错误。
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
// Copy output vector from GPU buffer to host memory.复制输出向量从GPU缓冲区到主机内存。
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
Error:
cudaFree(dev_c);
cudaFree(dev_a);
cudaFree(dev_b);
return cudaStatus;
}
这是我的第一cuda程序
它是来计算两个数组相加的运算,可以粗略的看出cuda进行并行运算的思路。
首先 定义啦一个
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
它有三个参数c是存放结果,a、b分别存放输入。size则是我们分的数组个数,是为了后面的addKernel()服务,让它分成几个部分来进行运算。
接下来就是定义啦addKernel()。它是运算的主要部分实现啦加运算。它有自己的分配规则,分成几个线程来并行预算数组数据。
主函数在定义变量后,
// Add vectors in parallel.平行上传载体。
cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addWithCuda failed!");
return 1;
}
将变量传递给addWithCuda()进行并行上传载体,返回值为cudaStatus 检查并行运算的状态。
状态正常将会进行输出结果。
然后将设备复位(gpu被称为设备)
主要说下addWithCuda()
它用指针建立啦3个数组来存储2个输入数据和一个接受结果数据。然后确定设备,分配gpu缓存,将数据出给gpu缓存等状态正常后,调用addKernel()内核函数进行运算启动一个内核在GPU上的每个元素的一个线程,等待内核有无错误,将结果有gpu缓存传递给内存。