From c0b240aa433939081730f31563f38fc2f410847d Mon Sep 17 00:00:00 2001 From: luotao1 Date: Tue, 5 Mar 2019 15:33:53 +0800 Subject: [PATCH] try to fix distributed unit-test test=develop --- paddle/fluid/framework/operator.cc | 19 ++++++++++--------- paddle/fluid/framework/scope.cc | 4 ++++ paddle/fluid/framework/scope.h | 4 ++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index ef0a4779d..3959728a2 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -919,15 +919,16 @@ std::vector* OperatorWithKernel::GetKernelConfig( void OperatorWithKernel::RunImpl(const Scope& scope, const platform::Place& place) const { const Scope* cur_scope = &scope; - if (!runtime_ctx_ || pre_scope_ != cur_scope || - scope.FindVar(details::kLocalExecScopeName)) { - // RuntimeContext is used to relate input/output names of Operator with - // the corresponding variables in Scope. - // In a same Scope, since the input/output names of Operator do not change - // in the execution, RuntimeContext could be created only at the first - // iteration of the execution to save the elapsed time. - // Note that the Scope should not be the local scope, since local scope - // would be cleaned regularly. + // RuntimeContext is used to relate input/output names of Operator with + // the corresponding variables in Scope. + // In a same Scope, since the input/output names of Operator do not change + // in the execution, RuntimeContext could be created only at the first + // iteration of the execution to save the elapsed time. + // Note that the Scope should not be the local scope, since local scope + // would be cleaned regularly. + if (scope.FindVar(details::kLocalExecScopeName)) { + runtime_ctx_.reset(new RuntimeContext(Inputs(), Outputs(), scope)); + } else if (!runtime_ctx_ || pre_scope_ != cur_scope) { runtime_ctx_.reset(new RuntimeContext(Inputs(), Outputs(), scope)); pre_scope_ = cur_scope; } diff --git a/paddle/fluid/framework/scope.cc b/paddle/fluid/framework/scope.cc index 87f0f307d..e6de47717 100644 --- a/paddle/fluid/framework/scope.cc +++ b/paddle/fluid/framework/scope.cc @@ -107,6 +107,10 @@ const Scope* Scope::FindScope(const Variable* var) const { return FindScopeInternal(var); } +bool Scope::HasLocalVar(const std::string& name) const { + return vars_.find(name) != vars_.end(); +} + void Scope::DropKids() { SCOPE_KIDS_WRITER_LOCK for (Scope* s : kids_) delete s; diff --git a/paddle/fluid/framework/scope.h b/paddle/fluid/framework/scope.h index f0915d2ee..38d3b4d6c 100644 --- a/paddle/fluid/framework/scope.h +++ b/paddle/fluid/framework/scope.h @@ -75,6 +75,10 @@ class Scope { /// Caller doesn't own the returned Variable. Variable* FindLocalVar(const std::string& name) const; + /// Find whether a variable in the current scope. + /// Return false if cannot find. + bool HasLocalVar(const std::string& name) const; + const Scope* parent() const { return parent_; } /// Find the scope or an ancestor scope that contains the given variable. -- GitLab