Search in sources :

Example 36 with ServerMatrix

use of com.tencent.angel.ps.storage.matrix.ServerMatrix 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());
    CSRPartition part = (CSRPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
    IntCSRStorage storage = (IntCSRStorage) (part.getStorage());
    Int2ObjectOpenHashMap<int[]> results = new Int2ObjectOpenHashMap<>();
    int[] neighborOffsets = storage.getRowOffsets();
    int[] neighbors = storage.getColumnIndices();
    int startCol = (int) partParam.getPartKey().getStartCol();
    int[] nodeIds = param.getNodeIds();
    int count = param.getCount();
    Random r = new Random();
    for (int i = 0; i < nodeIds.length; i++) {
        int nodeId = nodeIds[i];
        // Get node neighbor number
        int num = neighborOffsets[nodeId - startCol + 1] - neighborOffsets[nodeId - startCol];
        int[] result;
        if (num == 0) {
            // If the neighbor number is 0, just return a int[0]
            result = new int[0];
        } else if (count <= 0 || num <= count) {
            // If count <= 0 or the neighbor number is less or equal then count, just copy all neighbors to the result array
            result = new int[num];
            System.arraycopy(neighbors, neighborOffsets[nodeId - startCol], result, 0, num);
        } else {
            // 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()) % num;
            result = new int[count];
            if (startPos + count <= num) {
                System.arraycopy(neighbors, neighborOffsets[nodeId - startCol] + startPos, result, 0, count);
            } else {
                System.arraycopy(neighbors, neighborOffsets[nodeId - startCol] + startPos, result, 0, num - startPos);
                System.arraycopy(neighbors, neighborOffsets[nodeId - startCol], result, num - startPos, count - (num - startPos));
            }
        }
        results.put(nodeIds[i], result);
    }
    return new PartSampleNeighborResult(results);
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Random(java.util.Random) ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) IntCSRStorage(com.tencent.angel.ps.storage.partition.storage.IntCSRStorage) CSRPartition(com.tencent.angel.ps.storage.partition.CSRPartition)

Example 37 with ServerMatrix

use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.

the class InitNeighborAliasTable method partitionUpdate.

@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
    PartInitNeighborAliasTableParam param = (PartInitNeighborAliasTableParam) partParam;
    ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
    RowBasedPartition part = (RowBasedPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
    ServerLongAnyRow row = (ServerLongAnyRow) part.getRow(0);
    ObjectIterator<Long2ObjectMap.Entry<NeighborsAliasTableElement>> iter = param.getNodeId2Neighbors().long2ObjectEntrySet().iterator();
    row.startWrite();
    try {
        while (iter.hasNext()) {
            Long2ObjectMap.Entry<NeighborsAliasTableElement> entry = iter.next();
            NeighborsAliasTableElement element = entry.getValue();
            if (element == null) {
                row.set(entry.getLongKey(), null);
            } else {
                row.set(entry.getLongKey(), new NeighborsAliasTableElement(element.getNeighborIds(), element.getAccept(), element.getAlias()));
            }
        }
    } finally {
        row.endWrite();
    }
}
Also used : ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) RowBasedPartition(com.tencent.angel.ps.storage.partition.RowBasedPartition) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) NeighborsAliasTableElement(com.tencent.angel.graph.psf.neighbors.samplebyaliastable.samplealiastable.NeighborsAliasTableElement)

Example 38 with ServerMatrix

use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.

the class GetNeighborAliasTable method partitionGet.

