unit_graph.cc 48.7 KB
Newer Older
1
2
/*!
 *  Copyright (c) 2019 by Contributors
Minjie Wang's avatar
Minjie Wang committed
3
4
 * \file graph/unit_graph.cc
 * \brief UnitGraph graph implementation
5
6
 */
#include <dgl/array.h>
7
#include <dgl/base_heterograph.h>
8
9
#include <dgl/immutable_graph.h>
#include <dgl/lazy.h>
10
11

#include "../c_api_common.h"
12
#include "./unit_graph.h"
13
14

namespace dgl {
15

16
namespace {
17
18
19

using namespace dgl::aten;

Minjie Wang's avatar
Minjie Wang committed
20
21
22
23
24
25
26
27
28
29
// create metagraph of one node type
inline GraphPtr CreateUnitGraphMetaGraph1() {
  // a self-loop edge 0->0
  std::vector<int64_t> row_vec(1, 0);
  std::vector<int64_t> col_vec(1, 0);
  IdArray row = aten::VecToIdArray(row_vec);
  IdArray col = aten::VecToIdArray(col_vec);
  GraphPtr g = ImmutableGraph::CreateFromCOO(1, row, col);
  return g;
}
30

Minjie Wang's avatar
Minjie Wang committed
31
32
33
34
35
// create metagraph of two node types
inline GraphPtr CreateUnitGraphMetaGraph2() {
  // an edge 0->1
  std::vector<int64_t> row_vec(1, 0);
  std::vector<int64_t> col_vec(1, 1);
36
37
38
39
40
  IdArray row = aten::VecToIdArray(row_vec);
  IdArray col = aten::VecToIdArray(col_vec);
  GraphPtr g = ImmutableGraph::CreateFromCOO(2, row, col);
  return g;
}
Minjie Wang's avatar
Minjie Wang committed
41
42
43
44
45
46
47
48
49
50
51
52

inline GraphPtr CreateUnitGraphMetaGraph(int num_vtypes) {
  static GraphPtr mg1 = CreateUnitGraphMetaGraph1();
  static GraphPtr mg2 = CreateUnitGraphMetaGraph2();
  if (num_vtypes == 1)
    return mg1;
  else if (num_vtypes == 2)
    return mg2;
  else
    LOG(FATAL) << "Invalid number of vertex types. Must be 1 or 2.";
  return {};
}
53
54

};  // namespace
55
56
57
58
59
60
61

//////////////////////////////////////////////////////////
//
// COO graph implementation
//
//////////////////////////////////////////////////////////

Minjie Wang's avatar
Minjie Wang committed
62
class UnitGraph::COO : public BaseHeteroGraph {
63
 public:
Minjie Wang's avatar
Minjie Wang committed
64
65
  COO(GraphPtr metagraph, int64_t num_src, int64_t num_dst, IdArray src, IdArray dst)
    : BaseHeteroGraph(metagraph) {
66
67
68
    CHECK(aten::IsValidIdArray(src));
    CHECK(aten::IsValidIdArray(dst));
    CHECK_EQ(src->shape[0], dst->shape[0]) << "Input arrays should have the same length.";
69
70
    adj_ = aten::COOMatrix{num_src, num_dst, src, dst};
  }
71

72
73
74
75
  COO(GraphPtr metagraph, const aten::COOMatrix& coo)
    : BaseHeteroGraph(metagraph), adj_(coo) {
    // Data index should not be inherited. Edges in COO format are always
    // assigned ids from 0 to num_edges - 1.
76
    CHECK(!COOHasData(coo)) << "[BUG] COO should not contain data.";
77
    adj_.data = aten::NullArray();
78
  }
79

Minjie Wang's avatar
Minjie Wang committed
80
81
  inline dgl_type_t SrcType() const {
    return 0;
82
  }
Minjie Wang's avatar
Minjie Wang committed
83
84
85
86
87
88
89

  inline dgl_type_t DstType() const {
    return NumVertexTypes() == 1? 0 : 1;
  }

  inline dgl_type_t EdgeType() const {
    return 0;
90
91
92
  }

  HeteroGraphPtr GetRelationGraph(dgl_type_t etype) const override {
Minjie Wang's avatar
Minjie Wang committed
93
    LOG(FATAL) << "The method shouldn't be called for UnitGraph graph. "
94
95
96
97
98
      << "The relation graph is simply this graph itself.";
    return {};
  }

  void AddVertices(dgl_type_t vtype, uint64_t num_vertices) override {
Minjie Wang's avatar
Minjie Wang committed
99
    LOG(FATAL) << "UnitGraph graph is not mutable.";
100
101
102
  }

  void AddEdge(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) override {
Minjie Wang's avatar
Minjie Wang committed
103
    LOG(FATAL) << "UnitGraph graph is not mutable.";
104
105
106
  }

  void AddEdges(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) override {
Minjie Wang's avatar
Minjie Wang committed
107
    LOG(FATAL) << "UnitGraph graph is not mutable.";
108
109
110
  }

  void Clear() override {
Minjie Wang's avatar
Minjie Wang committed
111
    LOG(FATAL) << "UnitGraph graph is not mutable.";
112
113
  }

114
115
116
117
  DLDataType DataType() const override {
    return adj_.row->dtype;
  }

118
119
120
121
122
123
124
125
  DLContext Context() const override {
    return adj_.row->ctx;
  }

  uint8_t NumBits() const override {
    return adj_.row->dtype.bits;
  }

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  COO AsNumBits(uint8_t bits) const {
    if (NumBits() == bits)
      return *this;

    COO ret(
        meta_graph_,
        adj_.num_rows, adj_.num_cols,
        aten::AsNumBits(adj_.row, bits),
        aten::AsNumBits(adj_.col, bits));
    return ret;
  }

  COO CopyTo(const DLContext& ctx) const {
    if (Context() == ctx)
      return *this;

    COO ret(
        meta_graph_,
        adj_.num_rows, adj_.num_cols,
        adj_.row.CopyTo(ctx),
        adj_.col.CopyTo(ctx));
    return ret;
  }

150
  bool IsMultigraph() const override {
151
    return aten::COOHasDuplicate(adj_);
152
153
154
155
156
157
158
  }

  bool IsReadonly() const override {
    return true;
  }

  uint64_t NumVertices(dgl_type_t vtype) const override {
Minjie Wang's avatar
Minjie Wang committed
159
    if (vtype == SrcType()) {
160
      return adj_.num_rows;
Minjie Wang's avatar
Minjie Wang committed
161
    } else if (vtype == DstType()) {
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
      return adj_.num_cols;
    } else {
      LOG(FATAL) << "Invalid vertex type: " << vtype;
      return 0;
    }
  }

  uint64_t NumEdges(dgl_type_t etype) const override {
    return adj_.row->shape[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 {
    LOG(FATAL) << "Not enabled for COO graph";
    return {};
  }

  bool HasEdgeBetween(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
183
184
185
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
    CHECK(HasVertex(DstType(), dst)) << "Invalid dst vertex id: " << dst;
    return aten::COOIsNonZero(adj_, src, dst);
186
187
188
  }

  BoolArray HasEdgesBetween(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) const override {
189
190
191
    CHECK(aten::IsValidIdArray(src_ids)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dst_ids)) << "Invalid vertex id array.";
    return aten::COOIsNonZero(adj_, src_ids, dst_ids);
192
193
194
  }

  IdArray Predecessors(dgl_type_t etype, dgl_id_t dst) const override {
195
196
    CHECK(HasVertex(DstType(), dst)) << "Invalid dst vertex id: " << dst;
    return aten::COOGetRowDataAndIndices(aten::COOTranspose(adj_), dst).second;
197
198
199
  }

  IdArray Successors(dgl_type_t etype, dgl_id_t src) const override {
200
201
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
    return aten::COOGetRowDataAndIndices(adj_, src).second;
202
203
204
  }

  IdArray EdgeId(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
205
206
207
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
    CHECK(HasVertex(DstType(), dst)) << "Invalid dst vertex id: " << dst;
    return aten::COOGetData(adj_, src, dst);
208
209
210
  }

  EdgeArray EdgeIds(dgl_type_t etype, IdArray src, IdArray dst) const override {
211
212
213
214
    CHECK(aten::IsValidIdArray(src)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dst)) << "Invalid vertex id array.";
    const auto& arrs = aten::COOGetDataAndIndices(adj_, src, dst);
    return EdgeArray{arrs[0], arrs[1], arrs[2]};
215
216
217
218
  }

