Search in sources :

Example 1 with GraphNode

use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.

the class SampleUtils method sampleByCount.

public static Long2ObjectOpenHashMap<long[]> sampleByCount(ServerRow row, int count, long[] nodeIds, long seed) {
    Random r = new Random(seed);
    Long2ObjectOpenHashMap<long[]> nodeId2SampleNeighbors = new Long2ObjectOpenHashMap<>(nodeIds.length);
    for (long nodeId : nodeIds) {
        long[] sampleNeighbors;
        // Get node neighbor number
        GraphNode graphNode = (GraphNode) ((ServerLongAnyRow) row).get(nodeId);
        if (graphNode == null) {
            sampleNeighbors = new long[0];
        } else {
            long[] nodeNeighbors = graphNode.getNeighbors();
            if (nodeNeighbors == null || nodeNeighbors.length == 0) {
                sampleNeighbors = new long[0];
            } else if (count <= 0 || nodeNeighbors.length <= count) {
                sampleNeighbors = nodeNeighbors;
            } else {
                sampleNeighbors = 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, sampleNeighbors, 0, count);
                } else {
                    System.arraycopy(nodeNeighbors, startPos, sampleNeighbors, 0, nodeNeighbors.length - startPos);
                    System.arraycopy(nodeNeighbors, 0, sampleNeighbors, nodeNeighbors.length - startPos, count - (nodeNeighbors.length - startPos));
                }
            }
        }
        nodeId2SampleNeighbors.put(nodeId, sampleNeighbors);
    }
    return nodeId2SampleNeighbors;
}
Also used : Random(java.util.Random) GraphNode(com.tencent.angel.graph.data.GraphNode) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)

Example 2 with GraphNode

use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.

the class SampleUtils method sampleWithBothType.

public static Tuple3<Long2ObjectOpenHashMap<long[]>, Long2ObjectOpenHashMap<int[]>, Long2ObjectOpenHashMap<int[]>> sampleWithBothType(ServerRow row, int count, long[] nodeIds, long seed) {
    Random r = new Random(seed);
    Long2ObjectOpenHashMap<long[]> nodeId2SampleNeighbors = new Long2ObjectOpenHashMap<>(nodeIds.length);
    Long2ObjectOpenHashMap<int[]> nodeId2SampleNeighborsType = new Long2ObjectOpenHashMap<>(nodeIds.length);
    Long2ObjectOpenHashMap<int[]> nodeId2SampleEdgeType = new Long2ObjectOpenHashMap<>(nodeIds.length);
    for (long nodeId : nodeIds) {
        long[] sampleNeighbors;
        int[] neighborsTypes;
        int[] edgeTypes;
        // Get node neighbor number
        GraphNode graphNode = (GraphNode) ((ServerLongAnyRow) row).get(nodeId);
        if (graphNode == null) {
            sampleNeighbors = new long[0];
            neighborsTypes = new int[0];
            edgeTypes = new int[0];
        } else {
            long[] nodeNeighbors = graphNode.getNeighbors();
            int[] nodeNeighborsTypes = graphNode.getTypes();
            int[] nodeEdgeTypes = graphNode.getEdgeTypes();
            if (nodeNeighbors == null || nodeNeighbors.length == 0) {
                sampleNeighbors = new long[0];
                neighborsTypes = new int[0];
                edgeTypes = 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;
                }
                if (nodeEdgeTypes == null || nodeEdgeTypes.length == 0) {
                    edgeTypes = new int[0];
                } else {
                    edgeTypes = nodeEdgeTypes;
                }
            } else {
                sampleNeighbors = new long[count];
                neighborsTypes = new int[count];
                edgeTypes = 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);
                    System.arraycopy(nodeEdgeTypes, startPos, edgeTypes, 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 node types
                    System.arraycopy(nodeNeighborsTypes, startPos, neighborsTypes, 0, nodeNeighborsTypes.length - startPos);
                    System.arraycopy(nodeNeighborsTypes, 0, neighborsTypes, nodeNeighborsTypes.length - startPos, count - (nodeNeighborsTypes.length - startPos));
                    // sample edge types
                    System.arraycopy(nodeEdgeTypes, startPos, edgeTypes, 0, nodeEdgeTypes.length - startPos);
                    System.arraycopy(nodeEdgeTypes, 0, edgeTypes, nodeEdgeTypes.length - startPos, count - (nodeEdgeTypes.length - startPos));
                }
            }
        }
        nodeId2SampleNeighbors.put(nodeId, sampleNeighbors);
        nodeId2SampleNeighborsType.put(nodeId, neighborsTypes);
        nodeId2SampleEdgeType.put(nodeId, edgeTypes);
    }
    return new Tuple3<>(nodeId2SampleNeighbors, nodeId2SampleNeighborsType, nodeId2SampleEdgeType);
}
Also used : Random(java.util.Random) Tuple3(scala.Tuple3) GraphNode(com.tencent.angel.graph.data.GraphNode) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)

Example 3 with GraphNode

use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.

the class InitLabels 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.setLabels(((Labels) neighbors[i]).getWeights());
        }
    } finally {
        row.endWrite();
    }
}
Also used : IElement(com.tencent.angel.ps.storage.vector.element.IElement) GeneralPartUpdateParam(com.tencent.angel.ml.matrix.psf.update.base.GeneralPartUpdateParam) GraphNode(com.tencent.angel.graph.data.GraphNode) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) ILongKeyAnyValuePartOp(com.tencent.angel.psagent.matrix.transport.router.operator.ILongKeyAnyValuePartOp)

Example 4 with GraphNode

use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.

the class InitNodeFeats method partitionUpdate.

@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
    GeneralPartUpdateParam initParam = (GeneralPartUpdateParam) partParam;
    ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, initParam);
    // Get node ids and features
    ILongKeyAnyValuePartOp split = (ILongKeyAnyValuePartOp) initParam.getKeyValuePart();
    long[] nodeIds = split.getKeys();
    IElement[] features = 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.setFeats(((Feature) features[i]).getFeatures());
        }
    } finally {
        row.endWrite();
    }
}
Also used : IElement(com.tencent.angel.ps.storage.vector.element.IElement) GeneralPartUpdateParam(com.tencent.angel.ml.matrix.psf.update.base.GeneralPartUpdateParam) GraphNode(com.tencent.angel.graph.data.GraphNode) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) ILongKeyAnyValuePartOp(com.tencent.angel.psagent.matrix.transport.router.operator.ILongKeyAnyValuePartOp)

Example 5 with GraphNode

use of com.tencent.angel.graph.data.GraphNode in project angel by Tencent.

the class GetNodes method partitionGet.

@Override
public PartitionGetResult partitionGet(PartitionGetParam param) {
    ServerLongAnyRow row = GraphMatrixUtils.getPSLongKeyRow(psContext, param);
    ObjectIterator<Entry<IElement>> it = row.iterator();
    LongArrayList nodes = new LongArrayList();
    Boolean isHash = ((PartGetNodesParam) param).getHash();
    long start = isHash ? 0 : param.getPartKey().getStartCol();
    while (it.hasNext()) {
        Long2ObjectMap.Entry entry = it.next();
        GraphNode node = (GraphNode) 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) Entry(it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) Entry(it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) GraphNode(com.tencent.angel.graph.data.GraphNode) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow)

Aggregations

GraphNode (com.tencent.angel.graph.data.GraphNode)16 ServerLongAnyRow (com.tencent.angel.ps.storage.vector.ServerLongAnyRow)11 GeneralPartUpdateParam (com.tencent.angel.ml.matrix.psf.update.base.GeneralPartUpdateParam)7 IElement (com.tencent.angel.ps.storage.vector.element.IElement)7 ILongKeyAnyValuePartOp (com.tencent.angel.psagent.matrix.transport.router.operator.ILongKeyAnyValuePartOp)7 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)5 Random (java.util.Random)4 LongElementStorage (com.tencent.angel.ps.storage.vector.storage.LongElementStorage)3 Tuple2 (scala.Tuple2)3 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)1 IndexPartGetLongResult (com.tencent.angel.ml.matrix.psf.get.indexed.IndexPartGetLongResult)1 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)1 Entry (it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry)1 LongArrayList (it.unimi.dsi.fastutil.longs.LongArrayList)1 Tuple3 (scala.Tuple3)1