@@ -43,56 +43,37 @@ Scope& Scope::NewScope() const {
43
43
}
44
44
45
45
Variable* Scope::Var (const std::string& name) {
46
- // acquire the lock when new var under this scope
47
46
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);
56
48
}
57
49
58
50
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 ());
60
53
if (name != nullptr ) {
61
- *name = var_name ;
54
+ *name = new_name ;
62
55
}
63
- return Var (var_name );
56
+ return VarInternal (new_name );
64
57
}
65
58
66
59
Variable* Scope::FindVar (const std::string& name) const {
67
- // acquire the lock when find var
68
60
std::unique_lock<std::mutex> lock (mutex_);
69
61
return FindVarInternal (name);
70
62
}
71
63
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
-
80
64
const Scope* Scope::FindScope (const Variable* var) const {
81
65
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);
88
67
}
68
+
89
69
void Scope::DropKids () {
90
70
std::unique_lock<std::mutex> lock (mutex_);
91
71
for (Scope* s : kids_) delete s;
92
72
kids_.clear ();
93
73
}
94
74
95
75
std::vector<std::string> Scope::LocalVarNames () const {
76
+ std::unique_lock<std::mutex> lock (mutex_);
96
77
std::vector<std::string> known_vars;
97
78
known_vars.reserve (this ->vars_ .size ());
98
79
for (auto & p : vars_) {
@@ -129,6 +110,38 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
129
110
void Scope::Rename (const std::string& origin_name,
130
111
const std::string& new_name) const {
131
112
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 {
132
145
auto origin_it = vars_.find (origin_name);
133
146
PADDLE_ENFORCE (origin_it != vars_.end (),
134
147
" Cannot find original variable with name %s" , origin_name);
@@ -139,10 +152,12 @@ void Scope::Rename(const std::string& origin_name,
139
152
vars_.erase (origin_it);
140
153
}
141
154
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);
146
161
}
147
162
148
163
Variable* Scope::FindVarLocally (const std::string& name) const {
0 commit comments