Search in sources :

Example 16 with IntIntVector

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

the class ColumnFormat method saveLongIntRows.

private void saveLongIntRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
    Vector vec = ServerRowUtils.getVector((ServerLongIntRow) rows[0]);
    // int size = rows.size();
    long indexOffset = part.getPartitionKey().getStartCol();
    LongIntsCol col = new LongIntsCol(0, new int[rows.length]);
    if (vec instanceof IntIntVector) {
        IntIntVectorStorage storage = ((IntIntVector) vec).getStorage();
        long startCol = rows[0].getStartCol();
        long endCol = rows[0].getEndCol();
        if (storage.isDense()) {
            for (long i = startCol; i < endCol; i++) {
                col.colId = i;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerLongIntRow) (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] = ((ServerLongIntRow) (rows[j])).get(col.colId);
                    }
                    save(col, output);
                }
            } else {
                ObjectIterator<Int2IntMap.Entry> iter = storage.entryIterator();
                while (iter.hasNext()) {
                    col.colId = iter.next().getIntKey() + indexOffset;
                    for (int j = 0; j < rows.length; j++) {
                        col.colElems[j] = ((ServerLongIntRow) (rows[j])).get(col.colId);
                    }
                    save(col, output);
                }
            }
        }
    } else {
        LongIntVectorStorage storage = ((LongIntVector) vec).getStorage();
        if (saveContext.sortFirst()) {
            long[] 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] = ((ServerLongIntRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        } else {
            ObjectIterator<Long2IntMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                col.colId = iter.next().getLongKey() + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerLongIntRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        }
    }
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 17 with IntIntVector

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

the class ColumnFormat method saveIntIntRows.

private void saveIntIntRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
    Vector vec = ServerRowUtils.getVector((ServerIntIntRow) rows[0]);
    // int size = rows.size();
    int indexOffset = (int) part.getPartitionKey().getStartCol();
    IntIntVectorStorage storage = ((IntIntVector) vec).getStorage();
    IntIntsCol col = new IntIntsCol(0, new int[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] = ((ServerIntIntRow) (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] = ((ServerIntIntRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        } else {
            ObjectIterator<Int2IntMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                col.colId = iter.next().getIntKey() + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerIntIntRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        }
    }
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 18 with IntIntVector

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

the class RBCompIntIntMatrix method diag.

@Override
public Vector diag() {
    int[] resArr = new int[rows.length];
    for (int i = 0; i < rows.length; i++) {
        if (null == rows[i]) {
            resArr[i] = 0;
        } else {
            resArr[i] = rows[i].get(i);
        }
    }
    IntIntDenseVectorStorage storage = new IntIntDenseVectorStorage(resArr);
    return new IntIntVector(getMatrixId(), 0, getClock(), resArr.length, storage);
}
Also used : IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) CompIntIntVector(com.tencent.angel.ml.math2.vector.CompIntIntVector) IntIntDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntDenseVectorStorage)

Example 19 with IntIntVector

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

use of com.tencent.angel.ml.math2.vector.IntIntVector 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)55 Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)50 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)44 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)43 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)33 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)33 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)33 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 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)25 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)23 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)21 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)21 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)21 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20 IntLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage)20