dag.h 2.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 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
#include <string>
#include <vector>
G
guru4elephant 已提交
18
#include "core/predictor/common/inner_common.h"
W
wangguibao 已提交
19 20 21 22 23

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
24
enum EdgeMode { RO = 0, RW = 1, UNKNOWN };
W
wangguibao 已提交
25 26

struct DagNode {
W
wangguibao 已提交
27 28 29 30 31
  uint32_t id;
  uint32_t stage;
  std::string name;       // opname
  std::string full_name;  // workflow_stageindex_opname
  std::string type;
32
  std::vector<std::string> address;
W
wangguibao 已提交
33 34
  void* conf;
  boost::unordered_map<std::string, EdgeMode> depends;
W
wangguibao 已提交
35 36 37
};

struct DagStage {
W
wangguibao 已提交
38 39 40
  std::vector<DagNode*> nodes;
  std::string name;       // stageindex
  std::string full_name;  // workflow_stageindex
W
wangguibao 已提交
41 42 43
};

class Dag {
W
wangguibao 已提交
44 45
 public:
  Dag();
W
wangguibao 已提交
46

W
wangguibao 已提交
47
  virtual ~Dag();
W
wangguibao 已提交
48

W
wangguibao 已提交
49
  EdgeMode parse_mode(std::string& mode);  // NOLINT
W
wangguibao 已提交
50

W
wangguibao 已提交
51
  int init(const char* path, const char* file, const std::string& name);
W
wangguibao 已提交
52

W
wangguibao 已提交
53
  int init(const configure::Workflow& conf, const std::string& name);
W
wangguibao 已提交
54

W
wangguibao 已提交
55
  int deinit();
W
wangguibao 已提交
56

W
wangguibao 已提交
57
  uint32_t nodes_size();
W
wangguibao 已提交
58

W
wangguibao 已提交
59
  const DagNode* node_by_id(uint32_t id);
W
wangguibao 已提交
60

W
wangguibao 已提交
61
  const DagNode* node_by_id(uint32_t id) const;
W
wangguibao 已提交
62

W
wangguibao 已提交
63
  const DagNode* node_by_name(std::string& name);  // NOLINT
W
wangguibao 已提交
64

W
wangguibao 已提交
65
  const DagNode* node_by_name(const std::string& name) const;
W
wangguibao 已提交
66

W
wangguibao 已提交
67
  uint32_t stage_size();
W
wangguibao 已提交
68

W
wangguibao 已提交
69
  const DagStage* stage_by_index(uint32_t index);
W
wangguibao 已提交
70

W
wangguibao 已提交
71
  const std::string& name() const { return _dag_name; }
W
wangguibao 已提交
72

W
wangguibao 已提交
73
  const std::string& full_name() const { return _dag_name; }
W
wangguibao 已提交
74

W
wangguibao 已提交
75 76 77 78 79 80 81 82 83 84 85
  void regist_metric(const std::string& service_name);

 private:
  int topo_sort();

 private:
  std::string _dag_name;
  boost::unordered_map<std::string, DagNode*> _name_nodes;
  std::vector<DagNode*> _index_nodes;
  std::vector<DagStage*> _stages;
};
W
wangguibao 已提交
86

W
wangguibao 已提交
87 88 89
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu