gpt4 book ai didi

c++ - gpu::morphologyEx 在 CPU 中比 morphologyEx 慢?

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:52 28 4
gpt4 key购买 nike

我正在编写一个 c++ 代码,用于比较使用 CPU 和 GPU 版本的 opencv morphologyEx 方法的性能。这是我的代码:

#include <opencv2/opencv.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <sys/time.h>
#include <ctime>
using namespace cv;
using namespace std;


double start_timer()
{
double start_time = (double) getTickCount();
return start_time;
}

double end_timer(double start_time,int num_tests)
{
double time = (1000 * ((double) getTickCount() - start_time)/ getTickFrequency());
cout << "Average time of " << num_tests << " frames is: " << time/num_tests << " ms" << endl;
return time;
}


int main()
{
Mat cpuSrc;
cv::gpu::GpuMat src_gpu, dst_gpu;
Mat dst;
Mat element;
int element_shape = MORPH_RECT;
element = getStructuringElement(element_shape, Size(10, 10 ), Point(-1, -1) );
cpuSrc = imread("images.jpeg",CV_LOAD_IMAGE_ANYDEPTH);

if (!cpuSrc.data)
{
cerr << "Cannot read the data" << endl;
return -1;
}


cout << "Starting calculating time for CPU ....." << endl;
double start_time = start_timer();
int d = 0;
while(d<100)
{
cv::morphologyEx(cpuSrc, dst, CV_MOP_OPEN, element,Point(-1,-1),1);
}

double total_time_cpu = end_timer(start_time,d);



//--------------------------------------------------------------
cout << "Starting calculating time for GPU ....." << endl;
d = 0;
cv::gpu::GpuMat buf1, buf2;
gpu::Stream stream;
double start_time_1 = start_timer();

while(d<100)
{
stream.enqueueUpload(cpuSrc, src_gpu);
cv::gpu::morphologyEx(src_gpu,dst_gpu,CV_MOP_OPEN,element,
buf1,buf2,Point(-1,-1),1,stream);
stream.enqueueDownload(dst_gpu, dst);

}
stream.waitForCompletion();
double total_time_gpu = end_timer(start_time_1,d);

cout << "Gain is: " << total_time_cpu / total_time_gpu << endl;
return 0;
}

I am using a loop as if i am simulating a video that contains 100 frames. I am using NVIDIA Corporation GF110 [GeForce GTX 570] and Intel Corporation Xeon E5/Core i7 DMI2. Moreover, i tested the time for uploading and downloading and it is very large in the first frame but after that it can be neglected approximately for uploading it is 0.02ms per frame and downloading is 0.1ms and the main time consumption is with the morphologyEx operation.


本次模拟的时间结果如下:

for CPU morphology version, The average time of 100 frames is:: 0.027349 ms and for the GPU version is:: 18.0128 ms

Could you please help me to figure out what might be the reasons for such unexpected performance?!!

非常感谢您。

最佳答案

在初始化中你应该调用:

cv::gpu::setDevice(0);

它会加速初始化。

关于c++ - gpu::morphologyEx 在 CPU 中比 morphologyEx 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054874/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com