Search in sources :

Example 86 with IntDoubleVector

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

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat, IntDoubleVector v, int idx, boolean onCol, Binary op) {
    double[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = null;
    if (!v.isDense()) {
        flag = new byte[v.getDim()];
    }
    if (onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                data[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                newData[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            newData[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                data[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return mat;
    } else {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                newData[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            newData[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 87 with IntDoubleVector

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

the class UpdatePSFTest method testSparseDoubleUDF.

public void testSparseDoubleUDF() throws Exception {
    Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
    MatrixClient client1 = worker.getPSAgent().getMatrixClient(SPARSE_DOUBLE_MAT, 0);
    int matrixW1Id = client1.getMatrixId();
    // genIndexs(feaNum, nnz);
    int[] index = new int[feaNum];
    for (int i = 0; i < index.length; i++) {
        index[i] = i;
    }
    IntDoubleVector deltaVec = new IntDoubleVector(feaNum, new IntDoubleSparseVectorStorage(feaNum, nnz));
    for (int i = 0; i < index.length; i++) {
        deltaVec.set(index[i], index[i]);
    }
    // for (int i = 0; i < feaNum; i++) {
    // deltaVec.set(i, i);
    // }
    deltaVec.setRowId(0);
    Vector[] updates = new Vector[1];
    updates[0] = deltaVec;
    client1.asyncUpdate(new IncrementRows(new IncrementRowsParam(matrixW1Id, updates))).get();
    IntDoubleVector row = (IntDoubleVector) client1.getRow(0);
    for (int id : index) {
        // System.out.println("id=" + id + ", value=" + row.get(id));
        Assert.assertEquals(row.get(id), deltaVec.get(id), 0);
    }
    Assert.assertTrue(index.length == row.size());
}
Also used : IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IncrementRowsParam(com.tencent.angel.ml.matrix.psf.update.update.IncrementRowsParam) IncrementRows(com.tencent.angel.ml.matrix.psf.update.update.IncrementRows) Worker(com.tencent.angel.worker.Worker) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) Vector(com.tencent.angel.ml.math2.vector.Vector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 88 with IntDoubleVector

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

the class IndexGetRowTask method run.

@Override
public void run(TaskContext taskContext) throws AngelException {
    try {
        MatrixClient matrixClient = taskContext.getMatrix(IndexGetRowTest.DENSE_DOUBLE_MAT);
        MatrixMeta matrixMeta = PSAgentContext.get().getMatrixMetaManager().getMatrixMeta(IndexGetRowTest.DENSE_DOUBLE_MAT);
        int[] indices = genIndexs((int) matrixMeta.getColNum(), IndexGetRowTest.nnz);
        IntDoubleVector delta = VFactory.sparseDoubleVector((int) matrixMeta.getColNum(), IndexGetRowTest.nnz);
        for (int i = 0; i < IndexGetRowTest.nnz; i++) {
            delta.set(indices[i], indices[i]);
        }
        IntDoubleVector delta1 = VFactory.denseDoubleVector((int) matrixMeta.getColNum());
        for (int i = 0; i < IndexGetRowTest.colNum; i++) {
            delta1.set(i, i);
        }
        LOG.info("delta use " + delta.getType() + " type storage");
        int testNum = 500;
        long startTs = System.currentTimeMillis();
        LOG.info("for sparse delta type");
        conf.setBoolean("use.new.split", false);
        for (int i = 0; i < testNum; i++) {
            matrixClient.increment(0, delta, true);
            if (i > 0 && i % 10 == 0) {
                LOG.info("increment old use time = " + (System.currentTimeMillis() - startTs) / i);
            }
        }
        conf.setBoolean("use.new.split", true);
        startTs = System.currentTimeMillis();
        for (int i = 0; i < testNum; i++) {
            matrixClient.increment(0, delta, true);
            // IntDoubleVector vector = (IntDoubleVector) ((GetRowResult) matrixClient.get(func)).getRow();
            if (i > 0 && i % 10 == 0) {
                LOG.info("increment new use time = " + (System.currentTimeMillis() - startTs) / i);
            }
        }
        LOG.info("for dense delta type");
        conf.setBoolean("use.new.split", false);
        for (int i = 0; i < testNum; i++) {
            matrixClient.increment(0, delta1, true);
            if (i > 0 && i % 10 == 0) {
                LOG.info("increment old use time = " + (System.currentTimeMillis() - startTs) / i);
            }
        }
        conf.setBoolean("use.new.split", true);
        startTs = System.currentTimeMillis();
        for (int i = 0; i < testNum; i++) {
            matrixClient.increment(0, delta1, true);
            // IntDoubleVector vector = (IntDoubleVector) ((GetRowResult) matrixClient.get(func)).getRow();
            if (i > 0 && i % 10 == 0) {
                LOG.info("increment new use time = " + (System.currentTimeMillis() - startTs) / i);
            }
        }
    } catch (InvalidParameterException e) {
        LOG.error("get matrix failed ", e);
        throw new AngelException(e);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) InvalidParameterException(com.tencent.angel.exception.InvalidParameterException) MatrixMeta(com.tencent.angel.ml.matrix.MatrixMeta) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 89 with IntDoubleVector

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

the class RBCompLongDoubleMatrix method diag.

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

Example 90 with IntDoubleVector

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

the class RBIntDoubleMatrix method diag.

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

Aggregations

IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)95 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)55 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)51 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)51 CompIntDoubleVector (com.tencent.angel.ml.math2.vector.CompIntDoubleVector)40 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)32 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)32 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)32 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)32 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 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)26 IntDoubleDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage)23 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20