use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.
the class SampleUtils method sampleWithType.
public static Tuple2<Long2ObjectOpenHashMap<long[]>, Long2ObjectOpenHashMap<int[]>> sampleWithType(ServerRow row, int count, long[] nodeIds, SampleType sampleType, long seed) {
Random r = new Random(seed);
Long2ObjectOpenHashMap<long[]> nodeId2SampleNeighbors = new Long2ObjectOpenHashMap<>(nodeIds.length);
Long2ObjectOpenHashMap<int[]> nodeId2SampleNeighborsType = new Long2ObjectOpenHashMap<>(nodeIds.length);
for (long nodeId : nodeIds) {
long[] sampleNeighbors;
int[] neighborsTypes;
// Get node neighbor number
GraphNode graphNode = (GraphNode) ((ServerLongAnyRow) row).get(nodeId);
if (graphNode == null) {
sampleNeighbors = new long[0];
neighborsTypes = new int[0];
} else {
long[] nodeNeighbors = graphNode.getNeighbors();
int[] nodeNeighborsTypes;
if (sampleType == SampleType.NODE) {
nodeNeighborsTypes = graphNode.getTypes();
} else {
nodeNeighborsTypes = graphNode.getEdgeTypes();
}
if (nodeNeighbors == null || nodeNeighbors.length == 0) {
sampleNeighbors = new long[0];
neighborsTypes = new int[0];
} else if (count <= 0 || nodeNeighbors.length <= count) {
sampleNeighbors = nodeNeighbors;
if (nodeNeighborsTypes == null || nodeNeighborsTypes.length == 0) {
neighborsTypes = new int[0];
} else {
neighborsTypes = nodeNeighborsTypes;
}
} else {
sampleNeighbors = new long[count];
neighborsTypes = new int[count];
// If the neighbor number > count, just copy a range of neighbors to
// the result array, the copy position is random
int startPos = Math.abs(r.nextInt()) % nodeNeighbors.length;
if (startPos + count <= nodeNeighbors.length) {
System.arraycopy(nodeNeighbors, startPos, sampleNeighbors, 0, count);
System.arraycopy(nodeNeighborsTypes, startPos, neighborsTypes, 0, count);
} else {
System.arraycopy(nodeNeighbors, startPos, sampleNeighbors, 0, nodeNeighbors.length - startPos);
System.arraycopy(nodeNeighbors, 0, sampleNeighbors, nodeNeighbors.length - startPos, count - (nodeNeighbors.length - startPos));
// sample types
System.arraycopy(nodeNeighborsTypes, startPos, neighborsTypes, 0, nodeNeighborsTypes.length - startPos);
System.arraycopy(nodeNeighborsTypes, 0, neighborsTypes, nodeNeighborsTypes.length - startPos, count - (nodeNeighborsTypes.length - startPos));
}
}
}
nodeId2SampleNeighbors.put(nodeId, sampleNeighbors);
nodeId2SampleNeighborsType.put(nodeId, neighborsTypes);
}
return new Tuple2<>(nodeId2SampleNeighbors, nodeId2SampleNeighborsType);
}
use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.
the class InitEdgeTypes method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
GeneralPartUpdateParam param = (GeneralPartUpdateParam) partParam;
ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, param);
ILongKeyAnyValuePartOp keyValuePart = (ILongKeyAnyValuePartOp) param.getKeyValuePart();
long[] nodeIds = keyValuePart.getKeys();
IElement[] neighbors = keyValuePart.getValues();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
GraphNode graphNode = (GraphNode) row.get(nodeIds[i]);
if (graphNode == null) {
graphNode = new GraphNode();
row.set(nodeIds[i], graphNode);
}
graphNode.setEdgeTypes(((EdgeType) neighbors[i]).getTypes());
}
} finally {
row.endWrite();
}
}
use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.
the class InitEdgeFeats method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
GeneralPartUpdateParam initParam = (GeneralPartUpdateParam) partParam;
ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, initParam);
// Get nodes and features
ILongKeyAnyValuePartOp split = (ILongKeyAnyValuePartOp) initParam.getKeyValuePart();
long[] nodeIds = split.getKeys();
IElement[] edgeFeatures = split.getValues();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
GraphNode graphNode = (GraphNode) row.get(nodeIds[i]);
if (graphNode == null) {
graphNode = new GraphNode();
row.set(nodeIds[i], graphNode);
}
graphNode.setNeighbors(((LongEdgeFeats) edgeFeatures[i]).getNodeIds());
graphNode.setEdgeFeatures(((LongEdgeFeats) edgeFeatures[i]).getFeats());
}
} finally {
row.endWrite();
}
}
use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.
the class InitEdgeWeights method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
GeneralPartUpdateParam param = (GeneralPartUpdateParam) partParam;
ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, param);
ILongKeyAnyValuePartOp keyValuePart = (ILongKeyAnyValuePartOp) param.getKeyValuePart();
long[] nodeIds = keyValuePart.getKeys();
IElement[] neighbors = keyValuePart.getValues();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
GraphNode graphNode = (GraphNode) row.get(nodeIds[i]);
if (graphNode == null) {
graphNode = new GraphNode();
row.set(nodeIds[i], graphNode);
}
graphNode.setWeights(((Weights) neighbors[i]).getWeights());
}
} finally {
row.endWrite();
}
}
use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.
the class InitNeighbor method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
GeneralPartUpdateParam param = (GeneralPartUpdateParam) partParam;
ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, param);
ILongKeyAnyValuePartOp keyValuePart = (ILongKeyAnyValuePartOp) param.getKeyValuePart();
long[] nodeIds = keyValuePart.getKeys();
IElement[] neighbors = keyValuePart.getValues();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
GraphNode graphNode = (GraphNode) row.get(nodeIds[i]);
if (graphNode == null) {
graphNode = new GraphNode();
row.set(nodeIds[i], graphNode);
}
graphNode.setNeighbors(((LongNeighbor) neighbors[i]).getNodeIds());
}
} finally {
row.endWrite();
}
}
Aggregations