Search in sources :

Example 1 with Node

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);
}
Also used : Random(java.util.Random) ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) Node(com.tencent.angel.graph.data.Node) RowBasedPartition(com.tencent.angel.ps.storage.partition.RowBasedPartition) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) ServerPartition(com.tencent.angel.ps.storage.partition.ServerPartition)

Example 2 with Node

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);
}
Also used : Random(java.util.Random) Node(com.tencent.angel.graph.data.Node)

Example 3 with Node

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;
}
Also used : Node(com.tencent.angel.graph.data.Node) LongElementStorage(com.tencent.angel.ps.storage.vector.storage.LongElementStorage) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow)

Example 4 with Node

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());
}
Also used : IndexPartGetLongResult(com.tencent.angel.ml.matrix.psf.get.indexed.IndexPartGetLongResult) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) Node(com.tencent.angel.graph.data.Node) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow)

Example 5 with Node

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();
    }
}
Also used : Node(com.tencent.angel.graph.data.Node) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Aggregations

Node (com.tencent.angel.graph.data.Node)9 ServerLongAnyRow (com.tencent.angel.ps.storage.vector.ServerLongAnyRow)7 LongElementStorage (com.tencent.angel.ps.storage.vector.storage.LongElementStorage)3 Random (java.util.Random)2 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)1 IndexPartGetLongResult (com.tencent.angel.ml.matrix.psf.get.indexed.IndexPartGetLongResult)1 ServerMatrix (com.tencent.angel.ps.storage.matrix.ServerMatrix)1 RowBasedPartition (com.tencent.angel.ps.storage.partition.RowBasedPartition)1 ServerPartition (com.tencent.angel.ps.storage.partition.ServerPartition)1 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)1 LongArrayList (it.unimi.dsi.fastutil.longs.LongArrayList)1