Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
b8d315fb
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b8d315fb
编写于
6月 07, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make scope thread safe
上级
bfd42683
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
56 addition
and
33 deletion
+56
-33
paddle/fluid/framework/scope.cc
paddle/fluid/framework/scope.cc
+46
-31
paddle/fluid/framework/scope.h
paddle/fluid/framework/scope.h
+10
-2
未找到文件。
paddle/fluid/framework/scope.cc
浏览文件 @
b8d315fb
...
...
@@ -43,49 +43,29 @@ Scope& Scope::NewScope() const {
}
Variable
*
Scope
::
Var
(
const
std
::
string
&
name
)
{
// acquire the lock when new var under this scope
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
auto
*
v
=
FindVarLocally
(
name
);
if
(
v
!=
nullptr
)
return
v
;
v
=
new
Variable
();
vars_
[
name
].
reset
(
v
);
VLOG
(
3
)
<<
"Create variable "
<<
name
;
v
->
name_
=
&
(
vars_
.
find
(
name
)
->
first
);
return
v
;
return
VarInternal
(
name
);
}
Variable
*
Scope
::
Var
(
std
::
string
*
name
)
{
auto
var_name
=
string
::
Sprintf
(
"%p.%d"
,
this
,
vars_
.
size
());
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
auto
new_name
=
string
::
Sprintf
(
"%p.%d"
,
this
,
vars_
.
size
());
if
(
name
!=
nullptr
)
{
*
name
=
var
_name
;
*
name
=
new
_name
;
}
return
Var
(
var
_name
);
return
Var
Internal
(
new
_name
);
}
Variable
*
Scope
::
FindVar
(
const
std
::
string
&
name
)
const
{
// acquire the lock when find var
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
return
FindVarInternal
(
name
);
}
Variable
*
Scope
::
FindVarInternal
(
const
std
::
string
&
name
)
const
{
auto
var
=
FindVarLocally
(
name
);
if
(
var
!=
nullptr
)
{
return
var
;
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindVarInternal
(
name
);
}
const
Scope
*
Scope
::
FindScope
(
const
Variable
*
var
)
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
for
(
auto
&
kv
:
vars_
)
{
if
(
kv
.
second
.
get
()
==
var
)
{
return
this
;
}
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindScope
(
var
);
return
FindScopeInternal
(
var
);
}
void
Scope
::
DropKids
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
for
(
Scope
*
s
:
kids_
)
delete
s
;
...
...
@@ -93,6 +73,7 @@ void Scope::DropKids() {
}
std
::
vector
<
std
::
string
>
Scope
::
LocalVarNames
()
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
std
::
vector
<
std
::
string
>
known_vars
;
known_vars
.
reserve
(
this
->
vars_
.
size
());
for
(
auto
&
p
:
vars_
)
{
...
...
@@ -129,6 +110,38 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
void
Scope
::
Rename
(
const
std
::
string
&
origin_name
,
const
std
::
string
&
new_name
)
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
RenameInternal
(
origin_name
,
new_name
);
}
std
::
string
Scope
::
Rename
(
const
std
::
string
&
origin_name
)
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
auto
new_name
=
string
::
Sprintf
(
"%p.%d"
,
this
,
vars_
.
size
());
RenameInternal
(
origin_name
,
new_name
);
return
new_name
;
}
Variable
*
Scope
::
VarInternal
(
const
std
::
string
&
name
)
{
auto
*
v
=
FindVarLocally
(
name
);
if
(
v
!=
nullptr
)
return
v
;
v
=
new
Variable
();
vars_
[
name
].
reset
(
v
);
VLOG
(
3
)
<<
"Create variable "
<<
name
;
v
->
name_
=
&
(
vars_
.
find
(
name
)
->
first
);
return
v
;
}
const
Scope
*
Scope
::
FindScopeInternal
(
const
Variable
*
var
)
const
{
for
(
auto
&
kv
:
vars_
)
{
if
(
kv
.
second
.
get
()
==
var
)
{
return
this
;
}
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindScope
(
var
);
}
void
Scope
::
RenameInternal
(
const
std
::
string
&
origin_name
,
const
std
::
string
&
new_name
)
const
{
auto
origin_it
=
vars_
.
find
(
origin_name
);
PADDLE_ENFORCE
(
origin_it
!=
vars_
.
end
(),
"Cannot find original variable with name %s"
,
origin_name
);
...
...
@@ -139,10 +152,12 @@ void Scope::Rename(const std::string& origin_name,
vars_
.
erase
(
origin_it
);
}
std
::
string
Scope
::
Rename
(
const
std
::
string
&
origin_name
)
const
{
auto
var_name
=
string
::
Sprintf
(
"%p.%d"
,
this
,
vars_
.
size
());
Rename
(
origin_name
,
var_name
);
return
var_name
;
Variable
*
Scope
::
FindVarInternal
(
const
std
::
string
&
name
)
const
{
auto
var
=
FindVarLocally
(
name
);
if
(
var
!=
nullptr
)
{
return
var
;
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindVar
(
name
);
}
Variable
*
Scope
::
FindVarLocally
(
const
std
::
string
&
name
)
const
{
...
...
paddle/fluid/framework/scope.h
浏览文件 @
b8d315fb
...
...
@@ -85,12 +85,20 @@ class Scope {
// Call Scope::NewScope for a sub-scope.
explicit
Scope
(
Scope
const
*
parent
)
:
parent_
(
parent
)
{}
// Called by Var.
Variable
*
VarInternal
(
const
std
::
string
&
name
);
// Called by FindScope.
const
Scope
*
FindScopeInternal
(
const
Variable
*
var
)
const
;
// Called by Rename.
void
RenameInternal
(
const
std
::
string
&
origin_name
,
const
std
::
string
&
new_name
)
const
;
// Called by FindVar recursively.
// Caller doesn't own the returned Variable.
Variable
*
FindVarInternal
(
const
std
::
string
&
name
)
const
;
// Called by FindVarInternal and Var.
// Caller doesn't own the returned Variable.
Variable
*
FindVarLocally
(
const
std
::
string
&
name
)
const
;
mutable
std
::
unordered_map
<
std
::
string
,
std
::
unique_ptr
<
Variable
>>
vars_
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录