프리픽스 합 계산을위한 코드를 작성 중입니다. 여기서 상기 커널의 커널 내CUDA 커널의 이상한 동작
__global__ void prescan(int *indata,int *outdata,int n,long int *sums)
{
extern __shared__ int temp[];
int tid=threadIdx.x;
int offset=1,start_id,end_id;
int *global_sum=&temp[n+2];
if(tid==0)
{
temp[n]=blockDim.x*blockIdx.x;
temp[n+1]=blockDim.x*(blockIdx.x+1)-1;
start_id=temp[n];
end_id=temp[n+1];
//cuPrintf("Value of start %d and end %d\n",start_id,end_id);
}
__syncthreads();
start_id=temp[n];
end_id=temp[n+1];
temp[tid]=indata[start_id+tid];
temp[tid+1]=indata[start_id+tid+1];
for(int d=n>>1;d>0;d>>=1)
{
__syncthreads();
if(tid<d)
{
int ai=offset*(2*tid+1)-1;
int bi=offset*(2*tid+2)-1;
temp[bi]+=temp[ai];
}
offset*=2;
}
if(tid==0)
{
sums[blockIdx.x]=temp[n-1];
temp[n-1]=0;
cuPrintf("sums %d\n",sums[blockIdx.x]);
}
for(int d=1;d<n;d*=2)
{
offset>>=1;
__syncthreads();
if(tid<d)
{
int ai=offset*(2*tid+1)-1;
int bi=offset*(2*tid+2)-1;
int t=temp[ai];
temp[ai]=temp[bi];
temp[bi]+=t;
}
}
__syncthreads();
if(tid==0)
{
outdata[start_id]=0;
}
__threadfence_block();
__syncthreads();
outdata[start_id+tid]=temp[tid];
outdata[start_id+tid+1]=temp[tid+1];
__syncthreads();
if(tid==0)
{
temp[0]=0;
outdata[start_id]=0;
}
__threadfence_block();
__syncthreads();
if(blockIdx.x==0 && threadIdx.x==0)
{
for(int i=1;i<gridDim.x;i++)
{
sums[i]=sums[i]+sums[i-1];
}
}
__syncthreads();
__threadfence();
if(blockIdx.x==0 && threadIdx.x==0)
{
for(int i=0;i<gridDim.x;i++)
{
cuPrintf("****sums[%d]=%d ",i,sums[i]);
}
}
__syncthreads();
__threadfence();
if(blockIdx.x!=gridDim.x-1)
{
int tid=(blockIdx.x+1)*blockDim.x+threadIdx.x;
if(threadIdx.x==0)
cuPrintf("Adding %d \n",sums[blockIdx.x]);
outdata[tid]+=sums[blockIdx.x];
}
__syncthreads();
}
이고, 합계 어레이 블록 당 프리픽스 합을 축적되고 그리고 제 실이 합 어레이의 프리픽스 합을 계산한다. 이제 장치 쪽에서이 합계 배열을 인쇄하면 올바른 결과가 표시됩니다.
cuPrintf ("% d \ n"합계 [blockIdx.x]);
이 줄은 오래된 값을 취하고 있음을 나타냅니다. 그 이유는 무엇일까요?
불필요한 곳에서도 \ __ threadfence를 사용하고 있지만 여전히 이상한 단서가 있습니까? –
을 사용하면 'sums'배열을 선언 할 수 있습니다. 컴파일러가 결과를 레지스터에 캐시 할 수 있습니다. 즉, sums [i] = sums [i] + sums [i-1]; 비록 내가 완전히 확신하지는 않지만 .. –
"옛 가치를 지니고 있다는 것을 당신이 의미하는 것"이라고하는 것은 무엇을 의미합니까? 정확히 무엇이 잘못 될까요? 이렇게 긴 코드를 게시 할 때 사람들이 완벽하게 이해할 것이라고 기대할 수는 없습니다 - 무슨 일이 일어나고 있는지, 그리고 어떤 질문이 무엇인지에 대해 아주 명료해야합니다. – harrism