因为程序无法拷出来, 因此只能 在此输一遍
#include<>
.....
#define blocksize 16
#define W 145
#define H 145
#define B 220
__global__ void nvdiCUDA(int *Arr, float *Op, int wW, int wH)
{ int x=threadIdx.x+blockIdx.x*blockDim.x;
int y=threadIdx.y+blockIdx.y*blockDim.y;
float value=0;
if(x<wW&&y<wH)
{ value=Arr[59*wW*wH+x*wH+Y]+Arr[27*wW*wH+x*wH+Y];
Op[x*wH+y]=value;
}
}
int nvdi(int argc,char **argv,int blocksize,int *arr, float *op, dim3 &dimsA, dim3 &dimsO)
{ int *d_arr;
float *d_op ;
unsigned int mem_size_A=dimsA.x*dimsA.y*dimsA.z*sizeof(int);
unsigned int mem_size_O=dimsA.x*dimsA.y**sizeof(float);
cudaEror_t error;
error=cudaMalloc((void**)&d_Arr,mem_size_A);
if(error!=cudaSuccess)
{printf("cudaMalloc d_Arr return error code %d, line(%d)\n",error,__LINE__);
exit(EXIT_FAILURE);
}
error=cudaMalloc((void**)&d_Op,mem_size_O);
if(error!=cudaSuccess)
{printf("cudaMalloc d_Op return error code %d, line(%d)\n",error,__LINE__);
exit(EXIT_FAILURE);
}
error=cudaMemcpy(d_Arr,Arr,mem_size_A,cudaMemcpyHostToDevice);
if(error!=cudaSuccess)
{printf("cudaMemcpy(d_Arr,Arr) return error code %d, line(%d)\n",error,__LINE__);
exit(EXIT_FAILURE);
}
dim3 threads(blocksize,blocksize);
dim3 blocks((dimsA.x+blocksize-1)/blocksize,(dimsA.y+blocksize-1)/blocksize);
nvdiCUDA<<<blocks,threads>>>(d_Arr, d_Op,dimsA.x,dimsA.y);
cudaDeviceSynchronize();
cudaEvent_t start;
error=cudaEventCreat(&start);
if(error!=cudaSuccess)
{printf(stderr,"failed to creat start event(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
cudaEvent_t stop;
error=cudaEventCreat(&stop);
if(error!=cudaSuccess)
{printf(stderr,"failed to creat stop event(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
//recore start event
error=cudaEventRecord(start,NULL);
if(error!=cudaSuccess)
{printf(stderr,"failed to record start event(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
//excute the kernel
int nter=300;
for(int j=0;j<nter;j++){
nvdiCUDA<<<blocks,threads>>>(d_Arr,d_Op,dimsA.x,dimsA.y);
cudaDeviceSynchornize();}
//record stop event
error=cudaEventRecord(stop,NULL);
if(error!=cudaSuccess)
{printf(stderr,"failed to record stop event(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
//wait for the stop event
error=cudaEventSynchornize(stop);
if(error!=cudaSuccess)
{printf(stderr,"failed to synchronize on the stop event(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
float msecTotal = 0.0f;
error=cudaEventElapsedTime(&msecTotal,start,stop);
if(error!=cudaSuccess)
{printf(stderr,"failed to get time elapsed between events(errorcode %s)!\n",cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
float mescPerNvdi=msecTotal/nter;
printf("time=%.3f",mescperNvdi);
//copy result
error=cudaMemcpy(Op,d_Op,mem_size_O,cudaMemcpyDeviceToHost);
if(error!=cudaSuccess)
{printf("cudamencpy(Op,d_Op) returned error code %d,line(%d)\n",error,__LINE__);
exit(EXIT_FAILURE);
}
//clean up mem
cudaFree(d_Arr);
cudaFree(d_Op);
cudaDeviceReset();
return 1;
}
int main(int argc, char **argv)
{ intdevID = 0;
// 读设备参数
。。。。。
dim3 dimsA(W,H,B);
dim3 dimsO(W,H,1);
unsigned int sizea_s,sizeo_s,sizea,sizeo;
int i,*array;
float *oput;
sizea_s=W*H*B;
sizeo_s=W*H;
sizea=sizea_s*sizeof(int);
sizeo=sizeo_s*sizeof(float);
array=(int*)malloc(sizea);
oput=(float*)malloc(sizeo);
if(array==NULL||oput==NULL)
{printf("allocate mem failed\n");
return 0;
}
FILE *p;
if((fp=fopen("rcb.raw","r"))==NULL)
{printf("open file failed\n");
exit(0);
}
if((fread(array,sizeof(int),sizea_s,fp))==NULL)
{printf("read data failed\n");
exit(0);
}
fclose(fp);
int nvdi_result = nvdi(argc,argv,blocksize,array,oput,dimsA,dimsO);
FILE *P;
P=FOPEN("ndvi,txt","w");
if((fwrite(oput,sizeof(float),sizeo_s,p))==NULL)
{printf("write file failed\n"); exit(0);
}
fclose(p);
free(array);
free(oput);
exit(nvdi_result);
}
rcb.row是读入的外部文件, 查看了一下读入结果,前面数据输入正确,但是在第220个矩阵的最后一列从53-144个数据是错误的并且都为-842150451,查看了一个数据array[145*145*59]=2480,array[145*145*27]=4305,但是最后得到op[0]=8.7619483e+033,这个 会是怎么回事,还有 数据读入array为何最后92个数据 输入不对
|