  std::pair<dgl_id_t, dgl_id_t> FindEdge(dgl_type_t etype, dgl_id_t eid) const override {
    CHECK(eid < NumEdges(etype)) << "Invalid edge id: " << eid;
219
220
    const dgl_id_t src = aten::IndexSelect<int64_t>(adj_.row, eid);
    const dgl_id_t dst = aten::IndexSelect<int64_t>(adj_.col, eid);
221
222
223
224
    return std::pair<dgl_id_t, dgl_id_t>(src, dst);
  }

  EdgeArray FindEdges(dgl_type_t etype, IdArray eids) const override {
225
    CHECK(aten::IsValidIdArray(eids)) << "Invalid edge id array";
226
227
228
229
230
231
    return EdgeArray{aten::IndexSelect(adj_.row, eids),
                     aten::IndexSelect(adj_.col, eids),
                     eids};
  }

  EdgeArray InEdges(dgl_type_t etype, dgl_id_t vid) const override {
232
233
234
235
236
    IdArray ret_src, ret_eid;
    std::tie(ret_eid, ret_src) = aten::COOGetRowDataAndIndices(
        aten::COOTranspose(adj_), vid);
    IdArray ret_dst = aten::Full(vid, ret_src->shape[0], NumBits(), ret_src->ctx);
    return EdgeArray{ret_src, ret_dst, ret_eid};
237
238
239
  }

  EdgeArray InEdges(dgl_type_t etype, IdArray vids) const override {
240
241
242
243
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
    auto coosubmat = aten::COOSliceRows(aten::COOTranspose(adj_), vids);
    auto row = aten::IndexSelect(vids, coosubmat.row);
    return EdgeArray{coosubmat.col, row, coosubmat.data};
244
245
246
  }

  EdgeArray OutEdges(dgl_type_t etype, dgl_id_t vid) const override {
247
248
249
250
    IdArray ret_dst, ret_eid;
    std::tie(ret_eid, ret_dst) = aten::COOGetRowDataAndIndices(adj_, vid);
    IdArray ret_src = aten::Full(vid, ret_dst->shape[0], NumBits(), ret_dst->ctx);
    return EdgeArray{ret_src, ret_dst, ret_eid};
251
252
253
  }

  EdgeArray OutEdges(dgl_type_t etype, IdArray vids) const override {
254
255
256
257
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
    auto coosubmat = aten::COOSliceRows(adj_, vids);
    auto row = aten::IndexSelect(vids, coosubmat.row);
    return EdgeArray{row, coosubmat.col, coosubmat.data};
258
259
260
261
262
263
264
265
266
267
268
  }

  EdgeArray Edges(dgl_type_t etype, const std::string &order = "") const override {
    CHECK(order.empty() || order == std::string("eid"))
      << "COO only support Edges of order \"eid\", but got \""
      << order << "\".";
    IdArray rst_eid = aten::Range(0, NumEdges(etype), NumBits(), Context());
    return EdgeArray{adj_.row, adj_.col, rst_eid};
  }

  uint64_t InDegree(dgl_type_t etype, dgl_id_t vid) const override {
269
270
    CHECK(HasVertex(DstType(), vid)) << "Invalid dst vertex id: " << vid;
    return aten::COOGetRowNNZ(aten::COOTranspose(adj_), vid);
271
272
273
  }

  DegreeArray InDegrees(dgl_type_t etype, IdArray vids) const override {
274
275
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
    return aten::COOGetRowNNZ(aten::COOTranspose(adj_), vids);
276
277
278
  }

  uint64_t OutDegree(dgl_type_t etype, dgl_id_t vid) const override {
279
280
    CHECK(HasVertex(SrcType(), vid)) << "Invalid src vertex id: " << vid;
    return aten::COOGetRowNNZ(adj_, vid);
281
282
283
  }

  DegreeArray OutDegrees(dgl_type_t etype, IdArray vids) const override {
284
285
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
    return aten::COOGetRowNNZ(adj_, vids);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  }

  DGLIdIters SuccVec(dgl_type_t etype, dgl_id_t vid) const override {
    LOG(INFO) << "Not enabled for COO graph.";
    return {};
  }

  DGLIdIters OutEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
    LOG(INFO) << "Not enabled for COO graph.";
    return {};
  }

  DGLIdIters PredVec(dgl_type_t etype, dgl_id_t vid) const override {
    LOG(INFO) << "Not enabled for COO graph.";
    return {};
  }

  DGLIdIters InEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
    LOG(INFO) << "Not enabled for COO graph.";
    return {};
  }

  std::vector<IdArray> GetAdj(
      dgl_type_t etype, bool transpose, const std::string &fmt) const override {
    CHECK(fmt == "coo") << "Not valid adj format request.";
    if (transpose) {
      return {aten::HStack(adj_.col, adj_.row)};
    } else {
      return {aten::HStack(adj_.row, adj_.col)};
    }
  }

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  aten::COOMatrix GetCOOMatrix(dgl_type_t etype) const override {
    return adj_;
  }

  aten::CSRMatrix GetCSCMatrix(dgl_type_t etype) const override {
    LOG(FATAL) << "Not enabled for COO graph";
    return aten::CSRMatrix();
  }

  aten::CSRMatrix GetCSRMatrix(dgl_type_t etype) const override {
    LOG(FATAL) << "Not enabled for COO graph";
    return aten::CSRMatrix();
  }

  SparseFormat SelectFormat(dgl_type_t etype, SparseFormat preferred_format) const override {
    LOG(FATAL) << "Not enabled for COO graph";
334
    return SparseFormat::kAny;
335
336
  }

337
338
339
340
341
342
343
344
345
346
  std::string GetRestrictFormat() const override {
    LOG(FATAL) << "Not enabled for COO graph";
    return std::string("");
  }

  dgl_format_code_t GetFormatInUse() const override {
    LOG(FATAL) << "Not enabled for COO graph";
    return 0;
  }

347
  HeteroSubgraph VertexSubgraph(const std::vector<IdArray>& vids) const override {
348
349
350
351
352
353
354
355
356
357
358
359
    CHECK_EQ(vids.size(), NumVertexTypes()) << "Number of vertex types mismatch";
    auto srcvids = vids[SrcType()], dstvids = vids[DstType()];
    CHECK(aten::IsValidIdArray(srcvids)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dstvids)) << "Invalid vertex id array.";
    HeteroSubgraph subg;
    const auto& submat = aten::COOSliceMatrix(adj_, srcvids, dstvids);
    IdArray sub_eids = aten::Range(0, submat.data->shape[0], NumBits(), Context());
    subg.graph = std::make_shared<COO>(meta_graph(), submat.num_rows, submat.num_cols,
        submat.row, submat.col);
    subg.induced_vertices = vids;
    subg.induced_edges.emplace_back(submat.data);
    return subg;
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  }

  HeteroSubgraph EdgeSubgraph(
      const std::vector<IdArray>& eids, bool preserve_nodes = false) const override {
    CHECK_EQ(eids.size(), 1) << "Edge type number mismatch.";
    HeteroSubgraph subg;
    if (!preserve_nodes) {
      IdArray new_src = aten::IndexSelect(adj_.row, eids[0]);
      IdArray new_dst = aten::IndexSelect(adj_.col, eids[0]);
      subg.induced_vertices.emplace_back(aten::Relabel_({new_src}));
      subg.induced_vertices.emplace_back(aten::Relabel_({new_dst}));
      const auto new_nsrc = subg.induced_vertices[0]->shape[0];
      const auto new_ndst = subg.induced_vertices[1]->shape[0];
      subg.graph = std::make_shared<COO>(
Minjie Wang's avatar
Minjie Wang committed
374
          meta_graph(), new_nsrc, new_ndst, new_src, new_dst);
375
376
377
378
      subg.induced_edges = eids;
    } else {
      IdArray new_src = aten::IndexSelect(adj_.row, eids[0]);
      IdArray new_dst = aten::IndexSelect(adj_.col, eids[0]);
Quan (Andy) Gan's avatar
Quan (Andy) Gan committed
379
380
381
382
      subg.induced_vertices.emplace_back(
          aten::Range(0, NumVertices(SrcType()), NumBits(), Context()));
      subg.induced_vertices.emplace_back(
          aten::Range(0, NumVertices(DstType()), NumBits(), Context()));
383
      subg.graph = std::make_shared<COO>(
Quan (Andy) Gan's avatar
Quan (Andy) Gan committed
384
          meta_graph(), NumVertices(SrcType()), NumVertices(DstType()), new_src, new_dst);
385
386
387
388
389
      subg.induced_edges = eids;
    }
    return subg;
  }

390
391
392
393
394
  HeteroGraphPtr GetGraphInFormat(SparseFormat restrict_format) const override {
    LOG(FATAL) << "Not enabled for COO graph.";
    return nullptr;
  }

