Search in sources :

Example 41 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class SnapshotFormat method save.

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

Example 42 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class SnapshotFormat method save.

private void save(ServerIntIntRow row, PSMatrixSaveContext saveContext, MatrixPartitionMeta meta, DataOutputStream out) throws IOException {
    int startCol = (int) meta.getStartCol();
    IntIntVector vector = ServerRowUtils.getVector(row);
    if (vector.isDense()) {
        int[] data = vector.getStorage().getValues();
        for (int i = 0; i < data.length; i++) {
            out.writeInt(data[i]);
        }
    } else if (vector.isSorted()) {
        int[] indices = vector.getStorage().getIndices();
        int[] values = vector.getStorage().getValues();
        for (int i = 0; i < indices.length; i++) {
            out.writeInt(indices[i] + startCol);
            out.writeInt(values[i]);
        }
    } else {
        ObjectIterator<Int2IntMap.Entry> iter = vector.getStorage().entryIterator();
        Int2IntMap.Entry entry;
        while (iter.hasNext()) {
            entry = iter.next();
            out.writeInt(entry.getIntKey() + startCol);
            out.writeInt(entry.getIntValue());
        }
    }
}
Also used : IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 43 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class SnapshotFormat method save.

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

Example 44 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class BlasFloatMatrix method setRow.

public Matrix setRow(int i, Vector v) {
    if (v instanceof IntFloatVector) {
        float[] rowData;
        if (v.isDense()) {
            rowData = ((IntFloatVector) v).getStorage().getValues();
        } else if (v.isSparse()) {
            rowData = new float[numCols];
            ObjectIterator<Int2FloatMap.Entry> iter = ((IntFloatVector) v).getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                rowData[j] = entry.getFloatValue();
            }
        } else {
            // sorted
            rowData = new float[numCols];
            int[] idxs = ((IntFloatVector) v).getStorage().getIndices();
            float[] values = ((IntFloatVector) v).getStorage().getValues();
            int size = ((IntFloatVector) v).size();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                rowData[j] = values[k];
            }
        }
        System.arraycopy(rowData, 0, data, i * numCols, numCols);
    } else if (v instanceof IntLongVector) {
        long[] rowData;
        if (v.isDense()) {
            rowData = ((IntLongVector) v).getStorage().getValues();
        } else if (v.isSparse()) {
            rowData = new long[numCols];
            ObjectIterator<Int2LongMap.Entry> iter = ((IntLongVector) v).getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                rowData[j] = entry.getLongValue();
            }
        } else {
            // sorted
            rowData = new long[numCols];
            int[] idxs = ((IntLongVector) v).getStorage().getIndices();
            long[] values = ((IntLongVector) v).getStorage().getValues();
            int size = ((IntLongVector) v).size();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                rowData[j] = values[k];
            }
        }
        for (int j = 0; j < numCols; j++) {
            data[i * numCols + j] = rowData[j];
        }
    } else if (v instanceof IntIntVector) {
        int[] rowData;
        if (v.isDense()) {
            rowData = ((IntIntVector) v).getStorage().getValues();
        } else if (v.isSparse()) {
            rowData = new int[numCols];
            ObjectIterator<Int2IntMap.Entry> iter = ((IntIntVector) v).getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                rowData[j] = entry.getIntValue();
            }
        } else {
            // sorted
            rowData = new int[numCols];
            int[] idxs = ((IntIntVector) v).getStorage().getIndices();
            int[] values = ((IntIntVector) v).getStorage().getValues();
            int size = ((IntIntVector) v).size();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                rowData[j] = values[k];
            }
        }
        for (int j = 0; j < numCols; j++) {
            data[i * numCols + j] = rowData[j];
        }
    } else if (v instanceof IntDummyVector) {
        int[] rowData = new int[numCols];
        int[] idxs = ((IntDummyVector) v).getIndices();
        int size = ((IntDummyVector) v).size();
        for (int k = 0; k < size; k++) {
            int j = idxs[k];
            rowData[j] = 1;
        }
        for (int j = 0; j < numCols; j++) {
            data[i * numCols + j] = rowData[j];
        }
    } else {
        throw new AngelException("The operation is not supported!");
    }
    return this;
}
Also used : IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) AngelException(com.tencent.angel.exception.AngelException) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDummyVector(com.tencent.angel.ml.math2.vector.IntDummyVector) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 45 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class MixedDotExecutor method apply.

private static double apply(CompIntDoubleVector v1, IntIntVector v2) {
    double dotValue = 0.0;
    if (v2.isDense()) {
        int base = 0;
        int[] v2Values = v2.getStorage().getValues();
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2Values[idx];
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2Values[idx];
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2Values[idx];
                }
            }
            base += part.getDim();
        }
    } else if (v2.isSparse()) {
        ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2IntMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            dotValue += v1.get(idx) * entry.getIntValue();
        }
    } else if (v2.isSorted() && v1.size() > v2.size()) {
        // v2 is sorted
        int[] v2Indices = v2.getStorage().getIndices();
        int[] v2Values = v2.getStorage().getValues();
        for (int i = 0; i < v2Indices.length; i++) {
            int idx = v2Indices[i];
            dotValue += v1.get(idx) * v2Values[i];
        }
    } else {
        int base = 0;
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2.get(idx);
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2.get(idx);
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2.get(idx);
                }
            }
            base += part.getDim();
        }
    }
    return dotValue;
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Aggregations

ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)254 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)115 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)114 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)111 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)110 AngelException (com.tencent.angel.exception.AngelException)100 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)100 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)99 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)98 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)97 Storage (com.tencent.angel.ml.math2.storage.Storage)96 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)62 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)57 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)56 Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)55 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)47 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)44 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)44 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)43 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)43