use of com.tencent.angel.graph.data.Node in project angel by Tencent.
the class SampleNeighbor method partitionGet.
@Override
public PartitionGetResult partitionGet(PartitionGetParam partParam) {
PartSampleNeighborParam param = (PartSampleNeighborParam) partParam;
ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
ServerPartition part = matrix.getPartition(partParam.getPartKey().getPartitionId());
ServerLongAnyRow row = (ServerLongAnyRow) (((RowBasedPartition) part).getRow(0));
long[] nodeIds = param.getNodeIds();
long[][] neighbors = new long[nodeIds.length][];
int count = param.getCount();
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < nodeIds.length; i++) {
long nodeId = nodeIds[i];
// Get node neighbor number
Node element = (Node) (row.get(nodeId));
if (element == null) {
neighbors[i] = null;
} else {
long[] nodeNeighbors = element.getNeighbors();
if (nodeNeighbors == null || nodeNeighbors.length == 0) {
neighbors[i] = null;
} else if (count <= 0 || nodeNeighbors.length <= count) {
neighbors[i] = nodeNeighbors;
} else {
neighbors[i] = new long[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, neighbors[i], 0, count);
} else {
System.arraycopy(nodeNeighbors, startPos, neighbors[i], 0, nodeNeighbors.length - startPos);
System.arraycopy(nodeNeighbors, 0, neighbors[i], nodeNeighbors.length - startPos, count - (nodeNeighbors.length - startPos));
}
}
}
}
return new PartSampleNeighborResult(part.getPartitionKey().getPartitionId(), neighbors);
}
use of com.tencent.angel.graph.data.Node in project angel by Tencent.
the class SampleNeighborPartResult method serialize.
@Override
public void serialize(ByteBuf buf) {
// sample happens here to avoid memory copy on servers
Random rand = new Random(System.currentTimeMillis());
// write partition id first
buf.writeInt(partId);
buf.writeBoolean(sampleTypes);
buf.writeInt(keys.length);
int writeIndex = buf.writerIndex();
buf.writeInt(0);
int length = 0;
for (int i = 0; i < keys.length; i++) {
Node node = (Node) row.get(keys[i]);
if (node == null) {
// size for this node
buf.writeInt(0);
continue;
}
long[] neighbor = node.getNeighbors();
int[] types = node.getTypes();
if (neighbor == null || neighbor.length == 0) {
buf.writeInt(0);
continue;
}
int size = numSample;
if (numSample <= 0 || numSample >= neighbor.length) {
size = neighbor.length;
}
// # neighbors/types
length += size;
buf.writeInt(size);
int start = rand.nextInt(neighbor.length);
if (sampleTypes) {
for (int j = 0; j < size; j++) {
int idx = (start + j) % neighbor.length;
buf.writeLong(neighbor[idx]);
buf.writeInt(types[idx]);
}
} else {
for (int j = 0; j < size; j++) {
int idx = (start + j) % neighbor.length;
buf.writeLong(neighbor[idx]);
}
}
}
buf.setInt(writeIndex, length);
}
use of com.tencent.angel.graph.data.Node in project angel by Tencent.
the class NnzFeature method processRow.
@Override
public double processRow(ServerRow row) {
LongElementStorage storage = ((ServerLongAnyRow) row).getStorage();
ObjectIterator<Long2ObjectMap.Entry<IElement>> it = storage.iterator();
long size = 0;
while (it.hasNext()) {
Node node = (Node) (it.next().getValue());
if (node.getFeats() != null) {
size++;
}
}
return size;
}
use of com.tencent.angel.graph.data.Node in project angel by Tencent.
the class GetNodes method partitionGet.
@Override
public PartitionGetResult partitionGet(PartitionGetParam param) {
ServerLongAnyRow row = (ServerLongAnyRow) psContext.getMatrixStorageManager().getRow(param.getPartKey(), 0);
ObjectIterator<Long2ObjectMap.Entry<IElement>> it = row.iterator();
LongArrayList nodes = new LongArrayList();
long start = param.getPartKey().getStartCol();
while (it.hasNext()) {
Long2ObjectMap.Entry entry = it.next();
Node node = (Node) entry.getValue();
if (node.getFeats() != null && node.getNeighbors() == null) {
nodes.add(entry.getLongKey() + start);
}
}
return new IndexPartGetLongResult(param.getPartKey(), nodes.toLongArray());
}
use of com.tencent.angel.graph.data.Node in project angel by Tencent.
the class InitNodeFeats method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
InitNodeFeatsPartParam param = (InitNodeFeatsPartParam) partParam;
ServerLongAnyRow row = (ServerLongAnyRow) psContext.getMatrixStorageManager().getRow(param.getPartKey(), 0);
long[] nodeIds = param.getNodeIds();
IntFloatVector[] feats = param.getFeats();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
Node node = (Node) row.get(nodeIds[i]);
if (node == null) {
node = new Node();
row.set(nodeIds[i], node);
}
node.setFeats(feats[i]);
}
} finally {
row.endWrite();
}
}
Aggregations