395
396
397
398
  aten::COOMatrix adj() const {
    return adj_;
  }

399
400
401
402
403
404
405
406
407
  /*!
   * \brief Determines whether the graph is "hypersparse", i.e. having significantly more
   * nodes than edges.
   */
  bool IsHypersparse() const {
    return (NumVertices(SrcType()) / 8 > NumEdges(EdgeType())) &&
           (NumVertices(SrcType()) > 1000000);
  }

408
409
410
411
412
413
414
415
416
417
418
419
420
  bool Load(dmlc::Stream* fs) {
    auto meta_imgraph = Serializer::make_shared<ImmutableGraph>();
    CHECK(fs->Read(&meta_imgraph)) << "Invalid meta graph";
    meta_graph_ = meta_imgraph;
    CHECK(fs->Read(&adj_)) << "Invalid adj matrix";
    return true;
  }
  void Save(dmlc::Stream* fs) const {
    auto meta_graph_ptr = ImmutableGraph::ToImmutable(meta_graph());
    fs->Write(meta_graph_ptr);
    fs->Write(adj_);
  }

421
 private:
422
423
424
425
  friend class Serializer;

  COO() {}

426
427
428
429
430
431
432
433
434
435
436
  /*! \brief internal adjacency matrix. Data array is empty */
  aten::COOMatrix adj_;
};

//////////////////////////////////////////////////////////
//
// CSR graph implementation
//
//////////////////////////////////////////////////////////

/*! \brief CSR graph */
Minjie Wang's avatar
Minjie Wang committed
437
class UnitGraph::CSR : public BaseHeteroGraph {
438
 public:
Minjie Wang's avatar
Minjie Wang committed
439
  CSR(GraphPtr metagraph, int64_t num_src, int64_t num_dst,
440
      IdArray indptr, IdArray indices, IdArray edge_ids)
Minjie Wang's avatar
Minjie Wang committed
441
    : BaseHeteroGraph(metagraph) {
442
443
444
445
446
    CHECK(aten::IsValidIdArray(indptr));
    CHECK(aten::IsValidIdArray(indices));
    CHECK(aten::IsValidIdArray(edge_ids));
    CHECK_EQ(indices->shape[0], edge_ids->shape[0])
      << "indices and edge id arrays should have the same length";
447

448
449
450
    adj_ = aten::CSRMatrix{num_src, num_dst, indptr, indices, edge_ids};
  }

451
  CSR(GraphPtr metagraph, const aten::CSRMatrix& csr)
Da Zheng's avatar
Da Zheng committed
452
453
    : BaseHeteroGraph(metagraph), adj_(csr) {
  }
454

Minjie Wang's avatar
Minjie Wang committed
455
456
  inline dgl_type_t SrcType() const {
    return 0;
457
  }
Minjie Wang's avatar
Minjie Wang committed
458
459
460
461
462
463
464

  inline dgl_type_t DstType() const {
    return NumVertexTypes() == 1? 0 : 1;
  }

  inline dgl_type_t EdgeType() const {
    return 0;
465
466
467
  }

  HeteroGraphPtr GetRelationGraph(dgl_type_t etype) const override {
Minjie Wang's avatar
Minjie Wang committed
468
    LOG(FATAL) << "The method shouldn't be called for UnitGraph graph. "
469
470
471
472
473
      << "The relation graph is simply this graph itself.";
    return {};
  }

  void AddVertices(dgl_type_t vtype, uint64_t num_vertices) override {
Minjie Wang's avatar
Minjie Wang committed
474
    LOG(FATAL) << "UnitGraph graph is not mutable.";
475
476
477
  }

  void AddEdge(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) override {
Minjie Wang's avatar
Minjie Wang committed
478
    LOG(FATAL) << "UnitGraph graph is not mutable.";
479
480
481
  }

  void AddEdges(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) override {
Minjie Wang's avatar
Minjie Wang committed
482
    LOG(FATAL) << "UnitGraph graph is not mutable.";
483
484
485
  }

  void Clear() override {
Minjie Wang's avatar
Minjie Wang committed
486
    LOG(FATAL) << "UnitGraph graph is not mutable.";
487
488
  }

489
490
491
492
  DLDataType DataType() const override {
    return adj_.indices->dtype;
  }

493
494
495
496
497
498
499
500
  DLContext Context() const override {
    return adj_.indices->ctx;
  }

  uint8_t NumBits() const override {
    return adj_.indices->dtype.bits;
  }

501
502
503
504
505
  CSR AsNumBits(uint8_t bits) const {
    if (NumBits() == bits) {
      return *this;
    } else {
      CSR ret(
Minjie Wang's avatar
Minjie Wang committed
506
          meta_graph_,
507
508
509
510
511
512
513
514
515
516
517
518
519
          adj_.num_rows, adj_.num_cols,
          aten::AsNumBits(adj_.indptr, bits),
          aten::AsNumBits(adj_.indices, bits),
          aten::AsNumBits(adj_.data, bits));
      return ret;
    }
  }

  CSR CopyTo(const DLContext& ctx) const {
    if (Context() == ctx) {
      return *this;
    } else {
      CSR ret(
Minjie Wang's avatar
Minjie Wang committed
520
          meta_graph_,
521
522
523
524
525
526
527
528
          adj_.num_rows, adj_.num_cols,
          adj_.indptr.CopyTo(ctx),
          adj_.indices.CopyTo(ctx),
          adj_.data.CopyTo(ctx));
      return ret;
    }
  }

529
  bool IsMultigraph() const override {
530
    return aten::CSRHasDuplicate(adj_);
531
532
533
534
535
536
537
  }

  bool IsReadonly() const override {
    return true;
  }

  uint64_t NumVertices(dgl_type_t vtype) const override {
Minjie Wang's avatar
Minjie Wang committed
538
    if (vtype == SrcType()) {
539
      return adj_.num_rows;
Minjie Wang's avatar
Minjie Wang committed
540
    } else if (vtype == DstType()) {
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
      return adj_.num_cols;
    } else {
      LOG(FATAL) << "Invalid vertex type: " << vtype;
      return 0;
    }
  }

