玫瑰幻想 发表于 2013-11-26 13:51
楼主您好:
如果您是在kernel中使用的malloc,那么您依然可以复制到host memory的,
您好,那我的创建方法是没错是么?我的几个函数代码:
struct cPoint
{
int x; //height
int y;
int data;
};
__device__ void qiu(int* reslut,int tid)
{
int width1=5;
int height1=10;
cPoint** corner1=(cPoint**)malloc(sizeof(cPoint*)*width1);
(*corner1)->data=(int)malloc(sizeof(int)*width1);
(*corner1)->x=(int)malloc(sizeof(int)*width1);
(*corner1)->y=(int)malloc(sizeof(int)*width1);
for (int i=0;i<width1;i++)
{
corner1=(cPoint*)malloc(sizeof(cPoint)*height1);
corner1->data=(int)malloc(sizeof(int)*height1);
corner1->x=(int)malloc(sizeof(int)*height1);
corner1->y=(int)malloc(sizeof(int)*height1);
}
for (int i=0;i<THREAD_NUM*4;i++)
{
reslut=i;
}
}
__global__ static void CudaPlite(char* gray1,char* gray2,int width1,int height1,int width2,int height2,int* reslut)
{
const int tid=threadIdx.x;
qiu(reslut,tid);
}
void CutGrayImg(char* grayData11,char* grayData22,int width1,int height1,int width2,int height2)
{
int n=THREAD_NUM;
int len=height1/n;
int imageSize1,imageSize2;
imageSize1=(width1+3)/4*4*height1;
imageSize2=(width2+3)/4*4*height2;
cudaSetDevice(0); //CUDA begin
cudaDeviceSynchronize();
cudaThreadSynchronize();
char* gpuImg1,* gpuImg2;
int* reslut;
int xyz[THREAD_NUM*4];
checkCudaErrors(cudaMalloc((void**)&gpuImg1,sizeof(char)*imageSize1));
checkCudaErrors(cudaMalloc((void**)&gpuImg2,sizeof(char)*imageSize2));
checkCudaErrors(cudaMalloc((void**)&reslut,sizeof(int)*n*4));
// checkCudaErrors(cudaMemset(reslut,0,sizeof(int)*n*4));
checkCudaErrors(cudaMemcpy(gpuImg1,grayData11,sizeof(char)*imageSize1,cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(gpuImg2,grayData22,sizeof(char)*imageSize2,cudaMemcpyHostToDevice));
CudaPlite<<<1,THREAD_NUM,THREAD_NUM*4*sizeof(int)>>>(gpuImg1,gpuImg2,width1,height1,width2,height2,reslut);
checkCudaErrors(cudaMemcpy(xyz,reslut,sizeof(int)*n*4,cudaMemcpyDeviceToHost));
cudaFree(gpuImg1);
cudaFree(gpuImg2);
cudaFree(reslut);
}
但是我如果在qiu函数里面添加了这段二维创建代码之后,在CutGrayImg函数里面就无法将reslut的值复制到xyz上面;如果删除二维数组创建代码,就能复制成功?我不明白为什么? |