caffe caffe.cpp 程序入口分析

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?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458121.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458121.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458121.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php文件加密

1.在線加密 網址&#xff1a;http://www.phpjm.net/encode.html 本人測試過還可以&#xff0c;就是純加密&#xff0c;沒有解密。 轉載于:https://www.cnblogs.com/wuheng1991/p/5332617.html

樹莓派3 編譯驅動

分為本地編譯和交叉編譯&#xff0c;主要是Makefile的寫法&#xff1a; 本地編譯&#xff1a; obj-m : bcm2835-i2s.o KDIR : /lib/modules/$(shell uname -r)/build PWD : $(shell pwd) all:make -C $(KDIR) M$(PWD) modules clean:rm *.o *.ko *.mod.c modules.order Module.…

caffe common 程序分析 類中定義類

caffe中 有 common.hpp 和common.cpp // The main singleton of Caffe class and encapsulates the boost and CUDA random number // generation function, providing a unified interface. caffe的singleton 類&#xff0c; 封裝boost和cuda等操作。 提供一個統一的接口&am…

相機標定究竟在標定什么?

https://mp.weixin.qq.com/s/sWpVgwXmPvIEbObXvo1HRg

SpringMVC+Shiro權限管理

SpringMVCShiro權限管理 什么是權限呢&#xff1f;舉個簡單的例子&#xff1a; 我有一個論壇&#xff0c;注冊的用戶分為normal用戶&#xff0c;manager用戶。對論壇的帖子的操作有這些&#xff1a;添加&#xff0c;刪除&#xff0c;更新&#xff0c;查看&#xff0c;回復我們規…

Caffe源碼解析1:Blob

from:https://www.cnblogs.com/louyihang-loves-baiyan/p/5149628.html 轉載請注明出處&#xff0c;樓燚(y)航的blog&#xff0c;http://www.cnblogs.com/louyihang-loves-baiyan/ 首先看到的是Blob這個類&#xff0c;Blob是作為Caffe中數據流通的一個基本類&#xff0c;網絡…

學后感

今天上了構建之法&#xff0c;我加深了對軟件工程的了解&#xff0c;也明白了單元測試和回歸測試對軟件開發的重要性&#xff0c;然而在軟件開發的過程中&#xff0c; 一個團隊是需要一定的流程來管理開發活動&#xff0c;每個工程師在軟件生命周期所做的工作也應該有一個流程&…

Caffe源碼解析2:SycedMem

from:https://www.cnblogs.com/louyihang-loves-baiyan/p/5150554.html 轉載請注明出處&#xff0c;樓燚(y)航的blog&#xff0c;http://www.cnblogs.com/louyihang loves baiyan/ 看到SyncedMem就知道&#xff0c;這是在做內存同步的操作。這類個類的代碼比較少&#xff0c;…

REST學習

RPC架構與REST架構 RPC&#xff1a;RPC將服務器看作一些列動作的集合(需要做某件事) REST&#xff1a;將服務器看作分布式對象集合&#xff0c;客戶端通過調用這些對象上的方法來執行特定的任務&#xff0c;組件交互的可伸縮性、接口的通用性、組件的獨立部署、以及用來減少交互…

HI3559A和AI深度學習框架caffe

from:http://blog.sina.com.cn/s/blog_156e567660102ygdf.html 1、HI3559A支持深度學習框架caffe。其中的NNIE神經網絡加速單元是主要的屬性。 2、caffe是一種快速深度學習框架和TensorFlow一樣是一組標準深度學習開源框架。 3、對應想嘗試AI深度學習的朋友可以按照網上的流…

UValive4195 Heroes of Money and Magic

斜率優化 想罵人了&#xff0c;馬格吉最后調了半小時 TMD造數據的人是SB吧&#xff1f; 我寫 while(scanf("%d%d",&n,&m)!EOF&&n) 然后就TMD無限WA...WA...WA... 尼瑪 改成while(scanf("%d%d",&n,&m),n) 就過了&#xff0c;就過了…

Google Protocol Buffer 的使用和原理

from: https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html 簡介 什么是 Google Protocol Buffer&#xff1f; 假如您在網上搜索&#xff0c;應該會得到類似這樣的文字介紹&#xff1a; Google Protocol Buffer( 簡稱 Protobuf) 是 Google 公司內部的混合語言…

Electron

跨平臺桌面app開發 Appjs hex nwjs electron 官網&#xff1a;http://electron.atom.io/ 中文文檔&#xff1a;https://github.com/atom/electron/tree/master/docs-translations/zh-CN zcbenz&#xff1a; https://github.com/zcbenz https://github.com/atom/electron simple…

WCF技術剖析之十八:消息契約(Message Contract)和基于消息契約的序列化

在本篇文章中&#xff0c;我們將討論WCF四大契約&#xff08;服務契約、數據契約、消息契約和錯誤契約&#xff09;之一的消息契約&#xff08;Message Contract&#xff09;。服務契約關注于對服務操作的描述&#xff0c;數據契約關注于對于數據結構和格式的描述&#xff0c;而…

【深度學習數據集】常用公開圖片數據集下載

1.MNIST MNIST是一個手寫數字數據庫&#xff0c;它有60000個訓練樣本集和10000個測試樣本集&#xff0c;每個樣本圖像的寬高為28*28。此數據集是以二進制存儲的&#xff0c;不能直接以圖像格式查看&#xff0c;不過很容易找到將其轉換成圖像格式的工具。 最早的深度卷積網絡Le…

常用的幾種卷積神經網絡介紹

常用的幾種卷積神經網絡介紹 標簽&#xff08;空格分隔&#xff09;&#xff1a; 深度學習 這是一篇基礎理論的博客&#xff0c;基本手法是抄、刪、改、查&#xff0c;畢竟介紹這幾個基礎網絡的博文也挺多的&#xff0c;就算是自己的一個筆記吧&#xff0c;以后忘了多看看。主…

計算客 (人人都有極客精神)爆力

人人公司是一家極為鼓舞極客精神的公司&#xff0c;當有重要的項目須要上線但又時間太緊。甚至須要當天上線的時候。往往會掛起海盜旗開啟電子日期顯示。讓大家能夠在對時間有更明白的感知的情況下&#xff0c;同心協力搞定重要的項目。海盜旗下方的電子屏顯示的日期形式為 YYY…

深度學習案例

1. neural-style&#xff1a;利用卷積神經網絡將一幅圖像的內容與另一幅圖像的風格相結合 https://github.com/jcjohnson/neural-style 2.Nerual Doodles&#xff1a;把 2 位的 Doodle 轉成精良的藝術品 https://github.com/alexjc/neural-doodle 3. srez&#xff1a;通過深度…

深度學習圖像標注工具匯總

對于監督學習算法而言&#xff0c;數據決定了任務的上限&#xff0c;而算法只是在不斷逼近這個上限。世界上最遙遠的距離就是我們用同一個模型&#xff0c;但是卻有不同的任務。但是數據標注是個耗時耗力的工作&#xff0c;下面介紹幾個圖像標注工具&#xff1a; Labelme Labe…

UIBarbuttonItem

APPDelegate: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; //創建主界面&#xff0c;導航欄的第一個頁面 FirstViewContr…