public void finalize() throws Exception {
if (finalized)
return;
boolean parallel = true;
Parallel parfor = new Parallel();
LOG.debug("Finalize graph of size: " + edgeInfo.size());
ArrayList<Integer> permute = new ArrayList<Integer>(Collections.nCopies(
numEdges, 0));
ArrayList<AtomicInteger> counterArray = new ArrayList<AtomicInteger>(
numVertices + 1);
for (int j = 0; j < numVertices + 1; ++j)
counterArray.add(new AtomicInteger(0));
/* Construct the CSR */
/* Sort and divide edges by source id using counting sort. */
LOG.debug("Coutning sort source...");
counting_sort(edgeInfo.sources, counterArray, permute);
final CompareByVid cmpByTarget = new CompareByVid(edgeInfo.targets);
final ArrayList<List<Integer>> dummyList = new ArrayList<List<Integer>>(
numVertices);
/* Sort each part (of the same source) by its target. */
if (parallel) {
LOG.debug("Parallel sort target within source...");
for (int j = 0; j < numVertices; ++j)
dummyList.add(permute.subList(counterArray.get(j).get(), counterArray
.get(j + 1).get()));
parfor.For(dummyList, new Operation<List<Integer>>() {
@Override
public void perform(List<Integer> pParameter, int idx) {
// TODO Auto-generated method stub
java.util.Collections.sort(pParameter, cmpByTarget);
}
});
} else {
for (int j = 0; j < numVertices; ++j) {
java.util.Collections.sort(permute.subList(counterArray.get(j).get(),
counterArray.get(j + 1).get()), cmpByTarget);
}
}
/*
* Shuffle in place the sources, targets, and edatalist using the permute
* index.
*/
edgeInfo.inplace_shuffle(permute);
/* Fill in the CSR data structure. */
csr = new SparseGraphStruct(numVertices, edgeInfo.sources, edgeInfo.targets);
/* Construct the CSC */
/* Sort and divide edges by source id using counting sort. */
LOG.debug("Coutning sort target...");
counting_sort(edgeInfo.targets, counterArray, permute);
final CompareByVid cmpBySource = new CompareByVid(edgeInfo.sources);
if (parallel) {
LOG.debug("Parallel sort source within target...");
dummyList.clear();
for (int j = 0; j < numVertices; ++j)
dummyList.add(permute.subList(counterArray.get(j).get(), counterArray
.get(j + 1).get()));
parfor.For(dummyList, new Operation<List<Integer>>() {
@Override
public void perform(List<Integer> pParameter, int idx) {
java.util.Collections.sort(pParameter, cmpBySource);
}
});
} else {
for (int j = 0; j < numVertices; ++j) {
java.util.Collections.sort(permute.subList(counterArray.get(j).get(),
counterArray.get(j + 1).get()), cmpBySource);
}
}
/* Shuffle out of place the sources. */
final ArrayList<Integer> shuffledSource = new ArrayList<Integer>(
Collections.nCopies(edgeInfo.sources.size(), 0));
parfor.For(permute, new Operation<Integer>() {
@Override
public void perform(Integer val, int idx) {
shuffledSource.set(idx, edgeInfo.sources.get(val));
}
});
edgeInfo.sources = shuffledSource;
// Fill in the CSC data structure
List<Integer> transformedTargets = new ArrayList<Integer>(
edgeInfo.targets.size());
for (int j = 0; j < edgeInfo.targets.size(); ++j) {
transformedTargets.add(edgeInfo.targets.get(permute.get(j)));
}
csc = new SparseGraphStruct(numVertices, transformedTargets,
edgeInfo.sources);
c2rMap = permute;
edatalist = edgeInfo.edata;
finalized = true;
parfor.close();
}