谢谢版主的回答
我的C段代码测试的时间为1ms,代码如下
1.float*sal是将RGB图像变换成灰度图像,进行归一化的输入,
2.float*img3f是最开始的进行归一化的RGB输入图像
3.,int threshold是进行二值化时使用的阈值
4.float*image_output1是最后的输出,宽度是图像的三倍,高不变
clock_t start,finish;
start=clock();
for (int r = 0; r < sal.rows; r++)
{
//为数据的每一行的首地址指针
float *s = sal.ptr(r);
float *s1=image_output1.ptr(r);
float *rgb=img3f.ptr(r);
for (int c = 0; c <sal.cols; c++,rgb+=3)
{
//数据处理部分
s1[c+sal.cols]=s[c]*255;
s1[c]=(rgb[0]+rgb[1]+rgb[2])255/3;
if ((s[c]255)>threshold)
{
s1[c+2sal.cols]=255;
}
else
{
s1[c+2sal.cols]=0;
}
}
}
finish=clock();
double time=finish-start;
printf(“C处理此部分花费的时间所花的时间%fms”,time);
而我将它转成CUDA代码是,内核函数kernel所运行时间0.25ms,但是我测量完整的分配显存拷贝数据和调用kernel的函数的测量总的时间居然达到了67ms,代码如下
/////////////////////////////////////////////////////////////////////////////////
1.floatdev_sal是将RGB图像变换成灰度图像,进行归一化的输入,
2.floatdev_img3f是最开始的进行归一化的RGB输入图像
3.,int threshold是进行二值化时使用的阈值
4.floatdev_image_output1是最后的输出,宽度是图像的三倍,高不变
int Combin(floathos_sal,floathos_img3f,int width,int height,
int threshold,floathos_image_output1)
{
checkCudaErrors(cudaSetDevice(0));
cudaEvent_t start,stop;
float time;
int size=widthheightsizeof(float);
floatdev_sal=NULL;
floatdev_img3f=NULL;
float*dev_image_output1=NULL;
checkCudaErrors(cudaMalloc((void**)&dev_sal,size));
checkCudaErrors(cudaMalloc((void**)&dev_img3f,3size));
checkCudaErrors(cudaMalloc((void**)&dev_image_output1,3size));
checkCudaErrors(cudaMemcpy(dev_sal,hos_sal,size,cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(dev_img3f,hos_img3f,3*size,cudaMemcpyHostToDevice));
checkCudaErrors(cudaEventCreate(&start));
checkCudaErrors(cudaEventCreate(&stop));
checkCudaErrors(cudaEventRecord(start,0));
dim3 Dimblock(16,16,1);
dim3 Dimgrid((width+Dimblock.x-1)/Dimblock.x,(height+Dimblock.y-1)/Dimblock.y,1);
Combinekernel<<<Dimblock,Dimgrid>>>
(dev_sal,dev_img3f,width,height,threshold,dev_image_output1);
getLastCudaError(“Combinekernel function execution failed”);
checkCudaErrors(cudaEventRecord(stop,0));
checkCudaErrors(cudaEventSynchronize(stop));
checkCudaErrors(cudaEventElapsedTime(&time,start,stop));
checkCudaErrors(cudaMemcpy(hos_image_output1,dev_image_output1,3*size,cudaMemcpyDeviceToHost));
checkCudaErrors(cudaFree(dev_sal));
checkCudaErrors(cudaFree(dev_img3f));
checkCudaErrors(cudaFree(dev_image_output1));
printf(“kernel cost time=%fms\n”,time);
return 0;
}
请问版主,这么做有什么弊端吗,为什么时间会差距的如此之大,希望版主提点宝贵意见