-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add force link macro #1054
Add force link macro #1054
Conversation
reyoung
commented
Jan 3, 2017
•
edited
Loading
edited
- It could help us get rid of --whole-archive.
#include <paddle/utils/ForceLink.h> | ||
#include "test_ClassRegistrarLib.h" | ||
// Enable link test_ClassRegistrarLib.cpp | ||
PADDLE_ENABLE_FORCE_LINK_FILE(test_registrar); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of use --whole-archive
, we could use PADDLE_ENABLE_FORCE_LINK_FILE
macro to force link XXXLayer.cpp in TrainerMain.
It also could help us for predict SDK. We can generate PADDLE_ENABLE_FORCE_LINK_FILE
for XXXPredict, which only contains the Layers that Predict binary used.
C/C++编译器在静态连接的时候,会初始化使用到符号的文件的所有静态变量。所以,我们在需要初始化的CPP文件里面定义一个没啥意义的函数,在使用这个文件的地方,使用这个没啥意义的函数,就能够初始化这个CPP文件里面的所有静态变量了。 https://github.com/PaddlePaddle/Paddle/pull/1054/files#diff-6219ecc16db60fe517ce956bb9083561R17 |
* It could help us get rid of --whole-archieve. * Add comments
a6677d8
to
7848214
Compare
@gangliao @hedaoyuan Remove ' --whole-archive', instead, we add definitions to force linking files here |
|
* this file used for static linking libraries, to enable all InitFunction in | ||
* Paddle GServer. | ||
*/ | ||
PADDLE_ENABLE_FORCE_LINK_FILE(base_data_providers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写个函数(可以赋给一个静态的函数指针变量),里面调用一下每个Layer的构造函数因该就可以了,没必每个Layer文件都新增一个符号。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个新增一个符号要比直接引用那个Layer的构造函数耦合度要松一些。
1、这个新增的符号命名使用配对的宏去做的,所以不需要include头文件。
2、这个新增的符号是根据『文件』这个级别走的。也就是link某个『文件』的全部static变量,而不是某个『Layer』。这样做的好处是:
- Layer可以改名字。新增Layer而不新增文件,不需要修改这个
ForceLinkFiles.cpp
- 一组Layer的REGISTER部分可以放到一个文件里,这样也不需要写很多
PADDLE_ENABLE_FORCE_LINK_FILE
了- 譬如 写一个
cnn_register.cpp
,里面全是图像相关的Layer的REGISTER,那我们写一个PADDLE_ENABLE_FORCE_LINK_FILE(cnn_register)就好了。
- 譬如 写一个
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个新增一个符号要比直接引用那个Layer的构造函数耦合度要松一些
这个都是一样,都是在另外一个.o中引用一下某个Layer.o中的符号
Layer可以改名字。新增Layer而不新增文件,不需要修改这个ForceLinkFiles.cpp
Layer的名字一般不会修改,一般Layer都是单独写一个文件的,即使要在同一个文件中新增一个Layer,不修改ForceLinkFiles.cpp
也没什么问题(同样对于一个全是图像Layer的文件,只引用其中一个Layer的构造函数并没什么问题)。
本质上确实不需要增加额外的符号来实现--whole-archive
;对外,提供.so 不存在--whole-archive
问题;提供.a给别人用,是否需要--whole-archive
以及不同编译环境在该怎么实现--whole-archive这个是自己决定的。即使现在这种编译方式,在ar的时候去掉-s好像就不需要--whole-archive
了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1、这么搞不用include另一个头文件了呀。。否则,那个ForceLinkFiles.cpp
前面会有一堆include的。
2、况且,有的东西可以没有头文件的。比如PyDataProvider2.cpp
本质上确实不需要增加额外的符号来实现--whole-archive
增加这些符号只是为了写起来方便。确实可以不增加符号。但增加了这些符号也没啥坏处。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--whole-archive 有什么不好?为什么需要在源码级别引入一些symbols来实现force linking呢?
whole-archive有以下几点问题:
|
有道理,我改一下。。 |
Closed. 线下和 @hedaoyuan 讨论。这块的工作可以分为一下几步进行。
|