Skip to content

Commit

Permalink
ADDED reversed arrays for directed graphs to find in-neighbors in con…
Browse files Browse the repository at this point in the history
…stant time (#79)
  • Loading branch information
alvaradoo authored Dec 13, 2023
1 parent 3ea51a0 commit bb42931
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
22 changes: 21 additions & 1 deletion arachne/client/arachne/graphclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def add_edges_from(self, akarray_src:pdarray, akarray_dst:pdarray,
dst = all_vertices[src.size:]

### Create vertex index arrays.
# 1. Build the neighbors of the adjacency lists for each vertex.
# 1. Build the out-neighbors of the adjacency lists for each vertex.
gb_src = ak.GroupBy(src, assume_sorted = True)
gb_src_indices, gb_src_neighbors = gb_src.count()
neis = ak.full(gb_vertices.unique_keys.size, 0, dtype=ak.int64)
Expand All @@ -547,11 +547,31 @@ def add_edges_from(self, akarray_src:pdarray, akarray_dst:pdarray,
self.n_vertices = int(vmap.size)
self.n_edges = int(src.size)

### Create the reversed arrays for in-neighbor calculations.
# 1. Reverse the edges and sort them.
gb_edges_reversed = ak.GroupBy([dst, src])
src_reversed = gb_edges_reversed.unique_keys[0]
dst_reversed = gb_edges_reversed.unique_keys[1]

# 2. Build the in-neighbors of the adjacency lists for each vertex.
gb_src_reversed = ak.GroupBy(src_reversed, assume_sorted = True)
gb_src_indices_reversed, gb_src_neighbors_reversed = gb_src_reversed.count()
neis_reversed = ak.full(gb_vertices.unique_keys.size, 0, dtype=ak.int64)
neis_reversed[gb_src_indices_reversed] = gb_src_neighbors_reversed

# 3. Run a prefix (cumulative) sum on neis to get the starting indices for each vertex.
segs_reversed = ak.cumsum(neis_reversed)
first_seg_reversed = ak.array([0])
segs_reversed = ak.concatenate([first_seg_reversed, segs_reversed])

### Store everything in a graph object in the Chapel server.
# 1. Store data into an Graph object in the Chapel server.
args = { "AkArraySrc" : src,
"AkArrayDst" : dst,
"AkArraySeg" : segs,
"AkArraySrcR" : src_reversed,
"AkArrayDstR" : dst_reversed,
"AkArraySegR" : segs_reversed,
"AkArrayWeight" : wgt,
"AkArrayVmap" : vmap,
"Directed": bool(self.directed),
Expand Down
26 changes: 25 additions & 1 deletion arachne/server/BuildGraphMsg.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module BuildGraphMsg {
// Parse the message from the Python front-end.
var akarray_srcS = msgArgs.getValueOf("AkArraySrc");
var akarray_dstS = msgArgs.getValueOf("AkArrayDst");
var akarray_vmapS = msgArgs.getValueOf("AkArrayVmap");
var akarray_segS = msgArgs.getValueOf("AkArraySeg");
var akarray_vmapS = msgArgs.getValueOf("AkArrayVmap");
var akarray_weightS = msgArgs.getValueOf("AkArrayWeight");
var weightedS = msgArgs.getValueOf("Weighted");
var directedS = msgArgs.getValueOf("Directed");
Expand Down Expand Up @@ -136,6 +136,30 @@ module BuildGraphMsg {
}
}

var akarray_srcRS, akarray_dstRS, akarray_segRS:string;
if directed {
akarray_srcRS = msgArgs.getValueOf("AkArraySrcR");
akarray_dstRS = msgArgs.getValueOf("AkArrayDstR");
akarray_segRS = msgArgs.getValueOf("AkArraySegR");

var akarray_srcR_entry: borrowed GenSymEntry = getGenericTypedArrayEntry(akarray_srcRS, st);
var akarray_dstR_entry: borrowed GenSymEntry = getGenericTypedArrayEntry(akarray_dstRS, st);
var akarray_segR_entry: borrowed GenSymEntry = getGenericTypedArrayEntry(akarray_segRS, st);

var akarray_srcR_sym = toSymEntry(akarray_srcR_entry,int);
var srcR = akarray_srcR_sym.a;

var akarray_dstR_sym = toSymEntry(akarray_dstR_entry,int);
var dstR = akarray_dstR_sym.a;

var akarray_segR_sym = toSymEntry(akarray_segR_entry, int);
var segmentsR = akarray_segR_sym.a;

graph.withComp(new shared SymEntry(srcR):GenSymEntry, "SRC_R")
.withComp(new shared SymEntry(dstR):GenSymEntry, "DST_R")
.withComp(new shared SymEntry(segmentsR):GenSymEntry, "SEGMENTS_R");
}

// Create the ranges array that keeps track of the vertices the edge arrays store on each
// locale.
var D_sbdmn = {0..numLocales-1} dmapped Replicated();
Expand Down
5 changes: 5 additions & 0 deletions arachne/server/GraphArray.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ module GraphArray {
EDGE_PROPS_DTYPE_MAP, // Sorted array of column datatype to integer id (array index)
EDGE_PROPS_COL2DTYPE, // Map of column names to the datatype of the column

// For directed graphs we also want to maintain reversed edge arrays to easily find the
// in-neighbors for each vertex. We will use the SRC_R and DST_R components below to
// signify the reversed edge arrays as well as a SEGMENTS_R array defined here.
SEGMENTS_R,

// TEMPORARY COMPONENTS BELOW FOR UNDIRECTED GRAPHS TO ENSURE COMPATIBILTIY WITH OLD CODE.
// We want to phase out the need for reversed arrays in undirected graph algorithms.
// Includes: connected components, triangle counting, k-truss, and triangle centrality.
Expand Down

0 comments on commit bb42931

Please sign in to comment.