KILEYI

导航

Win7命令行编译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之类的根据自己的显卡强度看着来

 1 #include <iostream>
 2 
 3 #include <cuda.h>
 4 #include <cuda_runtime.h>
 5 #include <device_launch_parameters.h>
 6 
 7 using namespace std;
 8 
 9 __global__ void AddIntsCUDA(int* a, int* b)
10 {
11     for (int i = 0; i < 40000000; ++i)
12     {
13         a[0] += b[0];
14     }
15     
16 }
17 
18 int main()
19 {
20     int h_a = 0;
21     int h_b = 1;
22 
23     int* d_a;
24     int* d_b;
25 
26     if (cudaMalloc(&d_a, sizeof(int)) != cudaSuccess)
27     {
28         cout << "Error CUDA allocating memory" << endl;
29         return 0;
30     }
31     if (cudaMalloc(&d_b, sizeof(int)) != cudaSuccess)
32     {
33         cout << "Error CUDA allocating memory" << endl;
34         cudaFree(d_a);
35         return 0;
36     }
37 
38     if (cudaMemcpy(d_a, &h_a, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess)
39     {
40         cout << "Error CUDA copying memory" << endl;
41         cudaFree(d_a);
42         cudaFree(d_b);
43         return 0;
44     }
45     if (cudaMemcpy(d_b, &h_b, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess)
46     {
47         cout << "Error CUDA copying memory" << endl;
48         cudaFree(d_a);
49         cudaFree(d_b);
50         return 0;
51     }
52 
53     AddIntsCUDA << <1, 1 >> >(d_a, d_b);
54 
55     if (cudaMemcpy(&h_a, d_a, sizeof(int), cudaMemcpyDeviceToHost) != cudaSuccess)
56     {
57         cout << "Error CUDA copying memory" << endl;
58         cudaFree(d_a);
59         cudaFree(d_b);
60         return 0;
61     }
62 
63     if (cudaMemcpy(&h_b, d_b, sizeof(int), cudaMemcpyDeviceToHost) != cudaSuccess)
64     {
65         cout << "Error CUDA copying memory" << endl;
66         cudaFree(d_a);
67         cudaFree(d_b);
68         return 0;
69     }
70 
71     cout << "h_a is : " << h_a << endl;
72 
73     cudaFree(d_a);
74     cudaFree(d_b);
75 
76     // cudaDeviceReset must be called before exiting in order for profiling and
77     // tracing tools such as Nsight and Visual Profiler to show complete traces.
78     cudaError_t cudaStatus = cudaDeviceReset();
79     if (cudaStatus != cudaSuccess) {
80         fprintf(stderr, "cudaDeviceReset failed!");
81         return 1;
82     }
83 
84 
85     return 0;
86 }

 

 

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

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

 

Windows Registry Editor Version 5.00

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

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

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

posted on 2016-05-14 11:53  KILEYI  阅读(1818)  评论(0编辑  收藏  举报