heterograph.h 9.76 KB
Newer Older
1
2
/*!
 *  Copyright (c) 2019 by Contributors
3
4
 * @file graph/heterograph.h
 * @brief Heterograph
5
6
7
8
9
 */

#ifndef DGL_GRAPH_HETEROGRAPH_H_
#define DGL_GRAPH_HETEROGRAPH_H_

10
#include <dgl/runtime/shared_mem.h>
11
12
13
14
15
#include <dgl/base_heterograph.h>
#include <dgl/lazy.h>
#include <utility>
#include <string>
#include <vector>
16
17
#include <set>
#include <tuple>
18
#include <memory>
19
#include "./unit_graph.h"
20
#include "shared_mem_manager.h"
21
22
23

namespace dgl {

24
/*! @brief Heterograph */
25
26
class HeteroGraph : public BaseHeteroGraph {
 public:
27
28
29
30
  HeteroGraph(
      GraphPtr meta_graph,
      const std::vector<HeteroGraphPtr>& rel_graphs,
      const std::vector<int64_t>& num_nodes_per_type = {});
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

  HeteroGraphPtr GetRelationGraph(dgl_type_t etype) const override {
    CHECK_LT(etype, meta_graph_->NumEdges()) << "Invalid edge type: " << etype;
    return relation_graphs_[etype];
  }

  void AddVertices(dgl_type_t vtype, uint64_t num_vertices) override {
    LOG(FATAL) << "Bipartite graph is not mutable.";
  }

  void AddEdge(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) override {
    LOG(FATAL) << "Bipartite graph is not mutable.";
  }

  void AddEdges(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) override {
    LOG(FATAL) << "Bipartite graph is not mutable.";
  }

  void Clear() override {
    LOG(FATAL) << "Bipartite graph is not mutable.";
  }

53
  DGLDataType DataType() const override {
54
55
56
    return relation_graphs_[0]->DataType();
  }

57
  DGLContext Context() const override {
58
59
60
    return relation_graphs_[0]->Context();
  }

61
62
63
64
  bool IsPinned() const override {
    return relation_graphs_[0]->IsPinned();
  }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  uint8_t NumBits() const override {
    return relation_graphs_[0]->NumBits();
  }

  bool IsMultigraph() const override;

  bool IsReadonly() const override {
    return true;
  }

  uint64_t NumVertices(dgl_type_t vtype) const override {
    CHECK(meta_graph_->HasVertex(vtype)) << "Invalid vertex type: " << vtype;
    return num_verts_per_type_[vtype];
  }

80
81
82
83
  inline std::vector<int64_t> NumVerticesPerType() const override {
    return num_verts_per_type_;
  }

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  uint64_t NumEdges(dgl_type_t etype) const override {
    return GetRelationGraph(etype)->NumEdges(0);
  }

  bool HasVertex(dgl_type_t vtype, dgl_id_t vid) const override {
    return vid < NumVertices(vtype);
  }

  BoolArray HasVertices(dgl_type_t vtype, IdArray vids) const override;

  bool HasEdgeBetween(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
    return GetRelationGraph(etype)->HasEdgeBetween(0, src, dst);
  }

  BoolArray HasEdgesBetween(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) const override {
    return GetRelationGraph(etype)->HasEdgesBetween(0, src_ids, dst_ids);
  }

  IdArray Predecessors(dgl_type_t etype, dgl_id_t dst) const override {
    return GetRelationGraph(etype)->Predecessors(0, dst);
  }

  IdArray Successors(dgl_type_t etype, dgl_id_t src) const override {
    return GetRelationGraph(etype)->Successors(0, src);
  }

  IdArray EdgeId(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
    return GetRelationGraph(etype)->EdgeId(0, src, dst);
  }

114
115
116
117
118
119
  EdgeArray EdgeIdsAll(dgl_type_t etype, IdArray src, IdArray dst) const override {
    return GetRelationGraph(etype)->EdgeIdsAll(0, src, dst);
  }

