block_desc.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. */

L
liuruilong 已提交
15 16
#pragma once

17 18
#include <memory>
#include <vector>
L
liuruilong 已提交
19
#include "framework/framework.pb-c.h"
L
liuruilong 已提交
20 21 22 23 24 25
#include "framework/program/op_desc.h"
#include "framework/program/var_desc.h"

namespace paddle_mobile {
namespace framework {

L
liuruilong 已提交
26
class BlockDesc {
L
liuruilong 已提交
27 28 29
 public:
  friend class Node;
  friend class ProgramOptimize;
L
liuruilong 已提交
30
  BlockDesc() {}
31 32
  explicit BlockDesc(PaddleMobile__Framework__Proto__BlockDesc *desc);
  explicit BlockDesc(const BlockDesc &block_desc)
L
liuruilong 已提交
33
      : index_(block_desc.index_), parent_index_(block_desc.parent_index_) {
L
liuruilong 已提交
34 35 36 37 38
    for (auto &op_desc : block_desc.ops_) {
      std::shared_ptr<OpDesc> copy_op_desc = std::make_shared<OpDesc>(*op_desc);
      ops_.push_back(copy_op_desc);
    }

L
liuruilong 已提交
39 40
    for (int i = 0; i < block_desc.vars_.size(); ++i) {
      auto &var_desc = block_desc.vars_[i];
L
liuruilong 已提交
41
      vars_.emplace_back(std::make_shared<VarDesc>(*var_desc));
L
liuruilong 已提交
42 43 44 45 46
    }
  }

  const int &ID() const { return index_; }

L
liuruilong 已提交
47 48
  const bool &MultiThread() const { return multi_thread_; }

L
liuruilong 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  const int &Parent() const { return parent_index_; }

  bool operator==(const paddle_mobile::framework::BlockDesc &in_block) const {
    return this->ID() == in_block.ID() && this->Parent() == in_block.Parent();
  }

  bool operator<(const paddle_mobile::framework::BlockDesc &in_block) const {
    return this->ID() < in_block.ID() && this->Parent() < in_block.Parent();
  }

  std::vector<std::shared_ptr<VarDesc>> Vars() const;
  std::vector<std::shared_ptr<OpDesc>> Ops() const;

 private:
  int index_;
L
liuruilong 已提交
64
  bool multi_thread_;
L
liuruilong 已提交
65 66
  int parent_index_;
  std::vector<std::shared_ptr<OpDesc>> ops_;
L
liuruilong 已提交
67
  std::vector<std::shared_ptr<VarDesc>> vars_;
L
liuruilong 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
};

}  // namespace framework
}  // namespace paddle_mobile

namespace std {

template <>
struct hash<paddle_mobile::framework::BlockDesc> {
  typedef paddle_mobile::framework::BlockDesc argument_type;
  typedef std::size_t result_type;
  result_type operator()(argument_type const &s) const noexcept {
    result_type const h1(std::hash<int>{}(s.ID()));
    result_type const h2(std::hash<int>{}(s.ID()));
    return h1 ^ (h2 << 1);
  }
};

}  // namespace std