opencv/samples/gpu/multi.cpp

104 lines
2.3 KiB
C++
Raw Normal View History

/* This sample demonstrates the way you can perform independent tasks
2012-10-17 15:12:04 +08:00
on the different GPUs */
// Disable some warnings which are caused with CUDA headers
#if defined(_MSC_VER)
#pragma warning(disable: 4201 4408 4100)
#endif
#include <iostream>
2015-11-02 15:05:45 +08:00
#include "opencv2/cvconfig.h"
2016-02-15 21:37:29 +08:00
#include "opencv2/core.hpp"
2013-07-23 19:24:10 +08:00
#include "opencv2/cudaarithm.hpp"
2012-10-17 15:12:04 +08:00
#ifdef HAVE_TBB
# include "tbb/tbb.h"
# include "tbb/task.h"
# undef min
# undef max
#endif
2012-10-17 15:12:04 +08:00
#if !defined(HAVE_CUDA) || !defined(HAVE_TBB)
int main()
{
#if !defined(HAVE_CUDA)
std::cout << "CUDA support is required (CMake key 'WITH_CUDA' must be true).\n";
#endif
#if !defined(HAVE_TBB)
std::cout << "TBB support is required (CMake key 'WITH_TBB' must be true).\n";
#endif
return 0;
}
#else
using namespace std;
using namespace cv;
2013-08-28 19:45:13 +08:00
using namespace cv::cuda;
2012-10-17 15:12:04 +08:00
struct Worker { void operator()(int device_id) const; };
int main()
{
int num_devices = getCudaEnabledDeviceCount();
if (num_devices < 2)
{
std::cout << "Two or more GPUs are required\n";
return -1;
}
for (int i = 0; i < num_devices; ++i)
{
2013-08-28 19:45:13 +08:00
cv::cuda::printShortCudaDeviceInfo(i);
2012-10-17 15:12:04 +08:00
DeviceInfo dev_info(i);
if (!dev_info.isCompatible())
{
2013-07-24 17:55:18 +08:00
std::cout << "CUDA module isn't built for GPU #" << i << " ("
<< dev_info.name() << ", CC " << dev_info.majorVersion()
<< dev_info.minorVersion() << "\n";
2012-10-17 15:12:04 +08:00
return -1;
}
}
// Execute calculation in two threads using two GPUs
int devices[] = {0, 1};
tbb::parallel_do(devices, devices + 2, Worker());
2012-10-17 15:12:04 +08:00
return 0;
}
void Worker::operator()(int device_id) const
{
setDevice(device_id);
Mat src(1000, 1000, CV_32F);
Mat dst;
RNG rng(0);
rng.fill(src, RNG::UNIFORM, 0, 1);
// CPU works
cv::transpose(src, dst);
2012-10-17 15:12:04 +08:00
// GPU works
GpuMat d_src(src);
GpuMat d_dst;
2013-08-28 19:45:13 +08:00
cuda::transpose(d_src, d_dst);
2012-10-17 15:12:04 +08:00
// Check results
bool passed = cv::norm(dst - Mat(d_dst), NORM_INF) < 1e-3;
2012-10-17 15:12:04 +08:00
std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() << "): "
<< (passed ? "passed" : "FAILED") << endl;
// Deallocate data here, otherwise deallocation will be performed
// after context is extracted from the stack
d_src.release();
d_dst.release();
}
#endif