Skip to content

Commit 4b7b17a

Browse files
committed
fix conflcts
Merge remote-tracking branch 'ups/develop' into multithreads
2 parents 64323b1 + 106ee9d commit 4b7b17a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ DEFINE_bool(
3434
namespace paddle {
3535
namespace framework {
3636

37-
Scope::~Scope() {
38-
DropKids();
39-
for (auto& kv : vars_) {
40-
VLOG(3) << "Destroy variable " << kv.first;
41-
delete kv.second;
42-
}
43-
}
37+
Scope::~Scope() { DropKids(); }
4438

4539
Scope& Scope::NewScope() const {
4640
std::unique_lock<std::mutex> lock(mutex_);
@@ -53,8 +47,9 @@ Variable* Scope::Var(const std::string& name) {
5347
std::unique_lock<std::mutex> lock(mutex_);
5448
auto* v = FindVarLocally(name);
5549
if (v != nullptr) return v;
50+
5651
v = new Variable();
57-
vars_[name] = v;
52+
vars_[name].reset(v);
5853
VLOG(3) << "Create variable " << name;
5954
v->name_ = &(vars_.find(name)->first);
6055
return v;
@@ -84,7 +79,7 @@ Variable* Scope::FindVarInternal(const std::string& name) const {
8479

8580
const Scope* Scope::FindScope(const Variable* var) const {
8681
for (auto& kv : vars_) {
87-
if (kv.second == var) {
82+
if (kv.second.get() == var) {
8883
return this;
8984
}
9085
}
@@ -123,7 +118,6 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
123118
std::set<std::string> var_set(var_names.begin(), var_names.end());
124119
for (auto it = vars_.begin(); it != vars_.end();) {
125120
if (var_set.find(it->first) != var_set.end()) {
126-
delete it->second;
127121
it = vars_.erase(it);
128122
} else {
129123
++it;
@@ -139,7 +133,7 @@ void Scope::Rename(const std::string& origin_name,
139133
auto new_it = vars_.find(new_name);
140134
PADDLE_ENFORCE(new_it == vars_.end(),
141135
"The variable with name %s is already in the scope", new_name);
142-
vars_[new_name] = origin_it->second;
136+
vars_[new_name].reset(origin_it->second.release());
143137
vars_.erase(origin_it);
144138
}
145139

@@ -151,7 +145,7 @@ std::string Scope::Rename(const std::string& origin_name) const {
151145

152146
Variable* Scope::FindVarLocally(const std::string& name) const {
153147
auto it = vars_.find(name);
154-
if (it != vars_.end()) return it->second;
148+
if (it != vars_.end()) return it->second.get();
155149
return nullptr;
156150
}
157151

paddle/fluid/framework/scope.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ class Scope {
4747
Scope& NewScope() const;
4848

4949
/// Create a variable with given name if it doesn't exist.
50+
/// Caller doesn't own the returned Variable.
5051
Variable* Var(const std::string& name);
5152

5253
/// Create a variable with a scope-unique name.
54+
/// Caller doesn't own the returned Variable.
5355
Variable* Var(std::string* name = nullptr);
5456

5557
void EraseVars(const std::vector<std::string>& var_names);
5658

5759
/// Find a variable in the scope or any of its ancestors. Returns
5860
/// nullptr if cannot find.
61+
/// Caller doesn't own the returned Variable.
5962
Variable* FindVar(const std::string& name) const;
6063

6164
const Scope* parent() const { return parent_; }
@@ -82,13 +85,17 @@ class Scope {
8285
// Call Scope::NewScope for a sub-scope.
8386
explicit Scope(Scope const* parent) : parent_(parent) {}
8487

85-
// Called by FindVar recursively
88+
// Called by FindVar recursively.
89+
// Caller doesn't own the returned Variable.
8690
Variable* FindVarInternal(const std::string& name) const;
8791

88-
// Called by FindVarInternal and Var
92+
// Called by FindVarInternal and Var.
93+
// Caller doesn't own the returned Variable.
8994
Variable* FindVarLocally(const std::string& name) const;
9095

91-
mutable std::unordered_map<std::string, Variable*> vars_;
96+
mutable std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
97+
98+
// Scope in `kids_` are owned by this class.
9299
mutable std::list<Scope*> kids_;
93100
Scope const* parent_{nullptr};
94101

0 commit comments

Comments
 (0)