Search in sources :

Example 16 with IntDummyVector

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

the class MixedBinaryInAllExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntDummyVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (!op.isKeepStorage()) {
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
                resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
            }
        }
    }
    int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
    for (int i = 0; i < v1.getDim(); i++) {
        int pidx = (int) (i / subDim);
        int subidx = i % subDim;
        ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
    }
    IntDoubleVector[] res = new IntDoubleVector[parts.length];
    int i = 0;
    for (IntDoubleVector part : parts) {
        res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
        i++;
    }
    v1.setPartitions(res);
    return v1;
}
Also used : IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 17 with IntDummyVector

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasFloatMatrix mat, boolean trans, IntDummyVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    float[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new float[n];
    } else {
        assert n == v.getDim();
        resArr = new float[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    float[] data = mat.getData();
    if (trans) {
        for (int j = 0; j < c; j++) {
            int[] idxs = v.getIndices();
            for (int i : idxs) {
                resArr[j] += data[i * c + j];
            }
        }
    } else {
        for (int i = 0; i < r; i++) {
            int[] idxs = v.getIndices();
            for (int j : idxs) {
                resArr[i] += data[i * c + j];
            }
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 18 with IntDummyVector

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

the class SimpleBinaryOutAllExecutor method apply.

public static Vector apply(IntDoubleVector v1, IntDummyVector v2, Binary op) {
    IntDoubleVector res;
    if (v1.isDense()) {
        res = v1.copy();
        double[] resValues = res.getStorage().getValues();
        for (int i = 0; i < v1.getDim(); i++) {
            resValues[i] = op.apply(resValues[i], v2.get(i));
        }
    } else if (v1.isSparse()) {
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
            double[] resValues = newStorage.getValues();
            ObjectIterator<Int2DoubleMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2DoubleMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                resValues[idx] = entry.getDoubleValue();
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    } else {
        // sorted
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
            double[] resValues = newStorage.getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            double[] v1Values = v1.getStorage().getValues();
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = v1Values[i];
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    }
    return res;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 19 with IntDummyVector

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

the class SimpleBinaryOutAllExecutor method apply.

public static Vector apply(IntLongVector v1, IntDummyVector v2, Binary op) {
    IntLongVector res;
    if (v1.isDense()) {
        res = v1.copy();
        long[] resValues = res.getStorage().getValues();
        for (int i = 0; i < v1.getDim(); i++) {
            resValues[i] = op.apply(resValues[i], v2.get(i));
        }
    } else if (v1.isSparse()) {
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntLongVectorStorage newStorage = v1.getStorage().emptyDense();
            long[] resValues = newStorage.getValues();
            ObjectIterator<Int2LongMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2LongMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                resValues[idx] = entry.getLongValue();
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntLongVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    } else {
        // sorted
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntLongVectorStorage newStorage = v1.getStorage().emptyDense();
            long[] resValues = newStorage.getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            long[] v1Values = v1.getStorage().getValues();
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = v1Values[i];
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntLongVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    }
    return res;
}
Also used : IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) AngelException(com.tencent.angel.exception.AngelException) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 20 with IntDummyVector

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

the class SimpleBinaryOutNonZAExecutor method apply.

public static Vector apply(IntIntVector v1, IntDummyVector v2, Binary op) {
    IntIntVectorStorage newStorage = (IntIntVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isDense()) {
        int[] resValues = newStorage.getValues();
        int[] v2Indices = v2.getIndices();
        for (int idx : v2Indices) {
            resValues[idx] = op.apply(resValues[idx], 1);
        }
    } else if (v1.isSparse()) {
        int[] v2Indices = v2.getIndices();
        if (((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sparseDenseStorageThreshold * v1.getDim())) {
            int[] resValues = newStorage.getValues();
            ObjectIterator<Int2IntMap.Entry> iter = v1.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                newStorage.set(entry.getIntKey(), entry.getIntValue());
            }
            for (int idx : v2Indices) {
                newStorage.set(idx, op.apply(v1.get(idx), 1));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() < 1.5 * capacity) {
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), 1));
                }
            } else {
                ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2IntMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getIntValue());
                }
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), 1));
                }
            }
        }
    } else {
        // sorted
        int[] v1Indices = v1.getStorage().getIndices();
        int[] v2Indices = v2.getIndices();
        if (!op.isKeepStorage() && ((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sortedDenseStorageThreshold * v1.getDim())) {
            int[] v1Values = v1.getStorage().getValues();
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                newStorage.set(v1Indices[i], v1Values[i]);
            }
            size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(newStorage.get(idx), 1));
            }
        } else {
            int v1Pointor = 0;
            int v2Pointor = 0;
            int size1 = v1.size();
            int size2 = v2.size();
            int[] v1Values = v1.getStorage().getValues();
            while (v1Pointor < size1 || v2Pointor < size2) {
                if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                    newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], 1));
                    v1Pointor++;
                    v2Pointor++;
                } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                    newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                    v1Pointor++;
                } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                    newStorage.set(v2Indices[v2Pointor], op.apply(0, 1));
                    v2Pointor++;
                }
            }
        }
    }
    return new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
Also used : IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Aggregations

IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)26 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)26 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)26 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)26 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)24 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)24 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)24 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)24 Storage (com.tencent.angel.ml.math2.storage.Storage)24 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)18 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)16 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)16 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)16 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)16 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)16 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)16 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)16 IntLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage)16 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)16 LongDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage)16