Search in sources :

Example 6 with MatrixClient

use of com.tencent.angel.psagent.matrix.MatrixClient in project angel by Tencent.

the class TransportTest method testGetDenseIntMatrix.

@Test
public void testGetDenseIntMatrix() throws Exception {
    try {
        Worker worker = LocalClusterContext.get().getWorker(worker0Attempt0Id).getWorker();
        MatrixClient mat = worker.getPSAgent().getMatrixClient("dense_int_mat", 0);
        Random rand = new Random(System.currentTimeMillis());
        for (int rowId = 0; rowId < diRow; rowId += rand.nextInt(5) + 1) {
            DenseIntVector row = (DenseIntVector) mat.getRow(rowId);
            DenseIntVector expect = new DenseIntVector(diCol);
            assertArrayEquals(row.getValues(), expect.getValues());
            DenseIntVector update = new DenseIntVector(diCol);
            update.setRowId(rowId);
            for (int i = 0; i < ddCol; i += 2) update.set(i, rand.nextInt());
            mat.increment(update);
            mat.clock().get();
            row = (DenseIntVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues());
            update = new DenseIntVector(diCol);
            update.setRowId(rowId);
            for (int i = 0; i < diCol; i += 3) update.set(i, rand.nextInt());
            mat.increment(update);
            mat.clock().get();
            row = (DenseIntVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues());
        }
    } catch (Exception x) {
        LOG.error("run testGetDenseIntMatrix failed ", x);
        throw x;
    }
}
Also used : Random(java.util.Random) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IOException(java.io.IOException) DenseIntVector(com.tencent.angel.ml.math.vector.DenseIntVector) MasterServiceTest(com.tencent.angel.master.MasterServiceTest) Test(org.junit.Test)

Example 7 with MatrixClient

use of com.tencent.angel.psagent.matrix.MatrixClient in project angel by Tencent.

the class TransportTest method testGetFlowDenseDoubleMatrix.

@Test
public void testGetFlowDenseDoubleMatrix() throws Exception {
    try {
        Worker worker = LocalClusterContext.get().getWorker(worker0Attempt0Id).getWorker();
        MatrixClient mat = worker.getPSAgent().getMatrixClient("dense_double_mat_1", 0);
        double[][] data = new double[ddRow][ddCol];
        DenseDoubleMatrix expect = new DenseDoubleMatrix(ddRow, ddCol, data);
        RowIndex rowIndex = new RowIndex();
        for (int i = 0; i < ddRow; i++) rowIndex.addRowId(i);
        GetRowsResult result = mat.getRowsFlow(rowIndex, ddRow / 2);
        TVector row;
        while ((row = result.take()) != null) {
            LOG.info("===========get row index=" + row.getRowId());
            assertArrayEquals(((DenseDoubleVector) expect.getRow(row.getRowId())).getValues(), ((DenseDoubleVector) row).getValues(), 0.0);
        }
        Random rand = new Random(System.currentTimeMillis());
        for (int rowId = 0; rowId < ddRow; rowId++) {
            DenseDoubleVector update = new DenseDoubleVector(ddCol);
            for (int j = 0; j < ddCol; j += 3) update.set(j, rand.nextDouble());
            mat.increment(rowId, update);
            expect.getRow(rowId).plusBy(update);
        }
        mat.clock().get();
        rowIndex = new RowIndex();
        for (int i = 0; i < ddRow; i++) rowIndex.addRowId(i);
        result = mat.getRowsFlow(rowIndex, 2);
        while ((row = result.take()) != null) {
            assertArrayEquals(((DenseDoubleVector) expect.getRow(row.getRowId())).getValues(), ((DenseDoubleVector) row).getValues(), 0.0);
        }
        rowIndex = new RowIndex();
        for (int i = 0; i < ddRow; i++) rowIndex.addRowId(i);
        result = mat.getRowsFlow(rowIndex, 2);
        while (true) {
            row = result.poll();
            if (result.isFetchOver() && row == null)
                break;
            if (row == null)
                continue;
            assertArrayEquals(((DenseDoubleVector) expect.getRow(row.getRowId())).getValues(), ((DenseDoubleVector) row).getValues(), 0.0);
        }
    } catch (Exception x) {
        LOG.error("run testGetFlowDenseDoubleMatrix failed ", x);
        throw x;
    }
}
Also used : RowIndex(com.tencent.angel.psagent.matrix.transport.adapter.RowIndex) Random(java.util.Random) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) GetRowsResult(com.tencent.angel.psagent.matrix.transport.adapter.GetRowsResult) DenseDoubleMatrix(com.tencent.angel.ml.math.matrix.DenseDoubleMatrix) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) TVector(com.tencent.angel.ml.math.TVector) IOException(java.io.IOException) MasterServiceTest(com.tencent.angel.master.MasterServiceTest) Test(org.junit.Test)