  IdArray EdgeIdsOne(dgl_type_t etype, IdArray src, IdArray dst) const override {
    return GetRelationGraph(etype)->EdgeIdsOne(0, src, dst);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  }

  std::pair<dgl_id_t, dgl_id_t> FindEdge(dgl_type_t etype, dgl_id_t eid) const override {
    return GetRelationGraph(etype)->FindEdge(0, eid);
  }

  EdgeArray FindEdges(dgl_type_t etype, IdArray eids) const override {
    return GetRelationGraph(etype)->FindEdges(0, eids);
  }

  EdgeArray InEdges(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->InEdges(0, vid);
  }

  EdgeArray InEdges(dgl_type_t etype, IdArray vids) const override {
    return GetRelationGraph(etype)->InEdges(0, vids);
  }

  EdgeArray OutEdges(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->OutEdges(0, vid);
  }

  EdgeArray OutEdges(dgl_type_t etype, IdArray vids) const override {
    return GetRelationGraph(etype)->OutEdges(0, vids);
  }

  EdgeArray Edges(dgl_type_t etype, const std::string &order = "") const override {
    return GetRelationGraph(etype)->Edges(0, order);
  }

  uint64_t InDegree(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->InDegree(0, vid);
  }

  DegreeArray InDegrees(dgl_type_t etype, IdArray vids) const override {
    return GetRelationGraph(etype)->InDegrees(0, vids);
  }

  uint64_t OutDegree(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->OutDegree(0, vid);
  }

  DegreeArray OutDegrees(dgl_type_t etype, IdArray vids) const override {
    return GetRelationGraph(etype)->OutDegrees(0, vids);
  }

  DGLIdIters SuccVec(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->SuccVec(0, vid);
  }

  DGLIdIters OutEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->OutEdgeVec(0, vid);
  }

  DGLIdIters PredVec(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->PredVec(0, vid);
  }

  DGLIdIters InEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
    return GetRelationGraph(etype)->InEdgeVec(0, vid);
  }

  std::vector<IdArray> GetAdj(
      dgl_type_t etype, bool transpose, const std::string &fmt) const override {
    return GetRelationGraph(etype)->GetAdj(0, transpose, fmt);
  }

187
188
189
190
191
192
193
194
195
196
197
198
  aten::COOMatrix GetCOOMatrix(dgl_type_t etype) const override {
    return GetRelationGraph(etype)->GetCOOMatrix(0);
  }

  aten::CSRMatrix GetCSCMatrix(dgl_type_t etype) const override {
    return GetRelationGraph(etype)->GetCSCMatrix(0);
  }

  aten::CSRMatrix GetCSRMatrix(dgl_type_t etype) const override {
    return GetRelationGraph(etype)->GetCSRMatrix(0);
  }

199
200
  SparseFormat SelectFormat(dgl_type_t etype, dgl_format_code_t preferred_formats) const override {
    return GetRelationGraph(etype)->SelectFormat(0, preferred_formats);
201
202
  }

203
204
  dgl_format_code_t GetAllowedFormats() const override {
    return GetRelationGraph(0)->GetAllowedFormats();
205
206
  }

207
208
  dgl_format_code_t GetCreatedFormats() const override {
    return GetRelationGraph(0)->GetCreatedFormats();
209
210
  }

211
212
213
214
215
  HeteroSubgraph VertexSubgraph(const std::vector<IdArray>& vids) const override;

  HeteroSubgraph EdgeSubgraph(
      const std::vector<IdArray>& eids, bool preserve_nodes = false) const override;

216
  HeteroGraphPtr GetGraphInFormat(dgl_format_code_t formats) const override;
217

Minjie Wang's avatar
Minjie Wang committed
218
219
  FlattenedHeteroGraphPtr Flatten(const std::vector<dgl_type_t>& etypes) const override;

220
221
  GraphPtr AsImmutableGraph() const override;

222
  /*! @return Load HeteroGraph from stream, using CSRMatrix*/
