caffe caffe.cpp 程序入口分析

24 篇文章 0 订阅
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

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)(); 什么意思

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值