环境配置:
CUDA从官网下载,但是国内时常会下到只有几KB的安装包其实是抽风了,network也时常出错,感觉最稳妥的还是看运气下local的
下好之后在cmd下输入nvcc --version就可以看到是否安装成功了,也要保证CUDA_PATH的路径是对的
接下来是在VS中新建工程,为了通用性考虑建立一个空的项目
然后是项目属性:
Include Directories:$(CUDA_PATH)\include
Libarary Directories:$(CUDA_PATH)\lib\Win32 (Win32 或是 x64)
Linker:Additional Dependencies:
cuda.lib
cudadevrt.lib
cudart.lib
cudart_static.lib
nvcuvid.lib //这项有可能导致编译错误?
OpenCL.lib
右键项目:
添加.cu文件一定要从这里添加,将cpp改后缀可能会出现一些奇怪的问题
右键.cu 属性:
接下来是代码了。
首先要注意的一点是.cu和.cpp是用的不同的编译器,也就是说CUDA的__global__函数和出现的<<<1,size>>>等c++编译器识别不了的关键字必须放在.cu文件里。
而且c++文件包括".h",".cpp"都不能直接引用.cu文件!,编译无法通过,可能会报一个无法识别"<"的错误。
解决方法是在.cu的helper function也就是入口函数前加 extern "C"
并在cpp里extern "C" 这个函数 注意不能include .cu文件
完整文件结构可以如下:
CUDAHelloWorld.cu:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
__global__ void addKernel(int* c, const int* a, const int* b)
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}
// Helper function for using CUDA to add vectors in parallel.
extern "C"
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.
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) .
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.
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.
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
// 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.
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;
}
CUDAHelloWorldEntry.h:
#pragma once
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
class CUDAWrapper
{
public:
int CUDAHelloWorld();
};
CUDAHelloWorldEntry.cpp:
#include "CUDAHelloWorldEntry.h"
extern "C"
cudaError_t addWithCuda(int* c, const int* a, const int* b, unsigned int size);
int CUDAWrapper::CUDAHelloWorld()
{
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.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
system("pause");
return 0;
}
最后只要在需要的地方例如main函数调用
CUDAWrapper cw;
cw.CUDAHelloWorld();
就可以运行第一个CUDA程序了。