  uint64_t NumEdges(dgl_type_t etype) const override {
    return adj_.indices->shape[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 {
    LOG(FATAL) << "Not enabled for COO graph";
    return {};
  }

  bool HasEdgeBetween(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
Minjie Wang's avatar
Minjie Wang committed
562
563
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
    CHECK(HasVertex(DstType(), dst)) << "Invalid dst vertex id: " << dst;
564
565
566
567
    return aten::CSRIsNonZero(adj_, src, dst);
  }

  BoolArray HasEdgesBetween(dgl_type_t etype, IdArray src_ids, IdArray dst_ids) const override {
568
569
    CHECK(aten::IsValidIdArray(src_ids)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dst_ids)) << "Invalid vertex id array.";
570
571
572
573
574
575
576
577
578
    return aten::CSRIsNonZero(adj_, src_ids, dst_ids);
  }

  IdArray Predecessors(dgl_type_t etype, dgl_id_t dst) const override {
    LOG(INFO) << "Not enabled for CSR graph.";
    return {};
  }

  IdArray Successors(dgl_type_t etype, dgl_id_t src) const override {
Minjie Wang's avatar
Minjie Wang committed
579
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
580
581
582
583
    return aten::CSRGetRowColumnIndices(adj_, src);
  }

  IdArray EdgeId(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const override {
Minjie Wang's avatar
Minjie Wang committed
584
585
    CHECK(HasVertex(SrcType(), src)) << "Invalid src vertex id: " << src;
    CHECK(HasVertex(DstType(), dst)) << "Invalid dst vertex id: " << dst;
586
587
588
589
    return aten::CSRGetData(adj_, src, dst);
  }

  EdgeArray EdgeIds(dgl_type_t etype, IdArray src, IdArray dst) const override {
590
591
    CHECK(aten::IsValidIdArray(src)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dst)) << "Invalid vertex id array.";
592
593
594
595
596
    const auto& arrs = aten::CSRGetDataAndIndices(adj_, src, dst);
    return EdgeArray{arrs[0], arrs[1], arrs[2]};
  }

  std::pair<dgl_id_t, dgl_id_t> FindEdge(dgl_type_t etype, dgl_id_t eid) const override {
597
    LOG(FATAL) << "Not enabled for CSR graph.";
598
599
600
601
    return {};
  }

  EdgeArray FindEdges(dgl_type_t etype, IdArray eids) const override {
602
    LOG(FATAL) << "Not enabled for CSR graph.";
603
604
605
606
    return {};
  }

  EdgeArray InEdges(dgl_type_t etype, dgl_id_t vid) const override {
607
    LOG(FATAL) << "Not enabled for CSR graph.";
608
609
610
611
    return {};
  }

  EdgeArray InEdges(dgl_type_t etype, IdArray vids) const override {
612
    LOG(FATAL) << "Not enabled for CSR graph.";
613
614
615
616
    return {};
  }

  EdgeArray OutEdges(dgl_type_t etype, dgl_id_t vid) const override {
Minjie Wang's avatar
Minjie Wang committed
617
    CHECK(HasVertex(SrcType(), vid)) << "Invalid src vertex id: " << vid;
618
619
620
621
622
623
624
    IdArray ret_dst = aten::CSRGetRowColumnIndices(adj_, vid);
    IdArray ret_eid = aten::CSRGetRowData(adj_, vid);
    IdArray ret_src = aten::Full(vid, ret_dst->shape[0], NumBits(), ret_dst->ctx);
    return EdgeArray{ret_src, ret_dst, ret_eid};
  }

  EdgeArray OutEdges(dgl_type_t etype, IdArray vids) const override {
625
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
    auto csrsubmat = aten::CSRSliceRows(adj_, vids);
    auto coosubmat = aten::CSRToCOO(csrsubmat, false);
    // Note that the row id in the csr submat is relabled, so
    // we need to recover it using an index select.
    auto row = aten::IndexSelect(vids, coosubmat.row);
    return EdgeArray{row, coosubmat.col, coosubmat.data};
  }

  EdgeArray Edges(dgl_type_t etype, const std::string &order = "") const override {
    CHECK(order.empty() || order == std::string("srcdst"))
      << "CSR only support Edges of order \"srcdst\","
      << " but got \"" << order << "\".";
    const auto& coo = aten::CSRToCOO(adj_, false);
    return EdgeArray{coo.row, coo.col, coo.data};
  }

  uint64_t InDegree(dgl_type_t etype, dgl_id_t vid) const override {
643
    LOG(FATAL) << "Not enabled for CSR graph.";
644
645
646
647
    return {};
  }

  DegreeArray InDegrees(dgl_type_t etype, IdArray vids) const override {
648
    LOG(FATAL) << "Not enabled for CSR graph.";
649
650
651
652
    return {};
  }

  uint64_t OutDegree(dgl_type_t etype, dgl_id_t vid) const override {
Minjie Wang's avatar
Minjie Wang committed
653
    CHECK(HasVertex(SrcType(), vid)) << "Invalid src vertex id: " << vid;
654
655
656
657
    return aten::CSRGetRowNNZ(adj_, vid);
  }

  DegreeArray OutDegrees(dgl_type_t etype, IdArray vids) const override {
658
    CHECK(aten::IsValidIdArray(vids)) << "Invalid vertex id array.";
659
660
661
662
663
664
    return aten::CSRGetRowNNZ(adj_, vids);
  }

  DGLIdIters SuccVec(dgl_type_t etype, dgl_id_t vid) const override {
    // TODO(minjie): This still assumes the data type and device context
    //   of this graph. Should fix later.
665
    CHECK_EQ(NumBits(), 64);
666
667
668
669
670
671
672
    const dgl_id_t* indptr_data = static_cast<dgl_id_t*>(adj_.indptr->data);
    const dgl_id_t* indices_data = static_cast<dgl_id_t*>(adj_.indices->data);
    const dgl_id_t start = indptr_data[vid];
    const dgl_id_t end = indptr_data[vid + 1];
    return DGLIdIters(indices_data + start, indices_data + end);
  }

673
674
675
676
677
678
679
680
681
682
  DGLIdIters32 SuccVec32(dgl_type_t etype, dgl_id_t vid) {
    // TODO(minjie): This still assumes the data type and device context
    //   of this graph. Should fix later.
    const int32_t* indptr_data = static_cast<int32_t*>(adj_.indptr->data);
    const int32_t* indices_data = static_cast<int32_t*>(adj_.indices->data);
    const int32_t start = indptr_data[vid];
    const int32_t end = indptr_data[vid + 1];
    return DGLIdIters32(indices_data + start, indices_data + end);
  }

683
684
685
  DGLIdIters OutEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
    // TODO(minjie): This still assumes the data type and device context
    //   of this graph. Should fix later.
686
    CHECK_EQ(NumBits(), 64);
687
688
689
690
691
692
693
694
    const dgl_id_t* indptr_data = static_cast<dgl_id_t*>(adj_.indptr->data);
    const dgl_id_t* eid_data = static_cast<dgl_id_t*>(adj_.data->data);
    const dgl_id_t start = indptr_data[vid];
    const dgl_id_t end = indptr_data[vid + 1];
    return DGLIdIters(eid_data + start, eid_data + end);
  }

  DGLIdIters PredVec(dgl_type_t etype, dgl_id_t vid) const override {
695
    LOG(FATAL) << "Not enabled for CSR graph.";
696
697
698
699
    return {};
  }

  DGLIdIters InEdgeVec(dgl_type_t etype, dgl_id_t vid) const override {
700
    LOG(FATAL) << "Not enabled for CSR graph.";
701
702
703
704
705
706
707
708
709
    return {};
  }

  std::vector<IdArray> GetAdj(
      dgl_type_t etype, bool transpose, const std::string &fmt) const override {
    CHECK(!transpose && fmt == "csr") << "Not valid adj format request.";
    return {adj_.indptr, adj_.indices, adj_.data};
  }

710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
  aten::COOMatrix GetCOOMatrix(dgl_type_t etype) const override {
    LOG(FATAL) << "Not enabled for CSR graph";
    return aten::COOMatrix();
  }

  aten::CSRMatrix GetCSCMatrix(dgl_type_t etype) const override {
    LOG(FATAL) << "Not enabled for CSR graph";
    return aten::CSRMatrix();
  }

  aten::CSRMatrix GetCSRMatrix(dgl_type_t etype) const override {
    return adj_;
  }

  SparseFormat SelectFormat(dgl_type_t etype, SparseFormat preferred_format) const override {
    LOG(FATAL) << "Not enabled for CSR graph";
726
    return SparseFormat::kAny;
727
728
  }

729
730
731
732
733
734
735
736
737
738
  std::string GetRestrictFormat() const override {
    LOG(FATAL) << "Not enabled for CSR graph";
    return std::string("");
  }

  dgl_format_code_t GetFormatInUse() const override {
    LOG(FATAL) << "Not enabled for CSR graph";
    return 0;
  }

739
  HeteroSubgraph VertexSubgraph(const std::vector<IdArray>& vids) const override {
Minjie Wang's avatar
Minjie Wang committed
740
741
742
743
    CHECK_EQ(vids.size(), NumVertexTypes()) << "Number of vertex types mismatch";
    auto srcvids = vids[SrcType()], dstvids = vids[DstType()];
    CHECK(aten::IsValidIdArray(srcvids)) << "Invalid vertex id array.";
    CHECK(aten::IsValidIdArray(dstvids)) << "Invalid vertex id array.";
744
    HeteroSubgraph subg;
Minjie Wang's avatar
Minjie Wang committed
745
    const auto& submat = aten::CSRSliceMatrix(adj_, srcvids, dstvids);
746
    IdArray sub_eids = aten::Range(0, submat.data->shape[0], NumBits(), Context());
Minjie Wang's avatar
Minjie Wang committed
747
    subg.graph = std::make_shared<CSR>(meta_graph(), submat.num_rows, submat.num_cols,
748
749
750
751
752
753
754
755
        submat.indptr, submat.indices, sub_eids);
    subg.induced_vertices = vids;
    subg.induced_edges.emplace_back(submat.data);
    return subg;
  }

  HeteroSubgraph EdgeSubgraph(
      const std::vector<IdArray>& eids, bool preserve_nodes = false) const override {
756
    LOG(FATAL) << "Not enabled for CSR graph.";
757
758
759
    return {};
  }

760
761
762
763
764
  HeteroGraphPtr GetGraphInFormat(SparseFormat restrict_format) const override {
    LOG(FATAL) << "Not enabled for CSR graph.";
    return nullptr;
  }

765
766
767
768
  aten::CSRMatrix adj() const {
    return adj_;
  }

769
770
771
772
773
774
775
776
777
778
779
780
781
782
  bool Load(dmlc::Stream* fs) {
    auto meta_imgraph = Serializer::make_shared<ImmutableGraph>();
    CHECK(fs->Read(&meta_imgraph)) << "Invalid meta graph";
    meta_graph_ = meta_imgraph;
    CHECK(fs->Read(&adj_)) << "Invalid adj matrix";
    return true;
  }
  void Save(dmlc::Stream* fs) const {
    auto meta_graph_ptr = ImmutableGraph::ToImmutable(meta_graph());
    fs->Write(meta_graph_ptr);
    fs->Write(adj_);
  }


783
 private:
784
785
786
787
  friend class Serializer;

