OPENCV例子opencv-4.5.5\samples\gpu\multi.cpp的代码分析

此示例演示在不同 GPU 上执行独立任务的方式

并行计算 cv::parallel_for_() 函数

#include <opencv2/core/utility.hpp>
void cv::parallel_for_    (         const Range &     range,
                                               const ParallelLoopBody &     body,
                                               double     nstripes = -1. 
                                        )    

示例全局调用情况如下:

 

示例流程图情况如下:

 

示例UML逻辑图情况如下:

 

示例源代码如下:

/* This sample demonstrates the way you can perform independent tasks

   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>

#include "opencv2/core.hpp"

#include "opencv2/cudaarithm.hpp"

using namespace std;

using namespace cv;

using namespace cv::cuda;

struct Worker : public cv::ParallelLoopBody

{

    void operator()(const Range& r) const CV_OVERRIDE

    {

        for (int i = r.start; i < r.end; ++i) { this->operator()(i); }

    }

    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)

    {

        cv::cuda::printShortCudaDeviceInfo(i);

        DeviceInfo dev_info(i);

        if (!dev_info.isCompatible())

        {

            std::cout << "CUDA module isn't built for GPU #" << i << " ("

                 << dev_info.name() << ", CC " << dev_info.majorVersion()

                 << dev_info.minorVersion() << "\n";

            return -1;

        }

    }

    // Execute calculation in two threads using two GPUs使用两个 GPU 在两个线程中执行计算

    cv::Range devices(0, 2);

    cv::parallel_for_(devices, Worker(), devices.size());

    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 worksCPU干活

    cv::transpose(src, dst);

    // GPU works GPU干活

    GpuMat d_src(src);

    GpuMat d_dst;

    cuda::transpose(d_src, d_dst);

    // Check results检查结果

    bool passed = cv::norm(dst - Mat(d_dst), NORM_INF) < 1e-3;

    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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqq9668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值