@Override
public PartitionGetResult partitionGet(PartitionGetParam partParam) {
    PartGetNeighborAliasTableParam param = (PartGetNeighborAliasTableParam) 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();
    for (int i = 0; i < nodeIds.length; i++) {
        long nodeId = nodeIds[i];
        // Get node neighbor number
        NeighborsAliasTableElement element = (NeighborsAliasTableElement) (row.get(nodeId));
        if (element == null) {
            neighbors[i] = null;
        } else {
            long[] nodeNeighbors = element.getNeighborIds();
            if (nodeNeighbors == null || nodeNeighbors.length == 0 || count[i] <= 0) {
                neighbors[i] = null;
            } else {
                neighbors[i] = new long[count[i]];
                // start sampling by alias table for count times
                float[] accept = element.getAccept();
                int[] alias = element.getAlias();
                for (int j = 0; j < count[i]; j++) {
                    int index = r.nextInt(nodeNeighbors.length);
                    float ac = r.nextFloat();
                    if (ac < accept[index]) {
                        neighbors[i][j] = nodeNeighbors[index];
                    } else {
                        neighbors[i][j] = nodeNeighbors[alias[index]];
                    }
                }
            }
        }
    }
    return new PartGetNeighborAliasTableResult(part.getPartitionKey().getPartitionId(), neighbors);
}
Also used : ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) RowBasedPartition(com.tencent.angel.ps.storage.partition.RowBasedPartition) ServerLongAnyRow(com.tencent.angel.ps.storage.vector.ServerLongAnyRow) Random(java.util.Random) ServerPartition(com.tencent.angel.ps.storage.partition.ServerPartition)

Example 39 with ServerMatrix

use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.

the class InitOutDegreeFunc method partitionUpdate.

@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
    PartInitOutDegreeParam param = (PartInitOutDegreeParam) partParam;
    ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
    RowBasedPartition part = (RowBasedPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
    ServerLongIntRow row = (ServerLongIntRow) part.getRow(0);
    ObjectIterator<Long2IntMap.Entry> iter = param.getNodeIdToOutDegree().long2IntEntrySet().iterator();
    row.startWrite();
    try {
        while (iter.hasNext()) {
            Long2IntMap.Entry entry = iter.next();
            row.set(entry.getLongKey(), entry.getIntValue());
        }
    } finally {
        row.endWrite();
    }
}
Also used : ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) Long2IntMap(it.unimi.dsi.fastutil.longs.Long2IntMap) RowBasedPartition(com.tencent.angel.ps.storage.partition.RowBasedPartition) ServerLongIntRow(com.tencent.angel.ps.storage.vector.ServerLongIntRow)

Aggregations

ServerMatrix (com.tencent.angel.ps.storage.matrix.ServerMatrix)39 RowBasedPartition (com.tencent.angel.ps.storage.partition.RowBasedPartition)28 ServerPartition (com.tencent.angel.ps.storage.partition.ServerPartition)22 ServerLongAnyRow (com.tencent.angel.ps.storage.vector.ServerLongAnyRow)15 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)5 Random (java.util.Random)5 ServerIntAnyRow (com.tencent.angel.ps.storage.vector.ServerIntAnyRow)4 CSRPartition (com.tencent.angel.ps.storage.partition.CSRPartition)3 IntCSRStorage (com.tencent.angel.ps.storage.partition.storage.IntCSRStorage)3 ServerLongIntRow (com.tencent.angel.ps.storage.vector.ServerLongIntRow)3 AngelException (com.tencent.angel.exception.AngelException)2 MatrixFormat (com.tencent.angel.model.output.format.MatrixFormat)2 Pair (com.tencent.angel.protobuf.generated.MLProtos.Pair)2 ParameterServer (com.tencent.angel.ps.ParameterServer)2 ObjectNotFoundException (com.tencent.angel.ps.server.data.exception.ObjectNotFoundException)2 MatrixStorageManager (com.tencent.angel.ps.storage.MatrixStorageManager)2 ServerAnyAnyRow (com.tencent.angel.ps.storage.vector.ServerAnyAnyRow)2 ServerLongLongRow (com.tencent.angel.ps.storage.vector.ServerLongLongRow)2 ServerRow (com.tencent.angel.ps.storage.vector.ServerRow)2 LongArrayElement (com.tencent.angel.ps.storage.vector.element.LongArrayElement)2