dep_core.h 1.9 KB
Newer Older
D
dolphin8 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* 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. */

#pragma once
#ifdef PADDLE_EXECUTOR_MULTITHREAD
#include <string>
#include <unordered_map>
#include <vector>
#include "framework/operator.h"

namespace paddle_mobile {

class depCore {
 public:
  template <typename Dtype>
  void analysisDep(
      const std::vector<std::shared_ptr<framework::OperatorBase<Dtype>>>& ops) {
    std::unordered_map<std::string, int> vars;
    size_t nop = ops.size();
    deps.resize(nop);
    next.resize(nop);
    for (size_t i = 0; i < nop; i++) {
      const auto& op = ops[i];
      for (const auto& kv : op->Inputs()) {
D
dolphin8 已提交
36 37 38 39 40 41 42
        for (const auto& v : kv.second) {
          if (vars.find(v) == vars.end()) {
            continue;
          }
          int di = vars[v];
          if (di == i) {
            continue;
D
dolphin8 已提交
43
          }
D
dolphin8 已提交
44 45 46 47 48 49
          if (std::find(deps[i].begin(), deps[i].end(), di) != deps[i].end()) {
            continue;
          }
          deps[i].push_back(di);
          next[di].push_back(i);
        }
D
dolphin8 已提交
50 51 52 53
      }
      for (const auto& kv : op->Outputs()) {
        for (const auto& v : kv.second) {
          vars[v] = i;
D
dolphin8 已提交
54 55 56 57 58 59 60 61 62 63 64 65
        }
      }
    }
  }
  const std::vector<int>& getNext(int i) { return next[i]; }
  const std::vector<int>& getDeps(int i) { return deps[i]; }
  std::vector<std::vector<int>> deps;
  std::vector<std::vector<int>> next;
};
}  // namespace paddle_mobile

#endif