from:https://blog.csdn.net/u014114990/article/details/47747025
caffe.cpp??程序入口分析,
?
(1)main()函數中,輸入的train,test,device_query,time。 通過下面兩行進入程序。
????if (argc == 2) {
????return GetBrewFunction(caffe::string(argv[1]))();
?
(2)GetBrewFunction()函數定義如下,其返回BrewFunction函數指針。
static BrewFunction GetBrewFunction(const caffe::string& name) {
??// use map type to check name appears frequency?
??if (g_brew_map.count(name)) { //??判斷輸入的是不是g_brew_map中train,test,device_query,time中一個,
????return g_brew_map[name];????// 如果是的話,就調用相應的train(),test(),device_query(),time()
...
}
(3)g_brew_map實現過程,首先通過 typedef定義函數指針
typedef int (*BrewFunction)();
這個是用typedef定義函數指針方法。這個程序定義一個BrewFunction函數指針類型,
在caffe.cpp 中 BrewFunction 作為GetBrewFunction()函數的返回類型,可以是 train(),test(),device_query(),time() 這四個函數指針的其中一個。在train(),test(),中可以調用solver類的函數,從而進入到net,進入到每一層,運行整個caffe程序。
(4)g_brew_map定義
typedef std::map<caffe::string, BrewFunction> BrewMap;// 因為輸入參數可能為train,test,device_query,time,所以定義一個容器類型,
BrewMap g_brew_map;
(5) g_brew_map 初始化
#define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
?public: /* NOLINT */ \
??__Registerer_##func() { \
????g_brew_map[#func] = &func; \
??} \
}; \
__Registerer_##func g_registerer_##func; \
}
這個作用和#define RegisterBrewFunction(func) g_brew_map[#func]=&func;??這個宏定義功能類似,其中,func可以為:train,test,device_query,time。
?
??綜上, caffe中定義了train(),test(),device_query(),time()四種方式。??如果需要,咱們可以增加其他的方式,然后通過RegisterBrewFunction()?函數注冊一下即可。 ???
??
二、補充知識點
????
typedef int (*funcptr)(); 什么意思
typedef int (*funcptr)(); 什么意思
定義一個函數指針類型。
比如你有三個函數:
void hello(void) { printf("你好!"); }
void bye(void) { printf("再見!"); }
void ok(void) { printf("好的!"); }
typdef void (*funcptr)(void);
這樣就構造了一個通用的函數
你用的時候可以這樣:
void speak(int id)
{
funcptr words[3] = {&hello, &bye, &ok};
funcptr fun = words[id];
(*fun)();
}
這樣的話,如果speak(0)就會顯示“你好!”
speak(1)就會顯示“再見!”
speak(2)就會顯示“好的!”
用于處理參數和返回值的形式都一樣,但是功能不確定的一組函數,可以使用函數指針。
比如算術運算符,加、減、乘、除,都可以用typedef int (*calc)(int,int)代表,等等
三、 #define 宏定義
??
1.簡單的define定義
#define?MAXTIME 1000
一個簡單的MAXTIME就定義好了,它代表1000,如果在程序里面寫
if(i<MAXTIME){.........}
編譯器在處理這個代碼之前會對MAXTIME進行處理替換為1000。
這樣的定義看起來類似于普通的常量定義CONST,但也有著不同,因為define的定義更像是簡單的文本替換,而不是作為一個量來使用,這個問題在下面反映的尤為突出。
2.define的“函數定義”
define可以像函數那樣接受一些參數,如下
#define?max(x,y) (x)>(y)?(x):(y);
這個定義就將返回兩個數中較大的那個,看到了嗎?因為這個“函數”沒有類型檢查,就好像一個函數模板似的,當然,它絕對沒有模板那么安全就是了。可以作為一個簡單的模板來使用而已。
但是這樣做的話存在隱患,例子如下:
#define?Add(a,b) a+b;
在一般使用的時候是沒有問題的,但是如果遇到如:c * Add(a,b) * d的時候就會出現問題,代數式的本意是a+b然后去和c,d相乘,但是因為使用了define(它只是一個簡單的替換),所以式子實際上變成了
c*a + b*d
另外舉一個例子:
#define?pin (int*);
pin a,b;
本意是a和b都是int型指針,但是實際上變成int* a,b;
a是int型指針,而b是int型變量。
這是應該使用typedef來代替define,這樣a和b就都是int型指針了。
所以我們在定義的時候,養成一個良好的習慣,建議所有的層次都要加括號。
3.宏的單行定義
#define?A(x) T_##x
#define?B(x) #@x
#define?C(x) #x
我們假設:x=1,則有:
A(1)------〉T_1
B(1)------〉'1'
C(1)------〉"1"
(這里參考了 hustli的文章)
3.define的多行定義
define可以替代多行的代碼,例如MFC中的宏定義(非常的經典,雖然讓人看了惡心)
#define?MACRO(arg1, arg2) do { /
/* declarations */?/
stmt1; /
stmt2; /
/* ... */?/
}?while(0)?/* (no trailing ; ) */
關鍵是要在每一個換行的時候加上一個"/"
摘抄自http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml?修補了幾個bug
4.在大規模的開發過程中,特別是跨平臺和系統的軟件里,define最重要的功能是條件編譯。
就是:
#ifdef?WINDOWS
......
......
#endif
#ifdef?LINUX
......
......
#endif
可以在編譯的時候通過#define設置編譯環境
5.如何定義宏、取消宏
//定義宏
#define?[MacroName] [MacroValue]
//取消宏
#undef?[MacroName]
//普通宏
#define?PI (3.1415926)
帶參數的宏
#define?max(a,b) ((a)>(b)? (a),(b))
關鍵是十分容易產生錯誤,包括機器和人理解上的差異等等。
6.條件編譯
#ifdef?XXX…(#else) …?#endif
例如
#ifdef?DV22_AUX_INPUT
#define?AUX_MODE 3?
#else
#define?AUY_MODE 3
#endif
#ifndef?XXX … (#else) …?#endif
7.頭文件(.h)可以被頭文件或C文件包含;
重復包含(重復定義)
由于頭文件包含可以嵌套,那么C文件就有可能包含多次同一個頭文件,就可能出現重復定義的問題的。
通過條件編譯開關來避免重復包含(重復定義)
例如
#ifndef?__headerfileXXX__
#define?__headerfileXXX__
…
//文件內容
…
#endif
以上只是我從網絡上搜集了一些關于define的一些用法,可能還不全面,而且#define的使用本來也存在這爭議,如果你對#define的用法也很有興趣,可以來參加我們的討論
四。宏定義是在c中常用的,
????????在c++中,定義常變量,應該用const定義, 定義常用函數,應該用 inline 定義,這兩種方法更好。?
?inline函數和用macro定義的函數區別?
macro定義?
只是很初級的一種代換,實現的功能很單一?
而且安全性很差,比如類型錯誤、括號漏寫?
都會造成很大的錯誤,?
而且錯誤不容易被發現,隱患很大?
inline函數?
內聯函數要比前者好很多?
功能也要全面很多!?
最主要的是???????
內聯函數能夠進行安全檢查(比如參數類型 ??等)?
如果在能夠使用兩著的情況之下?
推薦使用 ??內聯???????
不過有兩點要注意:?
1 ????內聯 ??是以代碼膨脹為代價的,???
??????不是所有的函數都適合用 ??內聯 ??方式?
??????要考慮函數的實際情況???
2 ????macro定義 ??也不是說一無是處了?
??????在合適的時候使用 ??也許會有意想不到的效果?
const與#define最大的差別在于:前者在堆棧分配了空間,而后者只是把具體數值直接傳遞到目標變量罷了。或者說,const的常量是一個Run-Time的概念,他在程序中確確實實的存在可以被調用、傳遞。而#define常量則是一個Compile-Time概念,它的生命周期止于編譯期:在實際程序中他只是一個常數、一個命令中的參數,沒有實際的存在。
const常量存在于程序的數據段.
#define常量存在于程序的代碼段。
2優缺點:
至于兩者的優缺點,要看具體的情況了。一般的常數應用,我個人認為#define是一個更好的選擇:
i.從run-time的角度來看,他在空間上和時間上都有很好優勢。
ii.從compile-time的角度來看,類似m=t*10的代碼不會被編譯器優化,t*10的操作需要在run-time執行。而#define的常量會被合并。
但是:如果你需要粗魯的修改常數的值,那就的使用const了,因為后者在程序中沒有實際的存在.
另外在頭文件中使用 #define 可以避免頭文件重復包含的問題,這個功能,是const無法取代的。
1-Google Flag
1-1 Define Flags
1-2 Accessing Flag
1-3 Set up Flags
2-Caffe cpp
2-1 Part 1
2-2 Part 2
BrewFunction
1-Google Flag
Document is?How To Use Google Commandline Flags, where All materials discussed below come from.
Google Flags(gflags) is created to deal with commandline flags.
1-1.?Define Flags.
<code class="language-c hljs ?has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <gflags/gflags.h></span>
? ?DEFINE_bool(big_menu, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Include 'advanced' options in the menu listing"</span>);
? ?DEFINE_string(languages, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"english,french,german"</span>,
? ? ? ? ? ? ? ? ?<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"comma-separated list of languages to offer in the 'lang' menu"</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
Explain:?
Before defining gflags, include it at the top of file.
DEFINE_bool and DEFINE_string are flag types. Here are different types of flags.
<code class="language-c hljs ?has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">DEFINE_bool: boolean
DEFINE_int32: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">32</span>-bit integer
DEFINE_int64: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span>-bit integer
DEFINE_uint64: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span>-bit integer
DEFINE_double: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>
DEFINE_string: C++ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
DEFINE_xxx takes three arguments:?
1. the name of the flag?
2. its default value?
3. ′help′ string that describe its use.
1-2. Accessing Flag
All defined flags are available with the prefix FLAGS_. In the example above, two variables, FLAGS_big_menu, and FLAGS_languages can be used.
1-3. Set up Flags
<code class="language-c++ hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">google:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:ParseCommandLineFlags</span>(&argc, &argv, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2-Caffe. cpp
2-1. Part 1
<code class="language-c++ hljs ruleslanguage has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-array" style="box-sizing: border-box;">#include </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"caffe/caffe.hpp"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
#include <gflags/gflags.h> is included in caffe.hpp/common.hpp
<code class="language-c++ hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-function" style="box-sizing: border-box;">DEFINE_int32(gpu, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Run in GPU mode on given device ID."</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">DEFINE_string(solver, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"The solver definition protocol buffer text file."</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">DEFINE_string(model, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"The model definition protocol buffer text file.."</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">DEFINE_string(snapshot, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Optional; the snapshot solver state to resume training."</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">DEFINE_string(weights, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Optional; the pretrained weights to initialize finetuning. "</span>
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Cannot be set simultaneously with snapshot."</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">DEFINE_int32(iterations, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>,
? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"The number of iterations to run."</span>)</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
Initial all flags. (refer to?How to define flags?)
2-2. Part 2
main function:
<code class="language-c++ hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">int main(int argc, char** argv) {
? <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-constant" style="box-sizing: border-box;">Print</span> output to stderr (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> still logging).
? <span class="hljs-constant" style="box-sizing: border-box;">FLAGS_alsologtostderr</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
? <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-constant" style="box-sizing: border-box;">Usage</span> message.
? <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">gflags:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:SetUsageMessage</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"command line brew\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"usage: caffe <command> <args>\n\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"commands:\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ?train ? ? ? ? ? train or finetune a model\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ?test ? ? ? ? ? ?score a model\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ?device_query ? ?show GPU diagnostic information\n"</span>
? ? ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ?time ? ? ? ? ? ?benchmark model execution time"</span>);
? <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-constant" style="box-sizing: border-box;">Run</span> tool <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> show usage.
? <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">caffe:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:GlobalInit</span>(&argc, &argv);
? <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (argc == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) {
? ? <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-constant" style="box-sizing: border-box;">GetBrewFunction</span>(<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">caffe:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:string</span>(argv[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]))();
? } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
? ? <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">gflags:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:ShowUsageWithFlagsRestrict</span>(argv[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>], <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tools/caffe"</span>);
? }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
caffe.cpp is able to do four different task: train, test, device_query and time.
train: train or finetune a model\n”?
test: score a model\n”?
device_query: show GPU diagnostic information\n”?
time: benchmark model execution time”);
caffe::GlobalInit(&argc, &argv); includes gflags parse sentence:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">google:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:ParseCommandLineFlags</span>(&argc, &argv, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
So far, the corresponding flags are set up from command line.
It will call different functions by GetBrewFunction(). Here are four main functions: train(), test(), device_query() and time().
BrewFunction
<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> (*BrewFunction)();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><caffe::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>, BrewFunction></span> BrewMap;
BrewMap g_brew_map;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
The first line is to define BrewFunction as a function pointer with inputting no argument and returning int.
The second typedef is to create BrewMap as a map to store the flag and its corresponding function.
But How to initial BrewMap ??
see this block:
<code class="language-c++ hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>define RegisterBrewFunction(func) <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span>namespace <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span>class __Registerer_<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>func <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span> public: /* NOLINT */ <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span> ?__Registerer_<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>func() <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span> ? ?g_brew_map<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">[</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>func<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">]</span> = <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">&</span>func; <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span> ?<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span>; <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span>__Registerer_<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>func g_registerer_<span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span>func; <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\
</span><span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
It uses DEFINE to create a class, then use its public constructor function to initial BrewMap.
#: the parameter is replaced by a string literal
????Eg. #define str(x) #x?
????cout << str(test);
????it would be cout<<’test’;
##: concatenates two arguments.
It is very important to make constructor function public, otherwise we can call it.
Actually, we make a observation that?RegisterBrewFunction?comes after each main function (eg, train, test). The Class is initialized with running its constructor before going into main function.
After running GetBrewFunction(), it returns &func. In main function, it executes func().
typedef int (*funcptr)(); 什么意思
---------------------?
作者:deep_learninger?
來源:CSDN?
原文:https://blog.csdn.net/u014114990/article/details/47747025?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!