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>
L
liuruilong 已提交
18 19 20 21 22

//#ifdef  PADDLE_MOBILE_OCL
#include "framework/cl/cl_scope.h"
//#endif

W
wangliu 已提交
23
#include <unordered_map>
朔-望's avatar
朔-望 已提交
24 25 26
#include "variable.h"

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

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

//#ifdef  PADDLE_MOBILE_OCL
    delete cl_scope_;
//#endif

46
  }
朔-望's avatar
朔-望 已提交
47

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

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

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

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

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

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

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

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

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

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

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

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

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

L
liuruilong 已提交
85 86 87 88
  CLScope *GetCLScpoe() {
    return cl_scope_;
  }

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

93 94 95
  mutable std::unordered_map<std::string, Variable *> vars_;
  mutable std::list<Scope *> kids_;
  Scope const *parent_{nullptr};
L
liuruilong 已提交
96
//#ifdef  PADDLE_MOBILE_CL
L
liuruilong 已提交
97 98
  CLScope *cl_scope_ = new CLScope();
//#endif
朔-望's avatar
朔-望 已提交
99
};
朔-望's avatar
朔-望 已提交
100 101
}  // namespace framework
}  // namespace paddle_mobile