  CSR() {};

788
789
790
791
792
793
  /*! \brief internal adjacency matrix. Data array stores edge ids */
  aten::CSRMatrix adj_;
};

//////////////////////////////////////////////////////////
//
Minjie Wang's avatar
Minjie Wang committed
794
// unit graph implementation
795
796
797
//
//////////////////////////////////////////////////////////

798
799
800
801
DLDataType UnitGraph::DataType() const {
  return GetAny()->DataType();
}

Minjie Wang's avatar
Minjie Wang committed
802
DLContext UnitGraph::Context() const {
803
804
805
  return GetAny()->Context();
}

Minjie Wang's avatar
Minjie Wang committed
806
uint8_t UnitGraph::NumBits() const {
807
808
809
  return GetAny()->NumBits();
}

Minjie Wang's avatar
Minjie Wang committed
810
bool UnitGraph::IsMultigraph() const {
811
812
813
  return GetAny()->IsMultigraph();
}

Minjie Wang's avatar
Minjie Wang committed
814
uint64_t UnitGraph::NumVertices(dgl_type_t vtype) const {
815
  const SparseFormat fmt = SelectFormat(SparseFormat::kAny);
816
817
818
  const auto ptr = GetFormat(fmt);
  // TODO(BarclayII): we have a lot of special handling for CSC.
  // Need to have a UnitGraph::CSC backend instead.
819
  if (fmt == SparseFormat::kCSC)
Minjie Wang's avatar
Minjie Wang committed
820
    vtype = (vtype == SrcType()) ? DstType() : SrcType();
821
  return ptr->NumVertices(vtype);
822
823
}

Minjie Wang's avatar
Minjie Wang committed
824
uint64_t UnitGraph::NumEdges(dgl_type_t etype) const {
825
826
827
  return GetAny()->NumEdges(etype);
}

Minjie Wang's avatar
Minjie Wang committed
828
bool UnitGraph::HasVertex(dgl_type_t vtype, dgl_id_t vid) const {
829
  const SparseFormat fmt = SelectFormat(SparseFormat::kAny);
830
  const auto ptr = GetFormat(fmt);
831
  if (fmt == SparseFormat::kCSC)
Minjie Wang's avatar
Minjie Wang committed
832
    vtype = (vtype == SrcType()) ? DstType() : SrcType();
833
  return ptr->HasVertex(vtype, vid);
834
835
}

Minjie Wang's avatar
Minjie Wang committed
836
BoolArray UnitGraph::HasVertices(dgl_type_t vtype, IdArray vids) const {
837
  CHECK(aten::IsValidIdArray(vids)) << "Invalid id array input";
838
839
840
  return aten::LT(vids, NumVertices(vtype));
}

