我运行下面的代码来检查 GPU 和 CPU 使用率之间的性能差异。我正在计算平均时间cv::cvtColor()
功能。我进行了四个函数调用:
-
Just_mat()
(不使用 OpenCLMat
目的)
-
Just_UMat()
(不使用 OpenCLUmat
目的)
-
OpenCL_Mat()
(使用 OpenCL 进行Mat
object)
-
OpenCL_UMat()
(使用 OpenCL 进行UMat
目的)
适用于 CPU 和 GPU。
我没有发现 GPU 和 CPU 使用率之间存在巨大的性能差异。
int main(int argc, char* argv[])
{
loc = argv[1];
just_mat(loc);// Calling function Without OpenCL
just_umat(loc);//Calling function Without OpenCL
cv::ocl::Context context;
std::vector<cv::ocl::PlatformInfo> platforms;
cv::ocl::getPlatfomsInfo(platforms);
for (size_t i = 0; i < platforms.size(); i++)
{
//Access to Platform
const cv::ocl::PlatformInfo* platform = &platforms[i];
//Platform Name
std::cout << "Platform Name: " << platform->name().c_str() << "\n" << endl;
//Access Device within Platform
cv::ocl::Device current_device;
for (int j = 0; j < platform->deviceNumber(); j++)
{
//Access Device
platform->getDevice(current_device, j);
int deviceType = current_device.type();
cout << "Device name: " << current_device.name() << endl;
if (deviceType == 2)
cout << context.ndevices() << " CPU devices are detected." << std::endl;
if (deviceType == 4)
cout << context.ndevices() << " GPU devices are detected." << std::endl;
cout << "===============================================" << endl << endl;
switch (deviceType)
{
case (1 << 1):
cout << "CPU device\n";
if (context.create(deviceType))
opencl_mat(loc);//With OpenCL Mat
break;
case (1 << 2):
cout << "GPU device\n";
if (context.create(deviceType))
opencl_mat(loc);//With OpenCL UMat
break;
}
cin.ignore(1);
}
}
return 0;
}
int just_mat(string loc);// I check for the average time taken for cvtColor() without using OpenCl
int just_umat(string loc);// I check for the average time taken for cvtColor() without using OpenCl
int opencl_mat(string loc);//ocl::setUseOpenCL(true); and check for time difference for cvtColor function
int opencl_umat(string loc);//ocl::setUseOpenCL(true); and check for time difference for cvtColor function
上述代码的输出(以毫秒为单位)是
__________________________________________
|GPU 名称|使用 OpenCL Mat |使用 OpenCl UMat|
|________________________________________|
|--卡里佐---|------7.69052 ------ |-----0.247069--------|
|________________________________________|
|---岛屿--- |--------7.12455----- |-----0.233345-----|
|________________________________________|
__________________________________________
|----CPU---|带有 OpenCL Mat |使用 OpenCl UMat |
|________________________________________|
|---AMD---|--------6.76169 ------ |--------0.231103--------|
|________________________________________|
________________________________________________
|----CPU---|没有 OpenCL Mat |没有OpenCl UMat |
|________________________________________________________|
|----AMD---|--------7.15959------ |------------0.246138------------ |
|________________________________________________________|
在代码中,无论代码如何,使用 Mat 对象始终在 CPU 上运行,而使用 UMat 对象始终在 GPU 上运行ocl::setUseOpenCL(true/false);
有人能解释所有输出时间变化的原因吗?
还有一个问题,我没有使用任何 OpenCL 特定的 .dll 和 .exe 文件,但使用 GPU 时没有任何错误,在使用 Cmake 构建 OpenCV 时我检查过With_OpenCL
这是否构建了所有 OpenCL 所需的功能opencv_World310.dll
?