为什么我在cuda中尝试此代码时会看到黑屏?
我在“visual studio 2010”中使用Win8和Nsight,并为我的图形卡(9300m Gs)安装了“310.90-notebook-Win8-win7-winvista-32bit-international-whql”。但当我尝试下面的代码时,我看到了一个黑屏!错误:“显示驱动程序停止响应并已恢复”! 我知道问题出在“cudaMemcpy”上,但我不知道为什么为什么我在cuda中尝试此代码时会看到黑屏?,cuda,nsight,Cuda,Nsight,我在“visual studio 2010”中使用Win8和Nsight,并为我的图形卡(9300m Gs)安装了“310.90-notebook-Win8-win7-winvista-32bit-international-whql”。但当我尝试下面的代码时,我看到了一个黑屏!错误:“显示驱动程序停止响应并已恢复”! 我知道问题出在“cudaMemcpy”上,但我不知道为什么 #include "cuda_runtime.h" #include "device_launch_parameter
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#define N 8
__global__ void kernel(int *a)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int step = x;
while(step<N){
a[step] = threadIdx.x;
step += x;
}
}
int main()
{
int a[N],i=N,j=0;
for(;j<N;j++)
a[j]=i--;
int *dev_a;
cudaMalloc( (void**)&dev_a, N * sizeof(int) );
cudaMemcpy( dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice);
kernel<<<2,2>>>(dev_a);
cudaError_t cudaStatus = cudaMemcpy(a, dev_a,N-1 * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
//goto Error;
}
for(j=0;j<N;j++)printf("\n%d",a[j]);
int t;
scanf("%d",&t);
}
#包括“cuda_runtime.h”
#包括“设备启动参数.h”
#包括
#定义n8
__全局无效内核(int*a)
{
int x=threadIdx.x+blockIdx.x*blockDim.x;
int步长=x;
在内核中(步骤时,threadIdx.x
=0和blockIdx.x
=0的线程,即第一个块的第一个线程将无限期运行,导致内核崩溃
当threadIdx.x
=0和blockIdx.x
=0时,内核代码将变为:
int x = 0;
int step = 0;
while(step<N)
{
a[step] = 0;
step += 0; //This will create infinite loop
}
考虑到C中的运算符优先级,表达式N-1*sizeof(int)
将计算为N-4
(如果sizeof(int)
为4)在内核中,threadIdx.x
=0和blockIdx.x
=0的线程,即第一个块的第一个线程将无限期运行,导致内核崩溃
当threadIdx.x
=0和blockIdx.x
=0时,内核代码将变为:
int x = 0;
int step = 0;
while(step<N)
{
a[step] = 0;
step += 0; //This will create infinite loop
}
考虑到C中的运算符优先级,表达式N-1*sizeof(int)
将计算为N-4
(如果sizeof(int)
为4)。可能重复的