nodeflow.cc 3.41 KB
Newer Older
1
2
3
4
5
6
7
/*!
 *  Copyright (c) 2019 by Contributors
 * \file graph/nodeflow.cc
 * \brief DGL NodeFlow related functions.
 */

#include <dgl/immutable_graph.h>
8
#include <dgl/packed_func_ext.h>
9
10
#include <dgl/nodeflow.h>

11
#include <string>
12
13
14

#include "../c_api_common.h"

15
16
17
18
19
using dgl::runtime::DGLArgs;
using dgl::runtime::DGLArgValue;
using dgl::runtime::DGLRetValue;
using dgl::runtime::PackedFunc;

20
21
22
23
24
25
namespace dgl {

std::vector<IdArray> GetNodeFlowSlice(const ImmutableGraph &graph, const std::string &fmt,
                                      size_t layer0_size, size_t layer1_start,
                                      size_t layer1_end, bool remap) {
  CHECK_GE(layer1_start, layer0_size);
26
  if (fmt == std::string("csr")) {
27
    dgl_id_t first_vid = layer1_start - layer0_size;
28
    auto csr = aten::CSRSliceRows(graph.GetInCSR()->ToCSRMatrix(), layer1_start, layer1_end);
29
    if (remap) {
30
31
      dgl_id_t *eid_data = static_cast<dgl_id_t*>(csr.data->data);
      const dgl_id_t first_eid = eid_data[0];
32
33
      IdArray new_indices = aten::Sub(csr.indices, first_vid);
      IdArray new_data = aten::Sub(csr.data, first_eid);
34
35
36
      return {csr.indptr, new_indices, new_data};
    } else {
      return {csr.indptr, csr.indices, csr.data};
37
    }
38
  } else if (fmt == std::string("coo")) {
39
    auto csr = graph.GetInCSR()->ToCSRMatrix();
40
41
42
43
    const dgl_id_t* indptr = static_cast<dgl_id_t*>(csr.indptr->data);
    const dgl_id_t* indices = static_cast<dgl_id_t*>(csr.indices->data);
    const dgl_id_t* edge_ids = static_cast<dgl_id_t*>(csr.data->data);
    int64_t nnz = indptr[layer1_end] - indptr[layer1_start];
44
45
    IdArray idx = aten::NewIdArray(2 * nnz);
    IdArray eid = aten::NewIdArray(nnz);
46
47
48
49
    int64_t *idx_data = static_cast<int64_t*>(idx->data);
    dgl_id_t *eid_data = static_cast<dgl_id_t*>(eid->data);
    size_t num_edges = 0;
    for (size_t i = layer1_start; i < layer1_end; i++) {
50
      for (dgl_id_t j = indptr[i]; j < indptr[i + 1]; j++) {
51
52
53
54
55
56
57
58
        // These nodes are all in a layer. We need to remap them to the node id
        // local to the layer.
        idx_data[num_edges] = remap ? i - layer1_start : i;
        num_edges++;
      }
    }
    CHECK_EQ(num_edges, nnz);
    if (remap) {
59
60
      size_t edge_start = indptr[layer1_start];
      dgl_id_t first_eid = edge_ids[edge_start];
61
62
      dgl_id_t first_vid = layer1_start - layer0_size;
      for (int64_t i = 0; i < nnz; i++) {
63
64
65
        CHECK_GE(indices[edge_start + i], first_vid);
        idx_data[nnz + i] = indices[edge_start + i] - first_vid;
        eid_data[i] = edge_ids[edge_start + i] - first_eid;
66
67
      }
    } else {
68
69
70
71
      std::copy(indices + indptr[layer1_start],
                indices + indptr[layer1_end], idx_data + nnz);
      std::copy(edge_ids + indptr[layer1_start],
                edge_ids + indptr[layer1_end], eid_data);
72
73
74
75
    }
    return std::vector<IdArray>{idx, eid};
  } else {
    LOG(FATAL) << "unsupported adjacency matrix format";
76
    return {};
77
78
79
  }
}

80
DGL_REGISTER_GLOBAL("_deprecate.nodeflow._CAPI_NodeFlowGetBlockAdj")
81
.set_body([] (DGLArgs args, DGLRetValue* rv) {
82
    GraphRef g = args[0];
83
84
85
86
    std::string format = args[1];
    int64_t layer0_size = args[2];
    int64_t start = args[3];
    int64_t end = args[4];
87
    const bool remap = args[5];
88
89
    auto ig = CHECK_NOTNULL(std::dynamic_pointer_cast<ImmutableGraph>(g.sptr()));
    auto res = GetNodeFlowSlice(*ig, format, layer0_size, start, end, remap);
90
91
92
    *rv = ConvertNDArrayVectorToPackedFunc(res);
  });

93
}  // namespace dgl