Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
graph.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_UTILS_GRAPH_H_
23 #define SINGA_UTILS_GRAPH_H_
24 
25 #include <stack>
26 #include <string>
27 #include <map>
28 #include <vector>
29 
30 namespace singa {
31 
32 class Node {
33  public:
39  explicit Node(std::string name);
49  Node(const std::string& name, const std::string& origin, int id, void* proto);
50  ~Node() {} // the proto field is deleted outside by other functions
51  void AddDstNode(Node* dstnode);
52  void AddSrcNode(Node* srcnode);
53  void RemoveDstNode(Node* dst);
54  void RemoveSrcNode(Node* src);
55 
56  std::string name = "";
58  std::string origin = "";
60  int partition_id = 0;
62  void* proto = nullptr;
63  std::vector<Node*> srcnodes;
64  std::vector<Node*> dstnodes;
65 };
66 
72 class Graph {
73  public:
74  Graph() {}
75  ~Graph();
79  inline const std::vector<Node*>& nodes() const {
80  return nodes_;
81  }
86  inline Node* node(const std::string& name) const {
87  return name2node_.at(name);
88  }
89  void AddNode(Node* node);
90  Node* AddNode(const std::string& name);
91  void AddEdge(Node* srcnode, Node* dstnode);
92  void AddEdge(const std::string& src, const std::string& dst);
93  void RemoveEdge(Node* src, Node* dst);
94  void RemoveEdge(const std::string &src, const std::string& dst);
99  std::string ToJson() const;
105  std::string ToJson(const std::map<std::string, std::string>& info) const;
109  void Sort();
110 
111  private:
112  std::vector<Node*> nodes_;
113  std::map<std::string, Node*> name2node_;
114 };
115 
116 } // namespace singa
117 
118 #endif // SINGA_UTILS_GRAPH_H_
int partition_id
partition id
Definition: graph.h:60
std::string origin
name of the origin node/layer from which is node is derived
Definition: graph.h:58
void * proto
proto of the corresponding layer
Definition: graph.h:62
Definition: graph.h:32
const std::vector< Node * > & nodes() const
Definition: graph.h:79
Node(std::string name)
Node constructor.
void Sort()
Do topology sort for all nodes of the graph.
Neuralnet is constructed by creating a graph with each node representing one layer at first...
Definition: graph.h:72
Node * node(const std::string &name) const
Definition: graph.h:86
std::string ToJson() const
Dump the graph into json string which can be used to draw a picture by graphviz.