练习中遇到一个问题,核函数不执行,不知道原因?
typedef struct
{
int x;
int y;
}TstRst;
typedef struct
{
int x;
int y;
int z;
}TstDat;
device int TstDeviceFun(const int * pData1, const int * pData2)
{
return 100;
}
global void Kernel_Tst(TstRst*pRsts, const TstDat *pDats, const int nCount)
{
int i = threadIdx.x;
pRsts[i].y = 100;
return;
}
main里的相关调用如下
int nCount = 200;
TstRst*pTstRsts;
TstDat pTstDats;
TstRst*pAjustRsts = (TstRst)malloc(nCount * sizeof(TstRst));
TstDat*pAjustDats = (TstDat*)malloc(nCount * sizeof(TstDat));
memset(pAjustRsts, 0, nCount * sizeof(TstRst));
memset(pAjustDats, 0, nCount * sizeof(TstDat));
cudaMalloc((void**)&pTstRsts, nCount * sizeof(TstRst));
cudaMemcpy(pTstRsts, pAjustRsts, nCount * sizeof(TstRst), cudaMemcpyHostToDevice);
cudaMalloc((void**)&pTstDats, nCount * sizeof(TstDat));
cudaMemcpy(pTstDats, pAjustDats, nCount * sizeof(TstDat), cudaMemcpyHostToDevice);
Kernel_Tst <<<1, 100 >>>(pTstRsts, pTstDats, 100);
cudaStatus = cudaMemcpy(pAjustRsts, pTstRsts, nStockCount * sizeof(StockCoorInfo), cudaMemcpyDeviceToHost);
cudaStatus = cudaThreadSynchronize();
已解决,因为在窗口系统下,Kernel函数的执行有时间限制,Kernel函数执行时间过长而出错了
原因有多种多样的:
(1)其中常见的一种是,32 * 32 = 1024线程的block大小,超出了贵卡的能力范围。
(需要fermi+的,请告知一下你的卡的信息)
(2)此外的一种常见原因是越界访存,请确保你用的数据(例如缓冲区)也随之相应扩大了。
建议:
(1)检查offset的值范围,看看能否在(32,32,1)的block shape下,超出有效范围。
(2)在代码里读取dis[offset]前, 用if (offset < ....) 来限制一下(....是你自己知道的一个范围)。
(3)如果(1)(2)未能解决问题,则开启nsight的cuda memory checker, 直接用nsight定位可能的越界位置。