Search in sources :

Example 36 with LongFloatVector

use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.

the class StreamSerdeUtils method deserializeLongFloatVector.

private static LongFloatVector deserializeLongFloatVector(DataInputStream in, long dim) throws IOException {
    int storageType = deserializeInt(in);
    switch(storageType) {
        case SPARSE_STORAGE_TYPE:
            int len = deserializeInt(in);
            Long2FloatOpenHashMap idToValueMap = new Long2FloatOpenHashMap(len);
            for (int i = 0; i < len; i++) {
                idToValueMap.put(deserializeInt(in), deserializeFloat(in));
            }
            return new LongFloatVector((int) dim, new LongFloatSparseVectorStorage((int) dim, idToValueMap));
        case SORTED_STORAGE_TYPE:
            return VFactory.sortedLongKeyFloatVector((int) dim, deserializeLongs(in), deserializeFloats(in));
        default:
            throw new UnsupportedOperationException("Unsupport storage type " + storageType);
    }
}
Also used : Long2FloatOpenHashMap(it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)

Example 37 with LongFloatVector

use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.

the class ByteBufSerdeUtils method deserializeLongFloatVector.

private static LongFloatVector deserializeLongFloatVector(ByteBuf in, long dim) {
    int storageType = deserializeInt(in);
    switch(storageType) {
        case SPARSE_STORAGE_TYPE:
            int len = deserializeInt(in);
            Long2FloatOpenHashMap idToValueMap = new Long2FloatOpenHashMap(len);
            for (int i = 0; i < len; i++) {
                idToValueMap.put(deserializeInt(in), deserializeFloat(in));
            }
            return new LongFloatVector((int) dim, new LongFloatSparseVectorStorage((int) dim, idToValueMap));
        case SORTED_STORAGE_TYPE:
            return VFactory.sortedLongKeyFloatVector((int) dim, deserializeLongs(in), deserializeFloats(in));
        default:
            throw new UnsupportedOperationException("Unsupport storage type " + storageType);
    }
}
Also used : Long2FloatOpenHashMap(it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)

Example 38 with LongFloatVector

use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.

the class SnapshotFormat method save.

private void save(ServerLongFloatRow row, PSMatrixSaveContext saveContext, MatrixPartitionMeta meta, DataOutputStream out) throws IOException {
    long startCol = meta.getStartCol();
    if (ServerRowUtils.getVector(row) instanceof IntFloatVector) {
        IntFloatVector vector = (IntFloatVector) ServerRowUtils.getVector(row);
        if (vector.isDense()) {
            float[] data = vector.getStorage().getValues();
            for (int i = 0; i < data.length; i++) {
                out.writeFloat(data[i]);
            }
        } else if (vector.isSorted()) {
            int[] indices = vector.getStorage().getIndices();
            float[] values = vector.getStorage().getValues();
            for (int i = 0; i < indices.length; i++) {
                out.writeLong(indices[i] + startCol);
                out.writeFloat(values[i]);
            }
        } else {
            ObjectIterator<Int2FloatMap.Entry> iter = vector.getStorage().entryIterator();
            Int2FloatMap.Entry entry;
            while (iter.hasNext()) {
                entry = iter.next();
                out.writeLong(entry.getIntKey() + startCol);
                out.writeFloat(entry.getFloatValue());
            }
        }
    } else {
        LongFloatVector vector = (LongFloatVector) ServerRowUtils.getVector(row);
        if (vector.isSorted()) {
            long[] indices = vector.getStorage().getIndices();
            float[] values = vector.getStorage().getValues();
            for (int i = 0; i < indices.length; i++) {
                out.writeLong(indices[i] + startCol);
                out.writeFloat(values[i]);
            }
        } else {
            ObjectIterator<Long2FloatMap.Entry> iter = vector.getStorage().entryIterator();
            Long2FloatMap.Entry entry;
            while (iter.hasNext()) {
                entry = iter.next();
                out.writeLong(entry.getLongKey() + startCol);
                out.writeFloat(entry.getFloatValue());
            }
        }
    }
}
Also used : Long2FloatMap(it.unimi.dsi.fastutil.longs.Long2FloatMap) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 39 with LongFloatVector

use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.

the class HashRouterUtils method splitLongFloatVector.