Minjie Wang's avatar
Minjie Wang committed
841
bool UnitGraph::HasEdgeBetween(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const {
842
  const SparseFormat fmt = SelectFormat(SparseFormat::kAny);
843
  const auto ptr = GetFormat(fmt);
844
  if (fmt == SparseFormat::kCSC)
845
846
847
    return ptr->HasEdgeBetween(etype, dst, src);
  else
    return ptr->HasEdgeBetween(etype, src, dst);
848
849
}

Minjie Wang's avatar
Minjie Wang committed
850
BoolArray UnitGraph::HasEdgesBetween(
851
    dgl_type_t etype, IdArray src, IdArray dst) const {
852
  const SparseFormat fmt = SelectFormat(SparseFormat::kAny);
853
  const auto ptr = GetFormat(fmt);
854
  if (fmt == SparseFormat::kCSC)
855
856
857
    return ptr->HasEdgesBetween(etype, dst, src);
  else
    return ptr->HasEdgesBetween(etype, src, dst);
858
859
}

Minjie Wang's avatar
Minjie Wang committed
860
IdArray UnitGraph::Predecessors(dgl_type_t etype, dgl_id_t dst) const {
861
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
862
  const auto ptr = GetFormat(fmt);
863
  if (fmt == SparseFormat::kCSC)
864
865
866
    return ptr->Successors(etype, dst);
  else
    return ptr->Predecessors(etype, dst);
867
868
}

Minjie Wang's avatar
Minjie Wang committed
869
IdArray UnitGraph::Successors(dgl_type_t etype, dgl_id_t src) const {
870
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
871
872
  const auto ptr = GetFormat(fmt);
  return ptr->Successors(etype, src);
873
874
}

Minjie Wang's avatar
Minjie Wang committed
875
IdArray UnitGraph::EdgeId(dgl_type_t etype, dgl_id_t src, dgl_id_t dst) const {
876
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
877
  const auto ptr = GetFormat(fmt);
878
  if (fmt == SparseFormat::kCSC)
879
880
881
    return ptr->EdgeId(etype, dst, src);
  else
    return ptr->EdgeId(etype, src, dst);
882
883
}

Minjie Wang's avatar
Minjie Wang committed
884
EdgeArray UnitGraph::EdgeIds(dgl_type_t etype, IdArray src, IdArray dst) const {
885
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
886
  const auto ptr = GetFormat(fmt);
887
  if (fmt == SparseFormat::kCSC) {
888
    EdgeArray edges = ptr->EdgeIds(etype, dst, src);
889
890
    return EdgeArray{edges.dst, edges.src, edges.id};
  } else {
891
    return ptr->EdgeIds(etype, src, dst);
892
893
894
  }
}

Minjie Wang's avatar
Minjie Wang committed
895
std::pair<dgl_id_t, dgl_id_t> UnitGraph::FindEdge(dgl_type_t etype, dgl_id_t eid) const {
896
  const SparseFormat fmt = SelectFormat(SparseFormat::kCOO);
897
898
  const auto ptr = GetFormat(fmt);
  return ptr->FindEdge(etype, eid);
899
900
}

Minjie Wang's avatar
Minjie Wang committed
901
EdgeArray UnitGraph::FindEdges(dgl_type_t etype, IdArray eids) const {
902
  const SparseFormat fmt = SelectFormat(SparseFormat::kCOO);
903
904
  const auto ptr = GetFormat(fmt);
  return ptr->FindEdges(etype, eids);
905
906
}

Minjie Wang's avatar
Minjie Wang committed
907
EdgeArray UnitGraph::InEdges(dgl_type_t etype, dgl_id_t vid) const {
908
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
909
  const auto ptr = GetFormat(fmt);
910
  if (fmt == SparseFormat::kCSC) {
911
912
913
914
915
    const EdgeArray& ret = ptr->OutEdges(etype, vid);
    return {ret.dst, ret.src, ret.id};
  } else {
    return ptr->InEdges(etype, vid);
  }
916
917
}

Minjie Wang's avatar
Minjie Wang committed
918
EdgeArray UnitGraph::InEdges(dgl_type_t etype, IdArray vids) const {
919
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
920
  const auto ptr = GetFormat(fmt);
921
  if (fmt == SparseFormat::kCSC) {
922
923
924
925
926
    const EdgeArray& ret = ptr->OutEdges(etype, vids);
    return {ret.dst, ret.src, ret.id};
  } else {
    return ptr->InEdges(etype, vids);
  }
927
928
}

Minjie Wang's avatar
Minjie Wang committed
929
EdgeArray UnitGraph::OutEdges(dgl_type_t etype, dgl_id_t vid) const {
930
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
931
932
  const auto ptr = GetFormat(fmt);
  return ptr->OutEdges(etype, vid);
933
934
}

Minjie Wang's avatar
Minjie Wang committed
935
EdgeArray UnitGraph::OutEdges(dgl_type_t etype, IdArray vids) const {
936
  const SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
937
938
  const auto ptr = GetFormat(fmt);
  return ptr->OutEdges(etype, vids);
939
940
}

Minjie Wang's avatar
Minjie Wang committed
941
EdgeArray UnitGraph::Edges(dgl_type_t etype, const std::string &order) const {
942
943
  SparseFormat fmt;
  if (order == std::string("eid")) {
944
    fmt = SelectFormat(SparseFormat::kCOO);
945
  } else if (order.empty()) {
946
    // arbitrary order
947
    fmt = SelectFormat(SparseFormat::kAny);
948
  } else if (order == std::string("srcdst")) {
949
    fmt = SelectFormat(SparseFormat::kCSR);
950
951
  } else {
    LOG(FATAL) << "Unsupported order request: " << order;
952
    return {};
953
  }
954
955

  const auto& edges = GetFormat(fmt)->Edges(etype, order);
956
  if (fmt == SparseFormat::kCSC)
957
958
959
    return EdgeArray{edges.dst, edges.src, edges.id};
  else
    return edges;
960
961
}

Minjie Wang's avatar
Minjie Wang committed
962
uint64_t UnitGraph::InDegree(dgl_type_t etype, dgl_id_t vid) const {
963
  SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
964
  const auto ptr = GetFormat(fmt);
965
  if (fmt == SparseFormat::kCSC)
966
967
968
    return ptr->OutDegree(etype, vid);
  else
    return ptr->InDegree(etype, vid);
969
970
}

Minjie Wang's avatar
Minjie Wang committed
971
DegreeArray UnitGraph::InDegrees(dgl_type_t etype, IdArray vids) const {
972
  SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
973
  const auto ptr = GetFormat(fmt);
974
  if (fmt == SparseFormat::kCSC)
975
976
977
    return ptr->OutDegrees(etype, vids);
  else
    return ptr->InDegrees(etype, vids);
978
979
}

Minjie Wang's avatar
Minjie Wang committed
980
uint64_t UnitGraph::OutDegree(dgl_type_t etype, dgl_id_t vid) const {
981
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
982
983
  const auto ptr = GetFormat(fmt);
  return ptr->OutDegree(etype, vid);
984
985
}

Minjie Wang's avatar
Minjie Wang committed
986
DegreeArray UnitGraph::OutDegrees(dgl_type_t etype, IdArray vids) const {
987
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
988
989
  const auto ptr = GetFormat(fmt);
  return ptr->OutDegrees(etype, vids);
990
991
}

Minjie Wang's avatar
Minjie Wang committed
992
DGLIdIters UnitGraph::SuccVec(dgl_type_t etype, dgl_id_t vid) const {
993
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
994
995
  const auto ptr = GetFormat(fmt);
  return ptr->SuccVec(etype, vid);
996
997
}

998
999
1000
1001
1002
1003
1004
DGLIdIters32 UnitGraph::SuccVec32(dgl_type_t etype, dgl_id_t vid) const {
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
  const auto ptr = std::dynamic_pointer_cast<CSR>(GetFormat(fmt));
  CHECK_NOTNULL(ptr);
  return ptr->SuccVec32(etype, vid);
}

Minjie Wang's avatar
Minjie Wang committed
1005
DGLIdIters UnitGraph::OutEdgeVec(dgl_type_t etype, dgl_id_t vid) const {
1006
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
1007
1008
  const auto ptr = GetFormat(fmt);
  return ptr->OutEdgeVec(etype, vid);
1009
1010
}

Minjie Wang's avatar
Minjie Wang committed
1011
DGLIdIters UnitGraph::PredVec(dgl_type_t etype, dgl_id_t vid) const {
1012
  SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
1013
  const auto ptr = GetFormat(fmt);
1014
  if (fmt == SparseFormat::kCSC)
1015
1016
1017
    return ptr->SuccVec(etype, vid);
  else
    return ptr->PredVec(etype, vid);
1018
1019
}

Minjie Wang's avatar
Minjie Wang committed
1020
DGLIdIters UnitGraph::InEdgeVec(dgl_type_t etype, dgl_id_t vid) const {
1021
  SparseFormat fmt = SelectFormat(SparseFormat::kCSC);
1022
  const auto ptr = GetFormat(fmt);
1023
  if (fmt == SparseFormat::kCSC)
1024
1025
1026
    return ptr->OutEdgeVec(etype, vid);
  else
    return ptr->InEdgeVec(etype, vid);
1027
1028
}

Minjie Wang's avatar
Minjie Wang committed
1029
std::vector<IdArray> UnitGraph::GetAdj(
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
    dgl_type_t etype, bool transpose, const std::string &fmt) const {
  // TODO(minjie): Our current semantics of adjacency matrix is row for dst nodes and col for
  //   src nodes. Therefore, we need to flip the transpose flag. For example, transpose=False
  //   is equal to in edge CSR.
  //   We have this behavior because previously we use framework's SPMM and we don't cache
  //   reverse adj. This is not intuitive and also not consistent with networkx's
  //   to_scipy_sparse_matrix. With the upcoming custom kernel change, we should change the
  //   behavior and make row for src and col for dst.
  if (fmt == std::string("csr")) {
    return transpose? GetOutCSR()->GetAdj(etype, false, "csr")
      : GetInCSR()->GetAdj(etype, false, "csr");
  } else if (fmt == std::string("coo")) {
    return GetCOO()->GetAdj(etype, !transpose, fmt);
  } else {
    LOG(FATAL) << "unsupported adjacency matrix format: " << fmt;
    return {};
  }
}

Minjie Wang's avatar
Minjie Wang committed
1049
HeteroSubgraph UnitGraph::VertexSubgraph(const std::vector<IdArray>& vids) const {
1050
  // We prefer to generate a subgraph from out-csr.
1051
  SparseFormat fmt = SelectFormat(SparseFormat::kCSR);
1052
  HeteroSubgraph sg = GetFormat(fmt)->VertexSubgraph(vids);
1053
  HeteroSubgraph ret;
Quan (Andy) Gan's avatar
Quan (Andy) Gan committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073

  CSRPtr subcsr = nullptr;
  CSRPtr subcsc = nullptr;
  COOPtr subcoo = nullptr;
  switch (fmt) {
    case SparseFormat::kCSR:
      subcsr = std::dynamic_pointer_cast<CSR>(sg.graph);
      break;
    case SparseFormat::kCSC:
      subcsc = std::dynamic_pointer_cast<CSR>(sg.graph);
      break;
    case SparseFormat::kCOO:
      subcoo = std::dynamic_pointer_cast<COO>(sg.graph);
      break;
    default:
      LOG(FATAL) << "[BUG] unsupported format " << static_cast<int>(fmt);
      return ret;
  }

  ret.graph = HeteroGraphPtr(new UnitGraph(meta_graph(), subcsc, subcsr, subcoo));
1074
1075
1076
1077
1078
  ret.induced_vertices = std::move(sg.induced_vertices);
  ret.induced_edges = std::move(sg.induced_edges);
  return ret;
}

Minjie Wang's avatar
Minjie Wang committed
1079
HeteroSubgraph UnitGraph::EdgeSubgraph(
1080
    const std::vector<IdArray>& eids, bool preserve_nodes) const {
1081
  SparseFormat fmt = SelectFormat(SparseFormat::kCOO);
1082
  auto sg = GetFormat(fmt)->EdgeSubgraph(eids, preserve_nodes);
1083
  HeteroSubgraph ret;
Quan (Andy) Gan's avatar
Quan (Andy) Gan committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103

  CSRPtr subcsr = nullptr;
  CSRPtr subcsc = nullptr;
  COOPtr subcoo = nullptr;
  switch (fmt) {
    case SparseFormat::kCSR:
      subcsr = std::dynamic_pointer_cast<CSR>(sg.graph);
      break;
    case SparseFormat::kCSC:
      subcsc = std::dynamic_pointer_cast<CSR>(sg.graph);
      break;
    case SparseFormat::kCOO:
      subcoo = std::dynamic_pointer_cast<COO>(sg.graph);
      break;
    default:
      LOG(FATAL) << "[BUG] unsupported format " << static_cast<int>(fmt);
      return ret;
  }

  ret.graph = HeteroGraphPtr(new UnitGraph(meta_graph(), subcsc, subcsr, subcoo));
1104
1105
1106
1107
1108
  ret.induced_vertices = std::move(sg.induced_vertices);
  ret.induced_edges = std::move(sg.induced_edges);
  return ret;
}

Minjie Wang's avatar
Minjie Wang committed
1109
HeteroGraphPtr UnitGraph::CreateFromCOO(
1110
1111
    int64_t num_vtypes, int64_t num_src, int64_t num_dst,
    IdArray row, IdArray col,
1112
    SparseFormat restrict_format) {
Minjie Wang's avatar
Minjie Wang committed
1113
1114
1115
1116
1117
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(num_src, num_dst);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  COOPtr coo(new COO(mg, num_src, num_dst, row, col));
1118
1119
1120

  return HeteroGraphPtr(
      new UnitGraph(mg, nullptr, nullptr, coo, restrict_format));
1121
1122
}

1123
1124
1125
1126
1127
1128
1129
1130
HeteroGraphPtr UnitGraph::CreateFromCOO(
    int64_t num_vtypes, const aten::COOMatrix& mat,
    SparseFormat restrict_format) {
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(mat.num_rows, mat.num_cols);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  COOPtr coo(new COO(mg, mat));
1131

1132
1133
1134
1135
  return HeteroGraphPtr(
      new UnitGraph(mg, nullptr, nullptr, coo, restrict_format));
}

Minjie Wang's avatar
Minjie Wang committed
1136
1137
HeteroGraphPtr UnitGraph::CreateFromCSR(
    int64_t num_vtypes, int64_t num_src, int64_t num_dst,
1138
    IdArray indptr, IdArray indices, IdArray edge_ids, SparseFormat restrict_format) {
Minjie Wang's avatar
Minjie Wang committed
1139
1140
1141
1142
1143
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(num_src, num_dst);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  CSRPtr csr(new CSR(mg, num_src, num_dst, indptr, indices, edge_ids));
1144
  return HeteroGraphPtr(new UnitGraph(mg, nullptr, csr, nullptr, restrict_format));
1145
1146
}

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
HeteroGraphPtr UnitGraph::CreateFromCSR(
    int64_t num_vtypes, const aten::CSRMatrix& mat,
    SparseFormat restrict_format) {
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(mat.num_rows, mat.num_cols);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  CSRPtr csr(new CSR(mg, mat));
  return HeteroGraphPtr(new UnitGraph(mg, nullptr, csr, nullptr, restrict_format));
}

1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
HeteroGraphPtr UnitGraph::CreateFromCSC(
    int64_t num_vtypes, int64_t num_src, int64_t num_dst,
    IdArray indptr, IdArray indices, IdArray edge_ids, SparseFormat restrict_format) {
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(num_src, num_dst);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  CSRPtr csc(new CSR(mg, num_src, num_dst, indptr, indices, edge_ids));
  return HeteroGraphPtr(new UnitGraph(mg, csc, nullptr, nullptr, restrict_format));
}

HeteroGraphPtr UnitGraph::CreateFromCSC(
    int64_t num_vtypes, const aten::CSRMatrix& mat,
    SparseFormat restrict_format) {
  CHECK(num_vtypes == 1 || num_vtypes == 2);
  if (num_vtypes == 1)
    CHECK_EQ(mat.num_rows, mat.num_cols);
  auto mg = CreateUnitGraphMetaGraph(num_vtypes);
  CSRPtr csc(new CSR(mg, mat));
  return HeteroGraphPtr(new UnitGraph(mg, csc, nullptr, nullptr, restrict_format));
}

Minjie Wang's avatar
Minjie Wang committed
1180
HeteroGraphPtr UnitGraph::AsNumBits(HeteroGraphPtr g, uint8_t bits) {
1181
1182
1183
1184
1185
1186
1187
  if (g->NumBits() == bits) {
    return g;
  } else {
    // TODO(minjie): since we don't have int32 operations,
    //   we make sure that this graph (on CPU) has materialized CSR,
    //   and then copy them to other context (usually GPU). This should
    //   be fixed later.
Minjie Wang's avatar
Minjie Wang committed
1188
    auto bg = std::dynamic_pointer_cast<UnitGraph>(g);
1189
    CHECK_NOTNULL(bg);
1190

1191
1192
    CSRPtr new_incsr = CSRPtr(new CSR(bg->GetInCSR()->AsNumBits(bits)));
    CSRPtr new_outcsr = CSRPtr(new CSR(bg->GetOutCSR()->AsNumBits(bits)));
1193
1194
    return HeteroGraphPtr(
        new UnitGraph(g->meta_graph(), new_incsr, new_outcsr, nullptr, bg->restrict_format_));
1195
1196
1197
  }
}

Minjie Wang's avatar
Minjie Wang committed
1198
HeteroGraphPtr UnitGraph::CopyTo(HeteroGraphPtr g, const DLContext& ctx) {
1199
1200
1201
1202
1203
1204
1205
  if (ctx == g->Context()) {
    return g;
  }
  // TODO(minjie): since we don't have GPU implementation of COO<->CSR,
  //   we make sure that this graph (on CPU) has materialized CSR,
  //   and then copy them to other context (usually GPU). This should
  //   be fixed later.
Minjie Wang's avatar
Minjie Wang committed
1206
  auto bg = std::dynamic_pointer_cast<UnitGraph>(g);
1207
  CHECK_NOTNULL(bg);
1208

1209
1210
  CSRPtr new_incsr = CSRPtr(new CSR(bg->GetInCSR()->CopyTo(ctx)));
  CSRPtr new_outcsr = CSRPtr(new CSR(bg->GetOutCSR()->CopyTo(ctx)));
1211
1212
  return HeteroGraphPtr(
      new UnitGraph(g->meta_graph(), new_incsr, new_outcsr, nullptr, bg->restrict_format_));
1213
1214
}

1215
1216
UnitGraph::UnitGraph(GraphPtr metagraph, CSRPtr in_csr, CSRPtr out_csr, COOPtr coo,
                     SparseFormat restrict_format)
Minjie Wang's avatar
Minjie Wang committed
1217
  : BaseHeteroGraph(metagraph), in_csr_(in_csr), out_csr_(out_csr), coo_(coo) {
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  restrict_format_ = AutoDetectFormat(in_csr, out_csr, coo, restrict_format);
  switch (restrict_format) {
  case SparseFormat::kCSC:
    in_csr_ = GetInCSR();
    coo_ = nullptr;
    out_csr_ = nullptr;
    break;
  case SparseFormat::kCSR:
    out_csr_ = GetOutCSR();
    coo_ = nullptr;
    in_csr_ = nullptr;
    break;
  case SparseFormat::kCOO:
    coo_ = GetCOO();
    in_csr_ = nullptr;
    out_csr_ = nullptr;
    break;
  default:
    break;
1237
1238
  }

1239
1240
1241
  CHECK(GetAny()) << "At least one graph structure should exist.";
}

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
HeteroGraphPtr UnitGraph::CreateHomographFrom(
    const aten::CSRMatrix &in_csr,
    const aten::CSRMatrix &out_csr,
    const aten::COOMatrix &coo,
    bool has_in_csr,
    bool has_out_csr,
    bool has_coo,
    SparseFormat restrict_format) {
  auto mg = CreateUnitGraphMetaGraph1();

  CSRPtr in_csr_ptr = nullptr;
  CSRPtr out_csr_ptr = nullptr;
  COOPtr coo_ptr = nullptr;

  if (has_in_csr)
    in_csr_ptr = CSRPtr(new CSR(mg, in_csr));
  if (has_out_csr)
    out_csr_ptr = CSRPtr(new CSR(mg, out_csr));
  if (has_coo)
    coo_ptr = COOPtr(new COO(mg, coo));

  return HeteroGraphPtr(new UnitGraph(mg, in_csr_ptr, out_csr_ptr, coo_ptr, restrict_format));
}

1266
1267
1268
1269
1270
1271
1272
UnitGraph::CSRPtr UnitGraph::GetInCSR(bool inplace) const {
  if (inplace)
    if (restrict_format_ != SparseFormat::kAny &&
        restrict_format_ != SparseFormat::kCSC)
      LOG(FATAL) << "The graph have restricted sparse format " << GetRestrictFormat() <<
        ", cannot create CSC matrix.";
  CSRPtr ret = in_csr_;
1273
1274
1275
  if (!in_csr_) {
    if (out_csr_) {
      const auto& newadj = aten::CSRTranspose(out_csr_->adj());
1276
1277
1278
      ret = std::make_shared<CSR>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->in_csr_ = ret;
1279
1280
1281
1282
1283
    } else {
      CHECK(coo_) << "None of CSR, COO exist";
      const auto& adj = coo_->adj();
      const auto& newadj = aten::COOToCSR(
          aten::COOMatrix{adj.num_cols, adj.num_rows, adj.col, adj.row});
1284
1285
1286
      ret = std::make_shared<CSR>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->in_csr_ = ret;
1287
1288
    }
  }
1289
  return ret;
1290
1291
1292
}

/* !\brief Return out csr. If not exist, transpose the other one.*/
1293
1294
1295
1296
1297
1298
1299
UnitGraph::CSRPtr UnitGraph::GetOutCSR(bool inplace) const {
  if (inplace)
    if (restrict_format_ != SparseFormat::kAny &&
        restrict_format_ != SparseFormat::kCSR)
      LOG(FATAL) << "The graph have restricted sparse format " << GetRestrictFormat() <<
        ", cannot create CSR matrix.";
  CSRPtr ret = out_csr_;
1300
1301
1302
  if (!out_csr_) {
    if (in_csr_) {
      const auto& newadj = aten::CSRTranspose(in_csr_->adj());
1303
1304
1305
      ret = std::make_shared<CSR>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->out_csr_ = ret;
1306
1307
1308
    } else {
      CHECK(coo_) << "None of CSR, COO exist";
      const auto& newadj = aten::COOToCSR(coo_->adj());
1309
1310
1311
      ret = std::make_shared<CSR>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->out_csr_ = ret;
1312
1313
    }
  }
1314
  return ret;
1315
1316
1317
}

/* !\brief Return coo. If not exist, create from csr.*/
1318
1319
1320
1321
1322
1323
1324
UnitGraph::COOPtr UnitGraph::GetCOO(bool inplace) const {
  if (inplace)
    if (restrict_format_ != SparseFormat::kAny &&
        restrict_format_ != SparseFormat::kCOO)
      LOG(FATAL) << "The graph have restricted sparse format " << GetRestrictFormat() <<
        ", cannot create COO matrix.";
  COOPtr ret = coo_;
1325
1326
  if (!coo_) {
    if (in_csr_) {
1327
      const auto& newadj = aten::COOTranspose(aten::CSRToCOO(in_csr_->adj(), true));
1328
1329
1330
      ret = std::make_shared<COO>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->coo_ = ret;
1331
1332
1333
    } else {
      CHECK(out_csr_) << "Both CSR are missing.";
      const auto& newadj = aten::CSRToCOO(out_csr_->adj(), true);
1334
1335
1336
      ret = std::make_shared<COO>(meta_graph(), newadj);
      if (inplace)
        const_cast<UnitGraph*>(this)->coo_ = ret;
1337
1338
    }
  }
1339
  return ret;
1340
1341
}

1342
aten::CSRMatrix UnitGraph::GetCSCMatrix(dgl_type_t etype) const {
1343
1344
1345
  return GetInCSR()->adj();
}

1346
aten::CSRMatrix UnitGraph::GetCSRMatrix(dgl_type_t etype) const {
1347
1348
1349
  return GetOutCSR()->adj();
}

1350
aten::COOMatrix UnitGraph::GetCOOMatrix(dgl_type_t etype) const {
1351
1352
1353
  return GetCOO()->adj();
}

Minjie Wang's avatar
Minjie Wang committed
1354
HeteroGraphPtr UnitGraph::GetAny() const {
1355
1356
1357
1358
1359
1360
1361
1362
1363
  if (in_csr_) {
    return in_csr_;
  } else if (out_csr_) {
    return out_csr_;
  } else {
    return coo_;
  }
}

1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
dgl_format_code_t UnitGraph::GetFormatInUse() const {
  dgl_format_code_t ret = 0;
  if (in_csr_) ret = ret | 1;
  ret = ret << 1;
  if (out_csr_) ret = ret | 1;
  ret = ret << 1;
  if (coo_) ret = ret | 1;
  return ret;
}

1374
1375
HeteroGraphPtr UnitGraph::GetFormat(SparseFormat format) const {
  switch (format) {
1376
1377
1378
1379
1380
1381
1382
1383
  case SparseFormat::kCSR:
    return GetOutCSR();
  case SparseFormat::kCSC:
    return GetInCSR();
  case SparseFormat::kCOO:
    return GetCOO();
  case SparseFormat::kAny:
    return GetAny();
1384
1385
  default:  // SparseFormat::kAuto
    LOG(FATAL) << "Must specify a restrict format.";
1386
    return nullptr;
1387
1388
1389
  }
}

1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
HeteroGraphPtr UnitGraph::GetGraphInFormat(SparseFormat restrict_format) const {
    int64_t num_vtypes = NumVertexTypes();
    switch (restrict_format) {
    case SparseFormat::kCOO:
      return CreateFromCOO(
        num_vtypes, GetCOO(false)->adj(), restrict_format);
    case SparseFormat::kCSC:
      return CreateFromCSC(
        num_vtypes, GetInCSR(false)->adj(), restrict_format);
    case SparseFormat::kCSR:
      return CreateFromCSR(
        num_vtypes, GetOutCSR(false)->adj(), restrict_format);
    case SparseFormat::kAny:
      return HeteroGraphPtr(
        new UnitGraph(meta_graph_, in_csr_, out_csr_, coo_, restrict_format));
    default:  // SparseFormat::kAuto
      LOG(FATAL) << "Must specify a restrict format.";
      return nullptr;
  }
}

SparseFormat UnitGraph::AutoDetectFormat(
    CSRPtr in_csr, CSRPtr out_csr, COOPtr coo, SparseFormat restrict_format) const {
  if (restrict_format != SparseFormat::kAuto)
    return restrict_format;
  if (coo && coo->IsHypersparse())
    return SparseFormat::kCOO;
  return SparseFormat::kAny;
}

1420
SparseFormat UnitGraph::SelectFormat(SparseFormat preferred_format) const {
1421
  if (restrict_format_ != SparseFormat::kAny)
1422
    return restrict_format_;  // force to select the restricted format
1423
  else if (preferred_format != SparseFormat::kAny)
1424
1425
    return preferred_format;
  else if (in_csr_)
1426
    return SparseFormat::kCSC;
1427
  else if (out_csr_)
1428
    return SparseFormat::kCSR;
1429
  else
1430
    return SparseFormat::kCOO;
1431
1432
}

1433
1434
1435
1436
1437
1438
GraphPtr UnitGraph::AsImmutableGraph() const {
  CHECK(NumVertexTypes() == 1) << "not a homogeneous graph";
  dgl::CSRPtr in_csr_ptr = nullptr, out_csr_ptr = nullptr;
  dgl::COOPtr coo_ptr = nullptr;
  if (in_csr_) {
    aten::CSRMatrix csc = GetCSCMatrix(0);
1439
    in_csr_ptr = dgl::CSRPtr(new dgl::CSR(csc.indptr, csc.indices, csc.data));
1440
1441
1442
  }
  if (out_csr_) {
    aten::CSRMatrix csr = GetCSRMatrix(0);
1443
    out_csr_ptr = dgl::CSRPtr(new dgl::CSR(csr.indptr, csr.indices, csr.data));
1444
1445
1446
1447
  }
  if (coo_) {
    aten::COOMatrix coo = GetCOOMatrix(0);
    if (!COOHasData(coo)) {
1448
      coo_ptr = dgl::COOPtr(new dgl::COO(NumVertices(0), coo.row, coo.col));
1449
1450
1451
    } else {
      IdArray new_src = Scatter(coo.row, coo.data);
      IdArray new_dst = Scatter(coo.col, coo.data);
1452
      coo_ptr = dgl::COOPtr(new dgl::COO(NumVertices(0), new_src, new_dst));
1453
1454
1455
1456
1457
    }
  }
  return GraphPtr(new dgl::ImmutableGraph(in_csr_ptr, out_csr_ptr, coo_ptr));
}

1458
1459
1460
1461
1462
1463
constexpr uint64_t kDGLSerialize_UnitGraphMagic = 0xDD2E60F0F6B4A127;

bool UnitGraph::Load(dmlc::Stream* fs) {
  uint64_t magicNum;
  CHECK(fs->Read(&magicNum)) << "Invalid Magic Number";
  CHECK_EQ(magicNum, kDGLSerialize_UnitGraphMagic) << "Invalid UnitGraph Data";
1464
1465
1466
1467
1468
1469

  int64_t format_code;
  CHECK(fs->Read(&format_code)) << "Invalid format";
  restrict_format_ = static_cast<SparseFormat>(format_code);

  switch (restrict_format_) {
1470
    case SparseFormat::kCOO:
1471
1472
      fs->Read(&coo_);
      break;
1473
    case SparseFormat::kCSR:
1474
1475
      fs->Read(&out_csr_);
      break;
1476
    case SparseFormat::kCSC:
1477
1478
1479
1480
1481
1482
1483
1484
1485
      fs->Read(&in_csr_);
      break;
    default:
      LOG(FATAL) << "unsupported format code";
      break;
  }

  meta_graph_ = GetAny()->meta_graph();

1486
1487
1488
  return true;
}

1489

1490
1491
void UnitGraph::Save(dmlc::Stream* fs) const {
  fs->Write(kDGLSerialize_UnitGraphMagic);
1492
1493
  // Didn't write UnitGraph::meta_graph_, since it's included in the underlying
  // sparse matrix
1494
  auto avail_fmt = SelectFormat(SparseFormat::kAny);
1495
1496
  fs->Write(static_cast<int64_t>(avail_fmt));
  switch (avail_fmt) {
1497
    case SparseFormat::kCOO:
1498
1499
      fs->Write(GetCOO());
      break;
1500
    case SparseFormat::kCSR:
1501
1502
      fs->Write(GetOutCSR());
      break;
1503
    case SparseFormat::kCSC:
1504
1505
1506
1507
1508
1509
      fs->Write(GetInCSR());
      break;
    default:
      LOG(FATAL) << "unsupported format code";
      break;
  }
1510
1511
}

1512
}  // namespace dgl