Search in sources :

Example 66 with IntFloatVector

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

the class ByteBufSerdeUtils method serializeIntFloatVector.

// IntFloatVector
private static void serializeIntFloatVector(ByteBuf out, IntFloatVector vector) {
    IntFloatVectorStorage storage = vector.getStorage();
    if (storage.isDense()) {
        serializeInt(out, DENSE_STORAGE_TYPE);
        serializeFloats(out, storage.getValues());
    } else if (storage.isSparse()) {
        serializeInt(out, SPARSE_STORAGE_TYPE);
        serializeInt(out, storage.size());
        ObjectIterator<Int2FloatMap.Entry> iter = storage.entryIterator();
        while (iter.hasNext()) {
            Int2FloatMap.Entry e = iter.next();
            serializeInt(out, e.getIntKey());
            serializeFloat(out, e.getFloatValue());
        }
    } else if (storage.isSorted()) {
        serializeInt(out, SORTED_STORAGE_TYPE);
        int[] indices = vector.getStorage().getIndices();
        float[] values = vector.getStorage().getValues();
        serializeInts(out, indices);
        serializeFloats(out, values);
    } else {
        throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
    }
}
Also used : IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Entry(it.unimi.dsi.fastutil.ints.Int2DoubleMap.Entry) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 67 with IntFloatVector

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

the class ByteBufSerdeUtils method serializedIntFloatVectorLen.

public static int serializedIntFloatVectorLen(IntFloatVector vector) {
    int len = 0;
    IntFloatVectorStorage storage = vector.getStorage();
    if (storage.isDense()) {
        len += serializedIntLen(DENSE_STORAGE_TYPE);
        len += serializedFloatsLen(storage.getValues());
    } else if (storage.isSparse()) {
        len += serializedIntLen(SPARSE_STORAGE_TYPE);
        len += serializedIntLen(storage.size());
        len += storage.size() * (INT_LENGTH + FLOAT_LENGTH);
    } else if (storage.isSorted()) {
        len += serializedIntLen(SORTED_STORAGE_TYPE);
        len += serializedIntsLen(vector.getStorage().getIndices());
        len += serializedFloatsLen(vector.getStorage().getValues());
    } else {
        throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
    }
    return len;
}
Also used : IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)

Example 68 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector 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 69 with IntFloatVector

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

the class ColumnFormat method saveIntFloatRows.

private void saveIntFloatRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
    Vector vec = ServerRowUtils.getVector((ServerIntFloatRow) rows[0]);
    // int size = rows.length;
    int indexOffset = (int) part.getPartitionKey().getStartCol();
    IntFloatVectorStorage storage = ((IntFloatVector) vec).getStorage();
    IntFloatsCol col = new IntFloatsCol(0, new float[rows.length]);
    int startCol = (int) rows[0].getStartCol();
    int endCol = (int) rows[0].getEndCol();
    if (storage.isDense()) {
        for (int i = startCol; i < endCol; i++) {
            col.colId = i;
            for (int j = 0; j < rows.length; j++) {
                col.colElems[j] = ((ServerIntFloatRow) (rows[j])).get(col.colId);
            }
            save(col, output);
        }
    } else {
        if (saveContext.sortFirst()) {
            int[] indices = storage.getIndices();
            Sort.quickSort(indices, 0, indices.length - 1);
            for (int i = 0; i < indices.length; i++) {
                col.colId = indices[i] + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerIntFloatRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        } else {
            ObjectIterator<Int2FloatMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                col.colId = iter.next().getIntKey() + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerIntFloatRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        }
    }
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 70 with IntFloatVector

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

the class HashRouterUtils method splitIntFloatVector.

public static void splitIntFloatVector(KeyHash hasher, MatrixMeta matrixMeta, IntFloatVector vector, KeyValuePart[] dataParts) {
    int dataPartNum = dataParts.length;
    int dataPartNumMinus1 = dataPartNum - 1;
    if (isPow2(dataPartNum)) {
        IntFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
            ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
            float[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    } else {
        IntFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
            ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
            float[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    }
}
Also used : IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap)

Aggregations

IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)104 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)60 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)57 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)50 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)34 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)33 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)32 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)32 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)31 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)30 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)30 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)30 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)30 Storage (com.tencent.angel.ml.math2.storage.Storage)30 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)25 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)21 AngelException (com.tencent.angel.exception.AngelException)20 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20