public static void splitLongFloatVector(KeyHash hasher, MatrixMeta matrixMeta, LongFloatVector vector, KeyValuePart[] dataParts) {
    int dataPartNum = dataParts.length;
    int dataPartNumMinus1 = dataPartNum - 1;
    if (isPow2(dataPartNum)) {
        LongFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            LongFloatSparseVectorStorage sparseStorage = (LongFloatSparseVectorStorage) storage;
            ObjectIterator<Long2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Long2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getLongKey()) & dataPartNumMinus1;
                ((HashLongKeysFloatValuesPart) dataParts[partId]).add(keyValue.getLongKey(), keyValue.getFloatValue());
            }
        } else {
            // Key and value array pair
            LongFloatSortedVectorStorage sortStorage = (LongFloatSortedVectorStorage) storage;
            long[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
                ((HashLongKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    } else {
        LongFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            LongFloatSparseVectorStorage sparseStorage = (LongFloatSparseVectorStorage) storage;
            ObjectIterator<Long2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Long2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getLongKey()) % dataPartNum;
                ((HashLongKeysFloatValuesPart) dataParts[partId]).add(keyValue.getLongKey(), keyValue.getFloatValue());
            }
        } else {
            // Key and value array pair
            LongFloatSortedVectorStorage sortStorage = (LongFloatSortedVectorStorage) storage;
            long[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
                ((HashLongKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    }
}
Also used : LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) Long2FloatMap(it.unimi.dsi.fastutil.longs.Long2FloatMap) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)

Example 40 with LongFloatVector

use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.

the class HashRouterUtils method split.

/**
 * Split keys by matrix partition
 *
 * @param matrixMeta matrix meta data
 * @param vector Matrix vector
 * @return partition key to key partition map
 */
public static KeyValuePart[] split(MatrixMeta matrixMeta, Vector vector) {
    KeyHash hasher = HasherFactory.getHasher(matrixMeta.getRouterHash());
    PartitionKey[] matrixParts = matrixMeta.getPartitionKeys();
    KeyValuePart[] dataParts = new KeyValuePart[matrixParts.length];
    int estSize = (int) (vector.getSize() / matrixMeta.getPartitionNum());
    for (int i = 0; i < dataParts.length; i++) {
        dataParts[i] = generateDataPart(vector.getRowId(), vector.getType(), estSize);
    }
    switch(vector.getType()) {
        case T_DOUBLE_DENSE:
        case T_DOUBLE_SPARSE:
            {
                splitIntDoubleVector(hasher, matrixMeta, (IntDoubleVector) vector, dataParts);
                break;
            }
        case T_FLOAT_DENSE:
        case T_FLOAT_SPARSE:
            {
                splitIntFloatVector(hasher, matrixMeta, (IntFloatVector) vector, dataParts);
                break;
            }
        case T_INT_DENSE:
        case T_INT_SPARSE:
            {
                splitIntIntVector(hasher, matrixMeta, (IntIntVector) vector, dataParts);
                break;
            }
        case T_LONG_DENSE:
        case T_LONG_SPARSE:
            {
                splitIntLongVector(hasher, matrixMeta, (IntLongVector) vector, dataParts);
                break;
            }
        case T_DOUBLE_SPARSE_LONGKEY:
            {
                splitLongDoubleVector(hasher, matrixMeta, (LongDoubleVector) vector, dataParts);
                break;
            }
        case T_FLOAT_SPARSE_LONGKEY:
            {
                splitLongFloatVector(hasher, matrixMeta, (LongFloatVector) vector, dataParts);
                break;
            }
        case T_INT_SPARSE_LONGKEY:
            {
                splitLongIntVector(hasher, matrixMeta, (LongIntVector) vector, dataParts);
                break;
            }
        case T_LONG_SPARSE_LONGKEY:
            {
                splitLongLongVector(hasher, matrixMeta, (LongLongVector) vector, dataParts);
                break;
            }
        default:
            {
                throw new UnsupportedOperationException("Unsupport vector type " + vector.getType());
            }
    }
    for (int i = 0; i < dataParts.length; i++) {
        if (dataParts[i] != null) {
            dataParts[i].setRowId(vector.getRowId());
        }
    }
    return dataParts;
}
Also used : IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) CompStreamKeyValuePart(com.tencent.angel.psagent.matrix.transport.router.CompStreamKeyValuePart) KeyValuePart(com.tencent.angel.psagent.matrix.transport.router.KeyValuePart) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) KeyHash(com.tencent.angel.psagent.matrix.transport.router.KeyHash) PartitionKey(com.tencent.angel.PartitionKey)

Aggregations

LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)55 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)50 LongFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)37 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)34 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)33 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)32 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)30 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)30 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)30 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)30 Storage (com.tencent.angel.ml.math2.storage.Storage)30 CompLongFloatVector (com.tencent.angel.ml.math2.vector.CompLongFloatVector)28 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)25 Long2FloatMap (it.unimi.dsi.fastutil.longs.Long2FloatMap)23 LongDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage)22 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)21 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)20 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)20 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20