CUDA(8)之显卡驱动自动中断服务并恢复问题解决

摘要

本文主要讲述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. 再次运行程序,问题成功解决

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值