scope.cpp 3.7 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "framework/scope.h"
朔-望's avatar
朔-望 已提交
16

D
dolphin8 已提交
17
#include <algorithm>
朔-望's avatar
朔-望 已提交
18
#include <set>
朔-望's avatar
朔-望 已提交
19
#include <string>
朔-望's avatar
朔-望 已提交
20 21 22
#include <vector>

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
23
namespace framework {
朔-望's avatar
朔-望 已提交
24

朔-望's avatar
朔-望 已提交
25
Scope &Scope::NewScope() const {
26 27
  kids_.push_back(new Scope(this));
  return *kids_.back();
朔-望's avatar
朔-望 已提交
28
}
朔-望's avatar
朔-望 已提交
29

朔-望's avatar
朔-望 已提交
30
Variable *Scope::Var(const std::string &name) {
31 32
  auto *pvar = FindVarLocally(name);
  if (pvar != nullptr) {
朔-望's avatar
朔-望 已提交
33
    return pvar;
朔-望's avatar
朔-望 已提交
34
  }
35 36
  pvar = new Variable;
  vars_[name] = pvar;
W
wangliu 已提交
37
  pvar->name_ = vars_.find(name)->first;
38
  return pvar;
朔-望's avatar
朔-望 已提交
39
}
朔-望's avatar
朔-望 已提交
40

朔-望's avatar
朔-望 已提交
41
Variable *Scope::FindVar(const std::string &name) const {
42 43 44 45 46
  auto *pvar = FindVarLocally(name);
  if (pvar != nullptr) {
    return pvar;
  }
  return (parent_ == nullptr) ? nullptr : parent_->FindVar(name);
朔-望's avatar
朔-望 已提交
47
}
朔-望's avatar
朔-望 已提交
48

朔-望's avatar
朔-望 已提交
49
const Scope *Scope::FindScope(const Variable *var) const {
50 51 52
  for (auto &name_var : vars_) {
    if (name_var.second == var) {
      return this;
朔-望's avatar
朔-望 已提交
53
    }
54 55
  }
  return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
朔-望's avatar
朔-望 已提交
56
}
朔-望's avatar
朔-望 已提交
57

朔-望's avatar
朔-望 已提交
58
void Scope::DropKids() {
59 60 61 62
  for (Scope *s : kids_) {
    delete s;
  }
  kids_.clear();
朔-望's avatar
朔-望 已提交
63
}
朔-望's avatar
朔-望 已提交
64

朔-望's avatar
朔-望 已提交
65
std::vector<std::string> Scope::LocalVarNames() const {
66 67 68 69 70 71
  std::vector<std::string> known_vars;
  known_vars.reserve(vars_.size());
  for (auto &name_var : vars_) {
    known_vars.emplace_back(name_var.first);
  }
  return known_vars;
朔-望's avatar
朔-望 已提交
72
}
朔-望's avatar
朔-望 已提交
73

朔-望's avatar
朔-望 已提交
74
void Scope::DeleteScope(Scope *scope) const {
75 76 77
  auto it = std::find(kids_.begin(), kids_.end(), scope);
  kids_.erase(it);
  delete scope;
朔-望's avatar
朔-望 已提交
78
}
朔-望's avatar
朔-望 已提交
79

朔-望's avatar
朔-望 已提交
80
void Scope::EraseVars(const std::vector<std::string> &var_names) {
81 82 83 84 85 86 87
  std::set<std::string> var_set(var_names.begin(), var_names.end());
  for (auto it = vars_.begin(); it != vars_.end();) {
    if (var_set.find(it->first) != var_set.end()) {
      delete it->second;
      it = vars_.erase(it);
    } else {
      ++it;
朔-望's avatar
朔-望 已提交
88
    }
89
  }
朔-望's avatar
朔-望 已提交
90
}
朔-望's avatar
朔-望 已提交
91

朔-望's avatar
朔-望 已提交
92 93
void Scope::Rename(const std::string &origin_name,
                   const std::string &new_name) const {
94 95 96 97 98 99 100 101 102 103
  auto origin_it = vars_.find(origin_name);
  if (origin_it == vars_.end()) {
    return;
  }
  auto new_it = vars_.find(new_name);
  if (new_it != vars_.end()) {
    return;
  }
  vars_[new_name] = origin_it->second;
  vars_.erase(origin_it);
朔-望's avatar
朔-望 已提交
104
}
朔-望's avatar
朔-望 已提交
105

朔-望's avatar
朔-望 已提交
106
Variable *Scope::FindVarLocally(const std::string &name) const {
107 108 109 110 111
  auto it = vars_.find(name);
  if (it != vars_.end()) {
    return it->second;
  }
  return nullptr;
朔-望's avatar
朔-望 已提交
112
}
朔-望's avatar
朔-望 已提交
113

114 115 116 117 118
#ifdef PADDLE_MOBILE_FPGA
Variable *Scope::Var(const std::string &name, const int id) {
  return Var(name + std::to_string(id));
}

Z
zhangyang0701 已提交
119 120
std::vector<Variable *> Scope::VarContain(const std::string substring,
                                          int *min) {
121
  std::vector<Variable *> v;
Z
zhangyang0701 已提交
122 123 124

  int temp = 9999;
  auto len0 = substring.length();
125 126 127
  for (auto pair : vars_) {
    if (pair.first.find(substring) == 0) {
      v.push_back(pair.second);
Z
zhangyang0701 已提交
128 129 130 131 132
      auto len1 = pair.first.length();
      int index = std::stoi(pair.first.substr(len0, len1));
      if (index < temp) {
        temp = index;
      }
133 134
    }
  }
Z
zhangyang0701 已提交
135
  *min = temp;
136 137 138 139 140 141 142 143 144 145 146 147
  return v;
}

void Scope::print_vars() {
  DLOG << "====================start to print variables=================";
  for (auto pair : vars_) {
    DLOG << pair.first;
  }
  DLOG << "==================complete printing variables================";
}
#endif

朔-望's avatar
朔-望 已提交
148 149
}  // namespace framework
}  // namespace paddle_mobile