Win7 64位命令行编译cuda及设置Windows显卡响应时间

时间:2023-03-09 05:13:07
Win7 64位命令行编译cuda及设置Windows显卡响应时间

在开始菜单中找到Visual Studio 2013 >> Visual Studio Tools

选择86或64版本的VC命令提示符环境,我用的

VS2013 x86 Native Tools Command Prompt

这样应该就会配置好VC编译器的Path,环境变量中又有nvcc(cuda的c编译器)的Path

然后输入

nvcc cudaFileName.cu -o outFileName

这种格式,比如

nvcc hello.cu -o hello

就会编译hello.cu文件,生成hello.exe文件

顺便你如果好奇,肯定会拿VS2015的命令提示符环境试一次,但是肯定会失败,这再次说明了cuda从根本上就不支持VS2015,想在VS2015中使用cuda只能等待官方

============================================================

再来看看响应时间,下面的测试代码在我的笔记本Geforce GT 755m上大概要跑8秒(心里估算,没实际测试具体时间),如果出现

显卡驱动停止响应并已成功恢复

说明测试成功了,如果没出现,你可以修改一下那个循环数,乘2或者乘10之类的根据自己的显卡强度看着来

 #include <iostream>

 #include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h> using namespace std; __global__ void AddIntsCUDA(int* a, int* b)
{
for (int i = ; i < ; ++i)
{
a[] += b[];
} } int main()
{
int h_a = ;
int h_b = ; int* d_a;
int* d_b; if (cudaMalloc(&d_a, sizeof(int)) != cudaSuccess)
{
cout << "Error CUDA allocating memory" << endl;
return ;
}
if (cudaMalloc(&d_b, sizeof(int)) != cudaSuccess)
{
cout << "Error CUDA allocating memory" << endl;
cudaFree(d_a);
return ;
} if (cudaMemcpy(d_a, &h_a, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess)
{
cout << "Error CUDA copying memory" << endl;
cudaFree(d_a);
cudaFree(d_b);
return ;
}
if (cudaMemcpy(d_b, &h_b, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess)
{
cout << "Error CUDA copying memory" << endl;
cudaFree(d_a);
cudaFree(d_b);
return ;
} AddIntsCUDA << <, >> >(d_a, d_b); if (cudaMemcpy(&h_a, d_a, sizeof(int), cudaMemcpyDeviceToHost) != cudaSuccess)
{
cout << "Error CUDA copying memory" << endl;
cudaFree(d_a);
cudaFree(d_b);
return ;
} if (cudaMemcpy(&h_b, d_b, sizeof(int), cudaMemcpyDeviceToHost) != cudaSuccess)
{
cout << "Error CUDA copying memory" << endl;
cudaFree(d_a);
cudaFree(d_b);
return ;
} cout << "h_a is : " << h_a << endl; cudaFree(d_a);
cudaFree(d_b); // cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaError_t cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return ;
} return ;
}

出现这个说明程序运时时间过长,默认好像是2秒,windows接收不到显卡的响应

解决办法,改注册表,建议先备份注册表,以防万一

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\GraphicsDrivers]
"TdrLevel"=dword:
"TdrDelay"=dword:

TdrDelay是延时时间,改个自己感觉可以的就行了,我用的0x20,看某教程随便跟着写的,不过一般程序应该也不会运行这么久

完全是测试,但是指不定哪天突然想写个“蹩脚”的神奇算法要运行个5分10分钟的,也许用得上,谁知道呢