nodeflow.cc 3.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
 *  Copyright (c) 2019 by Contributors
 * \file graph/nodeflow.cc
 * \brief DGL NodeFlow related functions.
 */

#include <dgl/immutable_graph.h>
#include <dgl/nodeflow.h>

#include <string.h>

#include "../c_api_common.h"

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

19
20
21
22
23
24
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);
25
  if (fmt == std::string("csr")) {
26
    dgl_id_t first_vid = layer1_start - layer0_size;
27
    CSRMatrix csr = SliceRows(graph.GetInCSR()->ToCSRMatrix(), layer1_start, layer1_end);
28
    if (remap) {
29
30
31
32
33
34
35
      dgl_id_t *eid_data = static_cast<dgl_id_t*>(csr.data->data);
      const dgl_id_t first_eid = eid_data[0];
      IdArray new_indices = Sub(csr.indices, first_vid);
      IdArray new_data = Sub(csr.data, first_eid);
      return {csr.indptr, new_indices, new_data};
    } else {
      return {csr.indptr, csr.indices, csr.data};
36
    }
37
38
39
40
41
42
43
44
  } else if (fmt == std::string("coo")) {
    CSRMatrix csr = graph.GetInCSR()->ToCSRMatrix();
    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];
    IdArray idx = NewIdArray(2 * nnz);
    IdArray eid = NewIdArray(nnz);
45
46
47
48
    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++) {
49
      for (dgl_id_t j = indptr[i]; j < indptr[i + 1]; j++) {
50
51
52
53
54
55
56
57
        // 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) {
58
59
      size_t edge_start = indptr[layer1_start];
      dgl_id_t first_eid = edge_ids[edge_start];
60
61
      dgl_id_t first_vid = layer1_start - layer0_size;
      for (int64_t i = 0; i < nnz; i++) {
62
63
64
        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;
65
66
      }
    } else {
67
68
69
70
      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);
71
72
73
74
    }
    return std::vector<IdArray>{idx, eid};
  } else {
    LOG(FATAL) << "unsupported adjacency matrix format";
75
    return {};
76
77
78
  }
}

79
80
81
82
83
84
85
DGL_REGISTER_GLOBAL("nodeflow._CAPI_NodeFlowGetBlockAdj")
.set_body([] (DGLArgs args, DGLRetValue* rv) {
    GraphHandle ghandle = args[0];
    std::string format = args[1];
    int64_t layer0_size = args[2];
    int64_t start = args[3];
    int64_t end = args[4];
86
    const bool remap = args[5];
87
88
    const GraphInterface *ptr = static_cast<const GraphInterface *>(ghandle);
    const ImmutableGraph* gptr = dynamic_cast<const ImmutableGraph*>(ptr);
89
    auto res = GetNodeFlowSlice(*gptr, format, layer0_size, start, end, remap);
90
91
92
    *rv = ConvertNDArrayVectorToPackedFunc(res);
  });

93
}  // namespace dgl