第一次使用intel thread building blocks總結_並行計算
來源:互聯網
上載者:User
主要使用了基本的parallel_for的用法,按照師兄的版本修改了自己的字元識別函數,師兄建立了一個類作為一個容器,裡面承載了一個用於字元識別類的指標,具體原因據師兄說是因為在並行化的時候會建立多個類的執行個體,所以傳送一個指標,在並行類之外建立一次類的執行個體即可,代碼如下: #include <QtCore/QCoreApplication> #include "patternMatch.h" #include <tbb\concurrent_vector.h> #include <tbb\tbb.h> #include <tbb\scalable_allocator.h> #include <array> //using namespace cv; using namespace tbb;
class paralellRecognize { public: patternMatch *recognizeCore; cv::Mat *allFrame; paralellRecognize(patternMatch* newRecognize, cv::Mat* newlist) { recognizeCore = newRecognize; allFrame = newlist; }
void operator()(const blocked_range<long>& r) const { cv::Mat* a = allFrame;
for (long i = r.begin(); i != r.end(); ++i){ //cout << "frame index" << " " << i << endl; recognizeCore->RecognizeOneImage(i); }
}
~paralellRecognize(){ } };
陰影顯示的即為關鍵處,在main函數中的代碼: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv);
cv::Mat *mat = NULL; patternMatch *newRecognize = new patternMatch(4, 1986); // 執行個體化一次,4和1984是表示當前視頻有4行,1986幀,測試方便而使用了具體的數 int totalFileNum = newRecognize->GetFileNum(); // 非並行版本 tick_count t0 = tick_count::now(); // tick_count是tbb中內建計算函數已耗用時間的類型,很好用哦 newRecognize->startMatch(); tick_count t1 = tick_count::now();
// 並行版本 tick_count t2 = tick_count::now(); parallel_for(blocked_range<long>(0, totalFileNum), paralellRecognize(newRecognize, mat)); tick_count t3 = tick_count::now(); printf("serial work took %g seconds\n", (t1 - t0).seconds()); printf("parallel work took %g seconds\n", (t3 - t2).seconds()); double serialTime = (t1 - t0).seconds(); double parallelTime = (t3 - t2).seconds(); printf("ratio for two timt: %f\n", serialTime / parallelTime); // 列印結果至txt檔案 newRecognize->printText(); printf("Write file completed.\n"); return a.exec(); }
其次是release發布程式的時候,需要tbb.dll,我使用的是win32模式的release,所以應該使用的是32位版本的,我電腦中所在目錄為C:\Program Files (x86)\Intel\Composer XE 2015\redist\ia32\tbb\vc12,如果是64位的話,應該在C:\Program Files (x86)\Intel\Composer XE 2015\redist\intel64\tbb中選擇相應的tbb.dll檔案。否則會出現無法運行exe程式,應用程式無法正常啟動0xc000007b之類的問題。