Skip to content

Commit b8d315f

Browse files
committed
make scope thread safe
1 parent bfd4268 commit b8d315f

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,56 +43,37 @@ Scope& Scope::NewScope() const {
4343
}
4444

4545
Variable* Scope::Var(const std::string& name) {
46-
// acquire the lock when new var under this scope
4746
std::unique_lock<std::mutex> lock(mutex_);
48-
auto* v = FindVarLocally(name);
49-
if (v != nullptr) return v;
50-
51-
v = new Variable();
52-
vars_[name].reset(v);
53-
VLOG(3) << "Create variable " << name;
54-
v->name_ = &(vars_.find(name)->first);
55-
return v;
47+
return VarInternal(name);
5648
}
5749

5850
Variable* Scope::Var(std::string* name) {
59-
auto var_name = string::Sprintf("%p.%d", this, vars_.size());
51+
std::unique_lock<std::mutex> lock(mutex_);
52+
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
6053
if (name != nullptr) {
61-
*name = var_name;
54+
*name = new_name;
6255
}
63-
return Var(var_name);
56+
return VarInternal(new_name);
6457
}
6558

6659
Variable* Scope::FindVar(const std::string& name) const {
67-
// acquire the lock when find var
6860
std::unique_lock<std::mutex> lock(mutex_);
6961
return FindVarInternal(name);
7062
}
7163

72-
Variable* Scope::FindVarInternal(const std::string& name) const {
73-
auto var = FindVarLocally(name);
74-
if (var != nullptr) {
75-
return var;
76-
}
77-
return (parent_ == nullptr) ? nullptr : parent_->FindVarInternal(name);
78-
}
79-
8064
const Scope* Scope::FindScope(const Variable* var) const {
8165
std::unique_lock<std::mutex> lock(mutex_);
82-
for (auto& kv : vars_) {
83-
if (kv.second.get() == var) {
84-
return this;
85-
}
86-
}
87-
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
66+
return FindScopeInternal(var);
8867
}
68+
8969
void Scope::DropKids() {
9070
std::unique_lock<std::mutex> lock(mutex_);
9171
for (Scope* s : kids_) delete s;
9272
kids_.clear();
9373
}
9474

9575
std::vector<std::string> Scope::LocalVarNames() const {
76+
std::unique_lock<std::mutex> lock(mutex_);
9677
std::vector<std::string> known_vars;
9778
known_vars.reserve(this->vars_.size());
9879
for (auto& p : vars_) {
@@ -129,6 +110,38 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
129110
void Scope::Rename(const std::string& origin_name,
130111
const std::string& new_name) const {
131112
std::unique_lock<std::mutex> lock(mutex_);
113+
RenameInternal(origin_name, new_name);
114+
}
115+
116+
std::string Scope::Rename(const std::string& origin_name) const {
117+
std::unique_lock<std::mutex> lock(mutex_);
118+
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
119+
RenameInternal(origin_name, new_name);
120+
return new_name;
121+
}
122+
123+
Variable* Scope::VarInternal(const std::string& name) {
124+
auto* v = FindVarLocally(name);
125+
if (v != nullptr) return v;
126+
127+
v = new Variable();
128+
vars_[name].reset(v);
129+
VLOG(3) << "Create variable " << name;
130+
v->name_ = &(vars_.find(name)->first);
131+
return v;
132+
}
133+
134+
const Scope* Scope::FindScopeInternal(const Variable* var) const {
135+
for (auto& kv : vars_) {
136+
if (kv.second.get() == var) {
137+
return this;
138+
}
139+
}
140+
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
141+
}
142+
143+
void Scope::RenameInternal(const std::string& origin_name,
144+
const std::string& new_name) const {
132145
auto origin_it = vars_.find(origin_name);
133146
PADDLE_ENFORCE(origin_it != vars_.end(),
134147
"Cannot find original variable with name %s", origin_name);
@@ -139,10 +152,12 @@ void Scope::Rename(const std::string& origin_name,
139152
vars_.erase(origin_it);
140153
}
141154

142-
std::string Scope::Rename(const std::string& origin_name) const {
143-
auto var_name = string::Sprintf("%p.%d", this, vars_.size());
144-
Rename(origin_name, var_name);
145-
return var_name;
155+
Variable* Scope::FindVarInternal(const std::string& name) const {
156+
auto var = FindVarLocally(name);
157+
if (var != nullptr) {
158+
return var;
159+
}
160+
return (parent_ == nullptr) ? nullptr : parent_->FindVar(name);
146161
}
147162

148163
Variable* Scope::FindVarLocally(const std::string& name) const {

paddle/fluid/framework/scope.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,20 @@ class Scope {
8585
// Call Scope::NewScope for a sub-scope.
8686
explicit Scope(Scope const* parent) : parent_(parent) {}
8787

88+
// Called by Var.
89+
Variable* VarInternal(const std::string& name);
90+
91+
// Called by FindScope.
92+
const Scope* FindScopeInternal(const Variable* var) const;
93+
94+
// Called by Rename.
95+
void RenameInternal(const std::string& origin_name,
96+
const std::string& new_name) const;
97+
8898
// Called by FindVar recursively.
89-
// Caller doesn't own the returned Variable.
9099
Variable* FindVarInternal(const std::string& name) const;
91100

92101
// Called by FindVarInternal and Var.
93-
// Caller doesn't own the returned Variable.
94102
Variable* FindVarLocally(const std::string& name) const;
95103

96104
mutable std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;

0 commit comments

Comments
 (0)