scope.h 2.6 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
#include <list>
18 19 20
#include <string>
#include <unordered_map>
#include <vector>
L
liuruilong 已提交
21

22
#ifdef PADDLE_MOBILE_CL
L
liuruilong 已提交
23
#include "framework/cl/cl_scope.h"
L
liuruilong 已提交
24
#endif
25
#include "framework/variable.h"
朔-望's avatar
朔-望 已提交
26 27

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
28
namespace framework {
29

朔-望's avatar
朔-望 已提交
30
class Scope {
朔-望's avatar
朔-望 已提交
31
 public:
W
wangliu 已提交
32
  Scope() = default;
33 34 35 36 37 38 39 40 41 42

  ~Scope() {
    for (auto &var : vars_) {
      delete var.second;
    }
    vars_.clear();
    for (auto kid : kids_) {
      delete kid;
    }
    kids_.clear();
L
liuruilong 已提交
43

L
liuruilong 已提交
44
#ifdef PADDLE_MOBILE_CL
L
liuruilong 已提交
45
    delete cl_scope_;
L
liuruilong 已提交
46
#endif
47
  }
朔-望's avatar
朔-望 已提交
48

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

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

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

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

59 60 61
  /// 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
朔-望 已提交
62

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

65 66 67
  /// Find the scope or an ancestor scope that contains the given
  /// variable.
  const Scope *FindScope(const Variable *var) const;
朔-望's avatar
朔-望 已提交
68

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

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

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

77 78 79
  // Rename variable to a new name
  void Rename(const std::string &origin_name,
              const std::string &new_name) const;
朔-望's avatar
朔-望 已提交
80

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

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

L
liuruilong 已提交
86
#ifdef PADDLE_MOBILE_CL
87
  CLScope *GetCLScpoe() { return cl_scope_; }
L
liuruilong 已提交
88
#endif
L
liuruilong 已提交
89

朔-望's avatar
朔-望 已提交
90
 private:
91 92
  // Call Scope::NewScope for a sub-scope.
  explicit Scope(Scope const *parent) : parent_(parent) {}
朔-望's avatar
朔-望 已提交
93

94 95 96
  mutable std::unordered_map<std::string, Variable *> vars_;
  mutable std::list<Scope *> kids_;
  Scope const *parent_{nullptr};
L
liuruilong 已提交
97 98

#ifdef PADDLE_MOBILE_CL
L
liuruilong 已提交
99
  CLScope *cl_scope_ = new CLScope();
L
liuruilong 已提交
100
#endif
朔-望's avatar
朔-望 已提交
101
};
朔-望's avatar
朔-望 已提交
102 103
}  // namespace framework
}  // namespace paddle_mobile