Example 8 with MatrixClient

use of com.tencent.angel.psagent.matrix.MatrixClient in project angel by Tencent.

the class TransportTest method testGetDenseFloatMatrix.

@Test
public void testGetDenseFloatMatrix() throws Exception {
    try {
        Worker worker = LocalClusterContext.get().getWorker(worker0Attempt0Id).getWorker();
        MatrixClient mat = worker.getPSAgent().getMatrixClient("dense_float_mat", 0);
        Random rand = new Random(System.currentTimeMillis());
        for (int rowId = 0; rowId < dfRow; rowId += (rand.nextInt(4) + 1)) {
            LOG.info("=================get row " + rowId);
            DenseFloatVector getRow = (DenseFloatVector) mat.getRow(rowId);
            DenseFloatVector expect = new DenseFloatVector(dfCol);
            assertArrayEquals(getRow.getValues(), expect.getValues(), 0.0F);
            DenseFloatVector update = new DenseFloatVector(dfCol);
            update.setRowId(rowId);
            for (int i = 0; i < ddCol; i += 2) update.set(i, rand.nextFloat());
            mat.increment(update);
            mat.clock().get();
            DenseFloatVector row = (DenseFloatVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues(), 0.0F);
            update = new DenseFloatVector(ddCol);
            update.setRowId(rowId);
            for (int i = 0; i < ddCol; i += 3) update.set(i, rand.nextFloat());
            mat.increment(update);
            mat.clock().get();
            row = (DenseFloatVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues(), 0.0F);
        }
    } catch (Exception x) {
        LOG.error("run testGetDenseFloatMatrix failed ", x);
        throw x;
    }
}
Also used : DenseFloatVector(com.tencent.angel.ml.math.vector.DenseFloatVector) Random(java.util.Random) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IOException(java.io.IOException) MasterServiceTest(com.tencent.angel.master.MasterServiceTest) Test(org.junit.Test)

Example 9 with MatrixClient

use of com.tencent.angel.psagent.matrix.MatrixClient in project angel by Tencent.

the class GetValueOfIndexTask method sparseDouble.

