摘要
本文主要讲述CUDA程序运行时,显卡驱动服务超时而自动中断的问题。
1. 错误的案例
运行下面程序,CUDA占用DEVICE时间过长就会自动被操作系统中断服务。
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iostream>
using namespace std;
__global__ void Add(int *a, int *b, int *c){
for (int i=0; i<99999999; i++){
c[0] = a[0]+b[0];
}
}
int main(void) {
// Declare variables
int addResult;
int *devicePointerA;
int *devicePointerB;
int *devicePointerC;
//Create two integers. (Host)
int a = 5;
int b = 9;
// Allocate device memory for these two integers and their add result. (Device)
if(cudaMalloc(&devicePointerA, sizeof(int))!=cudaSuccess){
printf("Error allocating memory (devicePointerA)");
return 0;
}
if(cudaMalloc(&devicePointerB, sizeof(int))!=cudaSuccess){
printf("Error allocating memory (devicePointerB)");
return 0;
}
if(cudaMalloc(&devicePointerC, sizeof(int))!=cudaSuccess){
printf("Error allocating memory (devicePointerC)");
return 0;
}
// Copy these two integers to the device memory. (Host)
if(cudaMemcpy(devicePointerA, &a, sizeof(int), cudaMemcpyHostToDevice)!=cudaSuccess){
printf("Error copying memory devicePointerA cudaMemcpyHostToDevice");
return 0;
}
if(cudaMemcpy(devicePointerB, &b, sizeof(int), cudaMemcpyHostToDevice)!=cudaSuccess){
printf("Error copying memory devicePointerB cudaMemcpyHostToDevice");
return 0;
}
// Call the kernel to add these two integers together. (Device)
Add<<<1, 1>>>(devicePointerA, devicePointerB, devicePointerC);
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Copy the result back to the host memory. (Device)
if(cudaMemcpy(&addResult, devicePointerC, sizeof(int), cudaMemcpyDeviceToHost)!=cudaSuccess){
printf("Error copying memory devicePointerC cudaMemcpyDeviceToHost");
return 0;
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Print out the result. (Host)
printf("The add result is %d \n", addResult);
// Free the device memory. (Device)
cudaFree(devicePointerA);
cudaFree(devicePointerB);
cudaFree(devicePointerC);
// Reset device
cudaDeviceReset();
return 0;
}
2. 测试结果
当运行上面的错误案例之后,测试结果如下所示,即在将CUDA计算结果复制回HOST时,程序崩溃。
并且,操作系统显示如下错误信息。
3. 解决方案(修改windows系统的注册表)
a. 备份注册表
通过cmd打开“注册表编辑器”->点击“文件”选项 -> 点击“导出”选项 。其中,备份的时候"导出范围"记得选择”全部“
备份好的文件如图所示,
b. 修改注册表
在指定路径下,HKEY_LOCAL_MACHINE->SYSTEM->CurrentControlSet->control->GraphicsDrivers,
右键点击GraphicsDrivers,新建DWORD value(32位)类型的变量TdrLevel和TdrDelay。
其中,TdrLevel和TdrDelay分别取值为3(层),20(秒)。
然后,重启windows系统。
4. 再次运行程序,问题成功解决