我的显卡
参考官方页面http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-microsoft-windows/index.html
下载cuda5:https://developer.nvidia.com/cuda-downloads
The CUDA 5 Installers include the CUDA Toolkit, SDK code samples, and developer drivers.
从这里我们知道只要下载一个包安装就行了
然后更新系统环境变量
CUDA_PATH_V5_0=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\
CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\
CUDA_BIN_PATH=%CUDA_PATH%bin
CUDA_INC_PATH=%CUDA_PATH%include
CUDA_LIB_PATH=%CUDA_PATH%lib\Win32
CUDA_SDK_PARH=C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.0\
CUDA_SDK_LIB_PARH=%CUDA_SDK_PARH%common\lib\Win32
CUDA_SDK_BIN_PARH=%CUDA_SDK_PARH%bin\win32
在Path里,加这些
;%CUDA_PATH%;%CUDA_SDK_PARH%;%CUDA_LIB_PATH%;%CUDA_BIN_PATH%;%CUDA_SDK_LIB_PARH%;%CUDA_SDK_BIN_PARH%
复制
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\extras\visual_studio_integration\MSBuildExtensions
文件夹下的文件到
C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\MSBuildExtensions
让CUDA5和VS2012兼容,还需要做如下修改
打开CUDA 5.0.props
在
<CudaClVersion Condition="'$(PlatformToolset)' == 'v90'">2008</CudaClVersion>
<CudaClVersion Condition="'$(PlatformToolset)' == 'v100'">2010</CudaClVersion>
下面添加
<CudaClVersion Condition="'$(PlatformToolset)' == 'v110'">2010</CudaClVersion>
打开 CUDA 5.0.targets
找到“CudaCleanDependsOn”
将标签里的东西修改成
<CudaCleanDependsOn>
$(CudaCompileDependsOn);
_SelectedFiles;
CudaFilterSelectedFiles;
AddCudaCompileMetadata;
AddCudaLinkMetadata;
AddCudaCompileDeps;
AddCudaCompilePropsDeps;
ValidateCudaBuild;
ValidateCudaCodeGeneration;
ComputeCudaCompileOutput;
PrepareForCudaBuild
</CudaCleanDependsOn>
同样在 CUDA 5.0.targets里搜索“GenerateRelocatableDeviceCode”,修改成
GenerateRelocatableDeviceCode="%(CudaCompile.GenerateRelocatableDeviceCode)"
在该文件下面一点的地方查找 “CodeGeneration”,修改为
CodeGeneration="%(CudaCompile.CodeGenerationValues)"
CommandLineTemplate="(CudaToolkitNvccPath) %(CudaCompile.BuildCommandLineTemplate) %(CudaCompile.ApiCommandLineTemplate) %(CudaCompile.CleanCommandLineTemplate)" />
编辑
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include\host_config.h
修改
#if _MSC_VER < 1400 || _MSC_VER > 1600
为
#if _MSC_VER < 1400 || _MSC_VER > 1700
重启VS
设置CUDA关键词高亮
复制
C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.0\doc\syntax_highlighting\visual_studio_8\usertype.dat
到
C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE
目录下
安装VAssistx
下载http://www.wholetomato.com/downloads/default.asp
打开vs2012,依次打开VAssistXX|Visual assist X Options|Projects|C/C++Directories
右侧Platform选择Custom
Show Directorios for选择Other include files
添加
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include
C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.0\common\inc
如下图
使Visual Assist X支持.cu文件
打开注册表(win+R,输入 regedit)
找到HKEY_CURRENT_USER\Software\Whole Tomato\Visual Assist X\VANet11
在右侧找到ExtSource,右击修改,添加
.cu;.cuh;
然后重新打开vs
新建一个VC++的Win32控制台项目
附加选项选择“空项目”
-
Navigate to the “Configuration Properties\Linker\General” option - In the “Additional Library Directories” field, add “$(CUDA_PATH)lib\$(PlatformName)”
- Go to the “Configuration Properties\Linker\Input” option
- Lastly in the “Additional Dependencies” field, add “cudart.lib”
在属性页/链接/常规里找到附加库目录,添加“%CUDA_LIB_PATH%”
在属性页/链接/输入里找到附加依赖项,添加cudart.lib
最后使用官方给的一个程序测试一下
#include <iostream> using namespace std; __global__ void saxpy(int n, float a, float *x, float *y) { int i = blockIdx.x*blockDim.x + threadIdx.x; if (i < n) y[i] = a*x[i] + y[i]; } int main(void) { int N = 1<<20; float *x, *y, *d_x, *d_y; x = (float*)malloc(N*sizeof(float)); y = (float*)malloc(N*sizeof(float)); cudaMalloc(&d_x, N*sizeof(float)); cudaMalloc(&d_y, N*sizeof(float)); for (int i = 0; i < N; i++) { x[i] = 1.0f; y[i] = 2.0f; } cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice); // Perform SAXPY on 1M elements saxpy<<<(N+255)/256, 256>>>(N, 2.0, d_x, d_y); cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost); float maxError = 0.0f; for (int i = 0; i < N; i++) maxError = max(maxError, abs(y[i]-4.0f)); cout << "Max error: " << maxError; }
然后run吧
参考:http://wenku.baidu.com/view/6ec6b17402768e9951e738d6.html
http://blog.norture.com/2012/10/gpu-parallel-programming-in-vs2012-with-nvidia-cuda/
-
Navigate to the “Configuration Properties\Linker\General” option - In the “Additional Library Directories” field, add “$(CUDA_PATH)lib\$(PlatformName)”
- Go to the “Configuration Properties\Linker\Input” option
- Lastly in the “Additional Dependencies” field, add “cudart.lib”
在属性页/链接/常规里找到附加库目录,添加“%CUDA_LIB_PATH%”
在属性页/链接/输入里找到附加依赖项,添加cudart.lib