Intel TBB简介及在Windows7 VS2013上源码的编译过程
Intel TBB(Intel Threading Building Blocks)是Intel線程構建塊開源庫,它的License是Apache 2.0.
???????? Intel TBB是一種用于并行編程的基于C++語言的框架,它是一套C++模板庫。它提供了大量特性,具有比線程更高程度的抽象。
???????? Intel TBB可以在Windows、Linux和OSX上運行,支持Intel、Microsoft和GNU工具。
???????? Intel TBB特性:
???????? (1)、與線程不同,你可以對任務使用更高程度的抽象。Intel聲稱,在Linux系統上,啟動和結束任務的速度是對線程執行相同操作的18倍;
(2)、Intel TBB附帶了一個任務調度程序,該程序可以跨多個邏輯和物理內核高效地處理負載平衡。Intel TBB 中的默認任務調度策略不同于大多數線程調度程序所擁有的輪詢策略;
(3)、Intel TBB提供了一些可直接使用的線程安全容器;
(4)、可以使用通用的并行算法,如parallel_for和parallel_reduce;
(5)、模板類atomic中提供了無鎖(Lock-free,也稱為mutex-free)并發編程支持。這種支持使得Intel TBB適合用于高性能的應用程序,因為Intel TBB可以鎖定和解除鎖定互斥體 (mutex);
Intel TBB注意事項:
(1)、要運行Intel TBB程序,則必須正確地初始化任務調度程序;
(2)、Intel TBB提供了一個名為task_list容器,可以將它用作一個任務集合;
(3)、每個父任務都使用allocate_child函數調用創建一個子任務;
(4)、在衍生出任何子任務之前,父任務必須調用set_ref_count。如果沒有這么做,則會導致出現未定義的行為。如果打算衍生一些子任務,然后等待它們完成,那么count的值必須為子任務數+ 1;否則,count會等于子任務的數量;
(5)、最好由調度程序決定最佳的線程數量;
???????? 在Windows7 VS2013上編譯Intel TBB源代碼操作步驟:
???????? (1)、從?https://www.threadingbuildingblocks.org/download?下載源代碼tbb2017_20161128oss_src.tgz ;
???????? (2)、新建三個空工程,分別為tbb、tbbmalloc、tbbmalloc_proxy,項目配置類型為動態庫(.dll),其中tbb、tbbmalloc項目有匯編文件的參與,因此需要在tbb、tbbmalloc工程加入匯編文件的支持,步驟如下:
???????? 選中tbb或tbbmalloc--> 生成依賴項 --> 生成自定義 --> 勾選masm,點擊確定;
???????? (3)、將相應文件加入到工程中;
???????? (4)、生成tbb.def文件:這里有三種簡單方法(tbb.def可以由CMake通過win64-tbb-export.def和win64-tbb-export.lst文件產生):
A、在tbb2017_20161128oss_src/build/vs2012目錄下,打開makefile.sln,升級到vs2013版本,編譯tbb工程,期間便會生成tbb.def文件;
B、從 https://github.com/wjakob/tbb?下載Intel TBB,其提供在Windows下用cmake-gui.exe生成vs2013解決方案,編譯tbb工程,期間也會生成tbb.def文件;
C、從https://www.threadingbuildingblocks.org/download下載二進制文件tbb2017_20161128oss_win,在lib/intel64/vc**目錄內也有tbb.def文件;
(5)、將tbb.def文件加入到tbb工程屬性 --> 鏈接器 --> 命令行 --> 其它選項中;
(6)、生成tbbmalloc.def文件:與以上(4)、(5)中步驟一致;
(7)、三個工程編譯順序:先tbb,然后tbbmalloc,最后再是tbbmalloc_proxy,后者會依賴前者;
(8)、新創建一個IntelTBB_Test控制臺工程,測試代碼如下:
#include "funset.hpp"
#include <iostream>
#include <tbb/tbb.h>// reference: http://www.ibm.com/developerworks/cn/aix/library/au-intelthreadbuilding/
class first_task : public tbb::task {
public:tbb::task* execute() {fprintf(stderr, "Hello World!\n");return nullptr;}
};int test_IntelTBB_1()
{tbb::task_scheduler_init init(tbb::task_scheduler_init::automatic);first_task& f1 = *new(tbb::task::allocate_root()) first_task();tbb::task::spawn_root_and_wait(f1);return 0;
}class first_task_2 : public tbb::task {
public:tbb::task* execute() {fprintf(stderr, "Hello World!\n");tbb::task_list list1;list1.push_back(*new(allocate_child()) first_task_2());list1.push_back(*new(allocate_child()) first_task_2());set_ref_count(3); // 2 (1 per child task) + 1 (for the wait) spawn_and_wait_for_all(list1);return nullptr;}
};int test_IntelTBB_2()
{first_task& f1 = *new(tbb::task::allocate_root()) first_task();tbb::task::spawn_root_and_wait(f1);return 0;
}class say_hello {
public:say_hello(const char* str) : message(str) { }void operator( ) () const {fprintf(stderr, "%s\n", message);}
private:const char* message;
};int test_IntelTBB_3()
{tbb::task_group tg;tg.run(say_hello("child 1")); // spawn task and returntg.run(say_hello("child 2")); // spawn another task and return tg.wait(); // wait for tasks to completereturn 0;
}
GitHub: https://github.com/fengbingchun/Face_Test
總結
以上是生活随笔為你收集整理的Intel TBB简介及在Windows7 VS2013上源码的编译过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Caffe源码中syncedmem文件分
- 下一篇: Caffe源码中blob文件分析