OpenCV:OpenCV中的 parallel_for 和opencv parallel_for_
??????? OpenCV使用OMP完成并行運算,在使用AdaBoost檢測的時候,在cascadedetect.cpp 里面,大量使用
parallel_for_(Range(0, stripCount), CascadeClassifierInvoker( *this, processingRectSize, stripSize, yStep, factor,candidatesVector, rejectLevels, levelWeights, false, currentMask, &mtx) );語句。
???? ?? 策略對應的機制:
????? ? CascadeClassifierInvoker繼承自于 ParallelLoopBody ,實現parallel_for_( )語句。
???????????????? class CascadeClassifierInvoker : public ParallelLoopBody?
??
?????? CV2.4.3中自帶的calcOpticalFlowPyrLK函數也用parallel_for重寫過了,之前我一直認為parallel_for就是用來并行計算的,之前也自己寫了一些用parallel_for實現的算法。
????? 直到今天在opencv官網中看到別人的提問,才發現parallel_for實際上是serial loop (普通循環結構),而parallel_for_才是parallel loop(OpenCV官網answer),用以實現TBB或者OpenCL并行。
?????? 參考文章:OpenCV中parallel_for 和 parallel_for_學習筆記?
?
??????? 函數定義在parallel.cpp里面,定義為:
/* ================================ parallel_for_ ================================ */void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body, double nstripes) { #ifdef CV_PARALLEL_FRAMEWORKif(numThreads != 0){ProxyLoopBody pbody(body, range, nstripes);cv::Range stripeRange = pbody.stripeRange();#if defined HAVE_TBBtbb::parallel_for(tbb::blocked_range<int>(stripeRange.start, stripeRange.end), pbody); #elif defined HAVE_CSTRIPESparallel(MAX(0, numThreads)){int offset = stripeRange.start;int len = stripeRange.end - offset;Range r(offset + CPX_RANGE_START(len), offset + CPX_RANGE_END(len));pbody(r);barrier();}#elif defined HAVE_OPENMP#pragma omp parallel for schedule(dynamic)for (int i = stripeRange.start; i < stripeRange.end; ++i)pbody(Range(i, i + 1));#elif defined HAVE_GCDdispatch_queue_t concurrent_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_apply_f(stripeRange.end - stripeRange.start, concurrent_queue, &pbody, block_function);#elif defined HAVE_CONCURRENCYif(!pplScheduler || pplScheduler->Id() == Concurrency::CurrentScheduler::Id()){Concurrency::parallel_for(stripeRange.start, stripeRange.end, pbody);}else{pplScheduler->Attach();Concurrency::parallel_for(stripeRange.start, stripeRange.end, pbody);Concurrency::CurrentScheduler::Detach();}#else#error You have hacked and compiling with unsupported parallel framework#endif}else#endif // CV_PARALLEL_FRAMEWORK{(void)nstripes;body(range);} }其中使用的:
tbb::parallel_for(tbb::blocked_range<int>(stripeRange.start, stripeRange.end), pbody);使用TBB重寫 parallel_for 完成使用TBB并行加速。
???????
總結
以上是生活随笔為你收集整理的OpenCV:OpenCV中的 parallel_for 和opencv parallel_for_的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行存款利率表2022最新利率死期,各银
- 下一篇: 搜狗输入法如何设置快捷短语(搜狗搜索引擎