223
224
  bool Load(dmlc::Stream* fs);

225
  /*! @return Save HeteroGraph to stream, using CSRMatrix */
226
227
  void Save(dmlc::Stream* fs) const;

228
  /*! @brief Convert the graph to use the given number of bits for storage */
229
230
  static HeteroGraphPtr AsNumBits(HeteroGraphPtr g, uint8_t bits);

231
  /*! @brief Copy the data to another context */
232
  static HeteroGraphPtr CopyTo(HeteroGraphPtr g, const DGLContext &ctx);
233

234

235
  /*!
236
237
  * @brief Pin all relation graphs of the current graph.
  * @note The graph will be pinned inplace. Behavior depends on the current context,
238
  *       kDGLCPU: will be pinned;
239
  *       IsPinned: directly return;
240
  *       kDGLCUDA: invalid, will throw an error.
241
242
  *       The context check is deferred to pinning the NDArray.
  */
243
  void PinMemory_() override;
244
245

  /*!
246
247
  * @brief Unpin all relation graphs of the current graph.
  * @note The graph will be unpinned inplace. Behavior depends on the current context,
248
  *       IsPinned: will be unpinned;
249
250
251
252
253
  *       others: directly return.
  *       The context check is deferred to unpinning the NDArray.
  */
  void UnpinMemory_();

254
  /*!
255
256
   * @brief Record stream for this graph.
   * @param stream The stream that is using the graph
257
258
259
   */
  void RecordStream(DGLStreamHandle stream) override;

260
  /*! @brief Copy the data to shared memory.
261
262
263
264
265
266
267
  *
  * Also save names of node types and edge types of the HeteroGraph object to shared memory
  */
  static HeteroGraphPtr CopyToSharedMem(
      HeteroGraphPtr g, const std::string& name, const std::vector<std::string>& ntypes,
      const std::vector<std::string>& etypes, const std::set<std::string>& fmts);

268
  /*! @brief Create a heterograph from 
269
270
271
272
  *   \return the HeteroGraphPtr, names of node types, names of edge types
  */
  static std::tuple<HeteroGraphPtr, std::vector<std::string>, std::vector<std::string>>
      CreateFromSharedMem(const std::string &name);
273

274
  /*! @brief Creat a LineGraph of self */
275
276
  HeteroGraphPtr LineGraph(bool backtracking) const;

277
278
279
280
  const std::vector<UnitGraphPtr>& relation_graphs() const {
    return relation_graphs_;
  }

281
 private:
282
283
284
285
  // To create empty class
  friend class Serializer;

  // Empty Constructor, only for serializer
286
  HeteroGraph() : BaseHeteroGraph() {}
287

288
  /*! @brief A map from edge type to unit graph */
289
  std::vector<UnitGraphPtr> relation_graphs_;
290

291
  /*! @brief A map from vert type to the number of verts in the type */
292
  std::vector<int64_t> num_verts_per_type_;
293

294
  /*! @brief The shared memory object for meta info*/
295
296
  std::shared_ptr<runtime::SharedMemory> shared_mem_;

297
  /*! @brief The name of the shared memory. Return empty string if it is not in shared memory. */
298
299
  std::string SharedMemName() const;

300
  /*! @brief template class for Flatten operation
301
  * 
302
303
304
  * @tparam IdType Graph's index data type, can be int32_t or int64_t
  * @param etypes vector of etypes to be falttened
  * @return pointer of FlattenedHeteroGraphh
305
306
307
  */
  template <class IdType>
  FlattenedHeteroGraphPtr FlattenImpl(const std::vector<dgl_type_t>& etypes) const;
308
309
310
311
};

}  // namespace dgl

312
313
314
315
316
317

namespace dmlc {
DMLC_DECLARE_TRAITS(has_saveload, dgl::HeteroGraph, true);
}  // namespace dmlc


318
#endif  // DGL_GRAPH_HETEROGRAPH_H_