scope.h 2.2 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

朔-望's avatar
朔-望 已提交
15 16
#pragma once

W
wangliu 已提交
17 18 19
#include <list>
#include <mutex>
#include <unordered_map>
朔-望's avatar
朔-望 已提交
20 21 22
#include "variable.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
23 24
namespace framework {
class Scope {
朔-望's avatar
朔-望 已提交
25
 public:
W
wangliu 已提交
26 27
  Scope() = default;
  ~Scope() = default;
朔-望's avatar
朔-望 已提交
28

29
  Scope &NewScope() const;
朔-望's avatar
朔-望 已提交
30

31 32
  /// Create a variable with given name if it doesn't exist.
  Variable *Var(const std::string &name);
朔-望's avatar
朔-望 已提交
33

34 35
  /// Create a variable with a scope-unique name.
  Variable *Var(std::string *name = nullptr);
朔-望's avatar
朔-望 已提交
36

37
  void EraseVars(const std::vector<std::string> &var_names);
朔-望's avatar
朔-望 已提交
38

39 40 41
  /// Find a variable in the scope or any of its ancestors.  Returns
  /// nullptr if cannot find.
  Variable *FindVar(const std::string &name) const;
朔-望's avatar
朔-望 已提交
42

43
  const Scope *parent() const { return parent_; }
朔-望's avatar
朔-望 已提交
44

45 46 47
  /// Find the scope or an ancestor scope that contains the given
  /// variable.
  const Scope *FindScope(const Variable *var) const;
朔-望's avatar
朔-望 已提交
48

49
  void DeleteScope(Scope *scope) const;
朔-望's avatar
朔-望 已提交
50

51 52
  /// Drop all kids scopes belonged to this scope.
  void DropKids();
朔-望's avatar
朔-望 已提交
53

54 55
  // enumerate all the variables current contains.
  std::vector<std::string> LocalVarNames() const;
朔-望's avatar
朔-望 已提交
56

57 58 59
  // Rename variable to a new name
  void Rename(const std::string &origin_name,
              const std::string &new_name) const;
朔-望's avatar
朔-望 已提交
60

61 62
  // Rename variable to a new name and return the new name
  std::string Rename(const std::string &origin_name) const;
朔-望's avatar
朔-望 已提交
63

64
  Variable *FindVarLocally(const std::string &name) const;
朔-望's avatar
朔-望 已提交
65

朔-望's avatar
朔-望 已提交
66
 private:
67 68
  // Call Scope::NewScope for a sub-scope.
  explicit Scope(Scope const *parent) : parent_(parent) {}
朔-望's avatar
朔-望 已提交
69

70 71 72
  mutable std::unordered_map<std::string, Variable *> vars_;
  mutable std::list<Scope *> kids_;
  Scope const *parent_{nullptr};
朔-望's avatar
朔-望 已提交
73

74
  mutable std::mutex mutex_;
朔-望's avatar
朔-望 已提交
75
};
朔-望's avatar
朔-望 已提交
76 77
}  // namespace framework
}  // namespace paddle_mobile