public void sparseDouble(TaskContext tContext) {
    long startGen = System.currentTimeMillis();
    int[] index = genIndexs(feaNum, nnz);
    long cost = System.currentTimeMillis() - startGen;
    LOG.info("Gen index cost: " + cost + " ms.");
    try {
        MatrixClient sMatClient = tContext.getMatrix(SPARSE_DOUBLE_MAT);
        // Set PS Model values
        long startInc = System.currentTimeMillis();
        SparseDoubleVector delt = new SparseDoubleVector(feaNum);
        for (int i = 0; i < feaNum; i++) delt.set(i, (double) i);
        sMatClient.increment(0, delt);
        sMatClient.clock().get();
        long costInc = System.currentTimeMillis() - startInc;
        LOG.info("Increment delt cost " + costInc + " ms.");
        // Wait for all tasks finish this clock
        sMatClient.getTaskContext().globalSync(sMatClient.getMatrixId());
        // Get values of index array
        long startGet = System.currentTimeMillis();
        IndexGetFunc func = new IndexGetFunc(new IndexGetParam(tContext.getMatrix(DENSE_DOUBLE_MAT).getMatrixId(), 0, index));
        SparseDoubleVector row = (SparseDoubleVector) ((GetRowResult) sMatClient.get(func)).getRow();
        long costGet = System.currentTimeMillis() - startGet;
        LOG.info("Get row of indexs cost " + costGet + " ms.");
    } catch (Throwable e) {
        throw new AngelException(e);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IndexGetFunc(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetFunc) IndexGetParam(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetParam) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector)

Example 10 with MatrixClient

use of com.tencent.angel.psagent.matrix.MatrixClient in project angel by Tencent.

the class LongKeyTestTask method run.

@Override
public void run(TaskContext taskContext) throws AngelException {
    try {
        MatrixClient client = taskContext.getMatrix("longkey_test");
        while (taskContext.getEpoch() < 100) {
            long startTs = System.currentTimeMillis();
            TVector row = client.getRow(0);
            LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " pull use time=" + (System.currentTimeMillis() - startTs) + ", sum=" + ((CompSparseLongKeyDoubleVector) row).sum());
            startTs = System.currentTimeMillis();
            CompSparseLongKeyDoubleVector deltaV = new CompSparseLongKeyDoubleVector(client.getMatrixId(), 0, 2100000000, 110000000);
            SparseLongKeyDoubleVector deltaV1 = new SparseLongKeyDoubleVector(2100000000, 150000000);
            DenseDoubleVector deltaV2 = new DenseDoubleVector(110000000);
            for (int i = 0; i < 2100000000; i += 20) {
                deltaV.set(i, 1.0);
                deltaV1.set(i, 1.0);
            }
            for (int i = 0; i < 110000000; i++) {
                deltaV2.set(i, 1.0);
            }
            startTs = System.currentTimeMillis();
            int tryNum = 100;
            while (tryNum-- > 0) {
                deltaV.timesBy(2.0);
            }
            LOG.info("combine times use time " + (System.currentTimeMillis() - startTs));
            startTs = System.currentTimeMillis();
            tryNum = 100;
            while (tryNum-- > 0) {
                deltaV1.timesBy(2.0);
            }
            LOG.info("single times use time " + (System.currentTimeMillis() - startTs));
            startTs = System.currentTimeMillis();
            tryNum = 100;
            while (tryNum-- > 0) {
                deltaV2.timesBy(2.0);
            }
            LOG.info("dense times use time " + (System.currentTimeMillis() - startTs));
            deltaV.setMatrixId(client.getMatrixId());
            deltaV.setRowId(0);
            LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " train use time=" + (System.currentTimeMillis() - startTs));
            startTs = System.currentTimeMillis();
            client.increment(deltaV);
            client.clock().get();
            LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " flush use time=" + (System.currentTimeMillis() - startTs));
            taskContext.incEpoch();
        }
    } catch (Throwable x) {
        throw new AngelException("run task failed ", x);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) CompSparseLongKeyDoubleVector(com.tencent.angel.ml.math.vector.CompSparseLongKeyDoubleVector) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) TVector(com.tencent.angel.ml.math.TVector) CompSparseLongKeyDoubleVector(com.tencent.angel.ml.math.vector.CompSparseLongKeyDoubleVector) SparseLongKeyDoubleVector(com.tencent.angel.ml.math.vector.SparseLongKeyDoubleVector)

Aggregations

MatrixClient (com.tencent.angel.psagent.matrix.MatrixClient)198 Worker (com.tencent.angel.worker.Worker)183 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)14 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)14 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)14 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)13 Vector (com.tencent.angel.ml.math2.vector.Vector)13 IncrementRows (com.tencent.angel.ml.matrix.psf.update.update.IncrementRows)13 IncrementRowsParam (com.tencent.angel.ml.matrix.psf.update.update.IncrementRowsParam)13 Test (org.junit.Test)13 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)12 LongDoubleVector (com.tencent.angel.ml.math2.vector.LongDoubleVector)12 LongIntVector (com.tencent.angel.ml.math2.vector.LongIntVector)12 LongLongVector (com.tencent.angel.ml.math2.vector.LongLongVector)12 AngelException (com.tencent.angel.exception.AngelException)10 LongIndexGet (com.tencent.angel.ml.matrix.psf.get.indexed.LongIndexGet)9 LongIndexGetParam (com.tencent.angel.ml.matrix.psf.get.indexed.LongIndexGetParam)9 TVector (com.tencent.angel.ml.math.TVector)6 MasterServiceTest (com.tencent.angel.master.MasterServiceTest)5 DenseDoubleVector (com.tencent.angel.ml.math.vector.DenseDoubleVector)5