Unverified Commit fea5579a authored by GaiYu0's avatar GaiYu0 Committed by GitHub
Browse files

[GraphIndex][Fix] signed and unsigned integer conversion (#359)

* signed and unsigned integer conversion

* replace long with uint64_t

* replace uint64_t with int64_t

* including cmath; gcc 8 crashes otherwise
parent 929742b5
...@@ -56,7 +56,7 @@ class ImmutableGraph: public GraphInterface { ...@@ -56,7 +56,7 @@ class ImmutableGraph: public GraphInterface {
return indices.size(); return indices.size();
} }
int64_t GetDegree(dgl_id_t vid) const { uint64_t GetDegree(dgl_id_t vid) const {
return indptr[vid + 1] - indptr[vid]; return indptr[vid + 1] - indptr[vid];
} }
DegreeArray GetDegrees(IdArray vids) const; DegreeArray GetDegrees(IdArray vids) const;
...@@ -81,7 +81,7 @@ class ImmutableGraph: public GraphInterface { ...@@ -81,7 +81,7 @@ class ImmutableGraph: public GraphInterface {
* we simply sort on the input edge list. We allow sorting on both end points of an edge, * we simply sort on the input edge list. We allow sorting on both end points of an edge,
* which is specified by `sort_on`. * which is specified by `sort_on`.
*/ */
static CSR::Ptr FromEdges(std::vector<Edge> *edges, int sort_on, int64_t num_nodes); static CSR::Ptr FromEdges(std::vector<Edge> *edges, int sort_on, uint64_t num_nodes);
}; };
/*! \brief Construct an immutable graph from the COO format. */ /*! \brief Construct an immutable graph from the COO format. */
......
...@@ -24,7 +24,7 @@ DLManagedTensor* CreateTmpDLManagedTensor(const DGLArgValue& arg) { ...@@ -24,7 +24,7 @@ DLManagedTensor* CreateTmpDLManagedTensor(const DGLArgValue& arg) {
PackedFunc ConvertNDArrayVectorToPackedFunc(const std::vector<NDArray>& vec) { PackedFunc ConvertNDArrayVectorToPackedFunc(const std::vector<NDArray>& vec) {
auto body = [vec](DGLArgs args, DGLRetValue* rv) { auto body = [vec](DGLArgs args, DGLRetValue* rv) {
const int which = args[0]; const size_t which = args[0];
if (which >= vec.size()) { if (which >= vec.size()) {
LOG(FATAL) << "invalid choice"; LOG(FATAL) << "invalid choice";
} else { } else {
......
...@@ -20,14 +20,14 @@ Graph::Graph(IdArray src_ids, IdArray dst_ids, IdArray edge_ids, size_t num_node ...@@ -20,14 +20,14 @@ Graph::Graph(IdArray src_ids, IdArray dst_ids, IdArray edge_ids, size_t num_node
CHECK(IsValidIdArray(edge_ids)); CHECK(IsValidIdArray(edge_ids));
this->AddVertices(num_nodes); this->AddVertices(num_nodes);
num_edges_ = src_ids->shape[0]; num_edges_ = src_ids->shape[0];
CHECK(num_edges_ == dst_ids->shape[0]) << "vectors in COO must have the same length"; CHECK(static_cast<int64_t>(num_edges_) == dst_ids->shape[0]) << "vectors in COO must have the same length";
CHECK(num_edges_ == edge_ids->shape[0]) << "vectors in COO must have the same length"; CHECK(static_cast<int64_t>(num_edges_) == edge_ids->shape[0]) << "vectors in COO must have the same length";
const dgl_id_t *src_data = static_cast<dgl_id_t*>(src_ids->data); const dgl_id_t *src_data = static_cast<dgl_id_t*>(src_ids->data);
const dgl_id_t *dst_data = static_cast<dgl_id_t*>(dst_ids->data); const dgl_id_t *dst_data = static_cast<dgl_id_t*>(dst_ids->data);
const dgl_id_t *edge_data = static_cast<dgl_id_t*>(edge_ids->data); const dgl_id_t *edge_data = static_cast<dgl_id_t*>(edge_ids->data);
all_edges_src_.reserve(num_edges_); all_edges_src_.reserve(num_edges_);
all_edges_dst_.reserve(num_edges_); all_edges_dst_.reserve(num_edges_);
for (int64_t i = 0; i < num_edges_; i++) { for (uint64_t i = 0; i < num_edges_; i++) {
auto src = src_data[i]; auto src = src_data[i];
auto dst = dst_data[i]; auto dst = dst_data[i];
auto eid = edge_data[i]; auto eid = edge_data[i];
...@@ -504,10 +504,10 @@ Subgraph Graph::EdgeSubgraph(IdArray eids) const { ...@@ -504,10 +504,10 @@ Subgraph Graph::EdgeSubgraph(IdArray eids) const {
} }
std::vector<IdArray> Graph::GetAdj(bool transpose, const std::string &fmt) const { std::vector<IdArray> Graph::GetAdj(bool transpose, const std::string &fmt) const {
int64_t num_edges = NumEdges(); uint64_t num_edges = NumEdges();
int64_t num_nodes = NumVertices(); uint64_t num_nodes = NumVertices();
if (fmt == "coo") { if (fmt == "coo") {
IdArray idx = IdArray::Empty({2 * num_edges}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); IdArray idx = IdArray::Empty({2 * static_cast<int64_t>(num_edges)}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
int64_t *idx_data = static_cast<int64_t*>(idx->data); int64_t *idx_data = static_cast<int64_t*>(idx->data);
if (transpose) { if (transpose) {
std::copy(all_edges_src_.begin(), all_edges_src_.end(), idx_data); std::copy(all_edges_src_.begin(), all_edges_src_.end(), idx_data);
...@@ -516,17 +516,17 @@ std::vector<IdArray> Graph::GetAdj(bool transpose, const std::string &fmt) const ...@@ -516,17 +516,17 @@ std::vector<IdArray> Graph::GetAdj(bool transpose, const std::string &fmt) const
std::copy(all_edges_dst_.begin(), all_edges_dst_.end(), idx_data); std::copy(all_edges_dst_.begin(), all_edges_dst_.end(), idx_data);
std::copy(all_edges_src_.begin(), all_edges_src_.end(), idx_data + num_edges); std::copy(all_edges_src_.begin(), all_edges_src_.end(), idx_data + num_edges);
} }
IdArray eid = IdArray::Empty({num_edges}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); IdArray eid = IdArray::Empty({static_cast<int64_t>(num_edges)}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
int64_t *eid_data = static_cast<int64_t*>(eid->data); int64_t *eid_data = static_cast<int64_t*>(eid->data);
for (uint64_t eid = 0; eid < num_edges; ++eid) { for (uint64_t eid = 0; eid < num_edges; ++eid) {
eid_data[eid] = eid; eid_data[eid] = eid;
} }
return std::vector<IdArray>{idx, eid}; return std::vector<IdArray>{idx, eid};
} else if (fmt == "csr") { } else if (fmt == "csr") {
IdArray indptr = IdArray::Empty({num_nodes + 1}, DLDataType{kDLInt, 64, 1}, IdArray indptr = IdArray::Empty({static_cast<int64_t>(num_nodes) + 1}, DLDataType{kDLInt, 64, 1},
DLContext{kDLCPU, 0}); DLContext{kDLCPU, 0});
IdArray indices = IdArray::Empty({num_edges}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); IdArray indices = IdArray::Empty({static_cast<int64_t>(num_edges)}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
IdArray eid = IdArray::Empty({num_edges}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); IdArray eid = IdArray::Empty({static_cast<int64_t>(num_edges)}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
int64_t *indptr_data = static_cast<int64_t*>(indptr->data); int64_t *indptr_data = static_cast<int64_t*>(indptr->data);
int64_t *indices_data = static_cast<int64_t*>(indices->data); int64_t *indices_data = static_cast<int64_t*>(indices->data);
int64_t *eid_data = static_cast<int64_t*>(eid->data); int64_t *eid_data = static_cast<int64_t*>(eid->data);
......
...@@ -70,7 +70,7 @@ PackedFunc ConvertSubgraphToPackedFunc(const Subgraph& sg) { ...@@ -70,7 +70,7 @@ PackedFunc ConvertSubgraphToPackedFunc(const Subgraph& sg) {
// Convert Sampled Subgraph structures to PackedFunc. // Convert Sampled Subgraph structures to PackedFunc.
PackedFunc ConvertSubgraphToPackedFunc(const std::vector<SampledSubgraph>& sg) { PackedFunc ConvertSubgraphToPackedFunc(const std::vector<SampledSubgraph>& sg) {
auto body = [sg] (DGLArgs args, DGLRetValue* rv) { auto body = [sg] (DGLArgs args, DGLRetValue* rv) {
const int which = args[0]; const size_t which = args[0];
if (which < sg.size()) { if (which < sg.size()) {
GraphInterface* gptr = sg[which].graph->Reset(); GraphInterface* gptr = sg[which].graph->Reset();
GraphHandle ghandle = gptr; GraphHandle ghandle = gptr;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <dgl/immutable_graph.h> #include <dgl/immutable_graph.h>
#include <cstdlib> #include <cstdlib>
#include <cmath>
#ifdef _MSC_VER #ifdef _MSC_VER
// rand in MS compiler works well in multi-threading. // rand in MS compiler works well in multi-threading.
...@@ -191,7 +192,7 @@ std::pair<ImmutableGraph::CSR::Ptr, IdArray> ImmutableGraph::CSR::VertexSubgraph ...@@ -191,7 +192,7 @@ std::pair<ImmutableGraph::CSR::Ptr, IdArray> ImmutableGraph::CSR::VertexSubgraph
// Store the non-zeros in a subgraph with edge attributes of new edge ids. // Store the non-zeros in a subgraph with edge attributes of new edge ids.
sub_csr->edge_ids.resize(sub_csr->indices.size()); sub_csr->edge_ids.resize(sub_csr->indices.size());
for (int64_t i = 0; i < sub_csr->edge_ids.size(); i++) for (size_t i = 0; i < sub_csr->edge_ids.size(); i++)
sub_csr->edge_ids[i] = i; sub_csr->edge_ids[i] = i;
IdArray rst_eids = IdArray::Empty({static_cast<int64_t>(orig_edge_ids.size())}, IdArray rst_eids = IdArray::Empty({static_cast<int64_t>(orig_edge_ids.size())},
...@@ -203,7 +204,7 @@ std::pair<ImmutableGraph::CSR::Ptr, IdArray> ImmutableGraph::CSR::VertexSubgraph ...@@ -203,7 +204,7 @@ std::pair<ImmutableGraph::CSR::Ptr, IdArray> ImmutableGraph::CSR::VertexSubgraph
} }
ImmutableGraph::CSR::Ptr ImmutableGraph::CSR::FromEdges(std::vector<Edge> *edges, ImmutableGraph::CSR::Ptr ImmutableGraph::CSR::FromEdges(std::vector<Edge> *edges,
int sort_on, int64_t num_nodes) { int sort_on, uint64_t num_nodes) {
CHECK(sort_on == 0 || sort_on == 1) << "we must sort on the first or the second vector"; CHECK(sort_on == 0 || sort_on == 1) << "we must sort on the first or the second vector";
int other_end = sort_on == 1 ? 0 : 1; int other_end = sort_on == 1 ? 0 : 1;
// TODO(zhengda) we should sort in parallel. // TODO(zhengda) we should sort in parallel.
...@@ -285,7 +286,7 @@ BoolArray ImmutableGraph::HasVertices(IdArray vids) const { ...@@ -285,7 +286,7 @@ BoolArray ImmutableGraph::HasVertices(IdArray vids) const {
BoolArray rst = BoolArray::Empty({len}, vids->dtype, vids->ctx); BoolArray rst = BoolArray::Empty({len}, vids->dtype, vids->ctx);
const dgl_id_t* vid_data = static_cast<dgl_id_t*>(vids->data); const dgl_id_t* vid_data = static_cast<dgl_id_t*>(vids->data);
dgl_id_t* rst_data = static_cast<dgl_id_t*>(rst->data); dgl_id_t* rst_data = static_cast<dgl_id_t*>(rst->data);
const int64_t nverts = NumVertices(); const uint64_t nverts = NumVertices();
for (int64_t i = 0; i < len; ++i) { for (int64_t i = 0; i < len; ++i) {
rst_data[i] = (vid_data[i] < nverts)? 1 : 0; rst_data[i] = (vid_data[i] < nverts)? 1 : 0;
} }
...@@ -804,7 +805,7 @@ SampledSubgraph ImmutableGraph::SampleSubgraph(IdArray seed_arr, ...@@ -804,7 +805,7 @@ SampledSubgraph ImmutableGraph::SampleSubgraph(IdArray seed_arr,
// BFS traverse the graph and sample vertices // BFS traverse the graph and sample vertices
// <vertex_id, layer_id> // <vertex_id, layer_id>
std::unordered_set<dgl_id_t> sub_ver_map; std::unordered_set<dgl_id_t> sub_ver_map;
std::vector<std::pair<dgl_id_t, dgl_id_t> > sub_vers; std::vector<std::pair<dgl_id_t, int> > sub_vers;
sub_vers.reserve(num_seeds * 10); sub_vers.reserve(num_seeds * 10);
// add seed vertices // add seed vertices
for (size_t i = 0; i < num_seeds; ++i) { for (size_t i = 0; i < num_seeds; ++i) {
...@@ -893,20 +894,20 @@ SampledSubgraph ImmutableGraph::SampleSubgraph(IdArray seed_arr, ...@@ -893,20 +894,20 @@ SampledSubgraph ImmutableGraph::SampleSubgraph(IdArray seed_arr,
// Copy sub_ver_map to output[0] // Copy sub_ver_map to output[0]
// Copy layer // Copy layer
int64_t num_vertices = sub_ver_map.size(); uint64_t num_vertices = sub_ver_map.size();
std::sort(sub_vers.begin(), sub_vers.end(), std::sort(sub_vers.begin(), sub_vers.end(),
[](const std::pair<dgl_id_t, dgl_id_t> &a1, const std::pair<dgl_id_t, dgl_id_t> &a2) { [](const std::pair<dgl_id_t, dgl_id_t> &a1, const std::pair<dgl_id_t, dgl_id_t> &a2) {
return a1.first < a2.first; return a1.first < a2.first;
}); });
SampledSubgraph subg; SampledSubgraph subg;
subg.induced_vertices = IdArray::Empty({num_vertices}, subg.induced_vertices = IdArray::Empty({static_cast<int64_t>(num_vertices)},
DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
subg.induced_edges = IdArray::Empty({num_edges}, subg.induced_edges = IdArray::Empty({static_cast<int64_t>(num_edges)},
DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
subg.layer_ids = IdArray::Empty({num_vertices}, subg.layer_ids = IdArray::Empty({static_cast<int64_t>(num_vertices)},
DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0}); DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
subg.sample_prob = runtime::NDArray::Empty({num_vertices}, subg.sample_prob = runtime::NDArray::Empty({static_cast<int64_t>(num_vertices)},
DLDataType{kDLFloat, 32, 1}, DLContext{kDLCPU, 0}); DLDataType{kDLFloat, 32, 1}, DLContext{kDLCPU, 0});
dgl_id_t *out = static_cast<dgl_id_t *>(subg.induced_vertices->data); dgl_id_t *out = static_cast<dgl_id_t *>(subg.induced_vertices->data);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment