Search in sources :

Example 51 with CompressedMatrixBlock

use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project incubator-systemml by apache.

the class SpoofCellwise method execute.

@Override
public MatrixBlock execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, MatrixBlock out, int k) {
    // sanity check
    if (inputs == null || inputs.size() < 1 || out == null)
        throw new RuntimeException("Invalid input arguments.");
    // input preparation
    MatrixBlock a = inputs.get(0);
    SideInput[] b = prepInputMatrices(inputs);
    double[] scalars = prepInputScalars(scalarObjects);
    final int m = a.getNumRows();
    final int n = a.getNumColumns();
    // sparse safe check
    boolean sparseSafe = isSparseSafe() || (b.length == 0 && genexec(0, b, scalars, m, n, 0, 0) == 0);
    long inputSize = sparseSafe ? getTotalInputNnz(inputs) : getTotalInputSize(inputs);
    if (inputSize < PAR_NUMCELL_THRESHOLD) {
        // serial execution
        k = 1;
    }
    // result allocation and preparations
    boolean sparseOut = _type == CellType.NO_AGG && sparseSafe && a.isInSparseFormat();
    switch(_type) {
        case NO_AGG:
            out.reset(m, n, sparseOut);
            break;
        case ROW_AGG:
            out.reset(m, 1, false);
            break;
        case COL_AGG:
            out.reset(1, n, false);
            break;
        default:
            throw new DMLRuntimeException("Invalid cell type: " + _type);
    }
    out.allocateBlock();
    long lnnz = 0;
    if (// SINGLE-THREADED
    k <= 1) {
        if (inputs.get(0) instanceof CompressedMatrixBlock)
            lnnz = executeCompressed((CompressedMatrixBlock) a, b, scalars, out, m, n, sparseSafe, 0, m);
        else if (!inputs.get(0).isInSparseFormat())
            lnnz = executeDense(a.getDenseBlock(), b, scalars, out, m, n, sparseSafe, 0, m);
        else
            lnnz = executeSparse(a.getSparseBlock(), b, scalars, out, m, n, sparseSafe, 0, m);
    } else // MULTI-THREADED
    {
        try {
            ExecutorService pool = CommonThreadPool.get(k);
            ArrayList<ParExecTask> tasks = new ArrayList<>();
            int nk = UtilFunctions.roundToNext(Math.min(8 * k, m / 32), k);
            int blklen = (int) (Math.ceil((double) m / nk));
            if (a instanceof CompressedMatrixBlock)
                blklen = BitmapEncoder.getAlignedBlocksize(blklen);
            for (int i = 0; i < nk & i * blklen < m; i++) tasks.add(new ParExecTask(a, b, scalars, out, m, n, sparseSafe, i * blklen, Math.min((i + 1) * blklen, m)));
            // execute tasks
            List<Future<Long>> taskret = pool.invokeAll(tasks);
            pool.shutdown();
            // aggregate nnz and error handling
            for (Future<Long> task : taskret) lnnz += task.get();
            if (_type == CellType.COL_AGG) {
                // aggregate partial results
                double[] c = out.getDenseBlockValues();
                ValueFunction vfun = getAggFunction();
                if (vfun instanceof KahanFunction) {
                    for (ParExecTask task : tasks) LibMatrixMult.vectAdd(task.getResult().getDenseBlockValues(), c, 0, 0, n);
                } else {
                    for (ParExecTask task : tasks) {
                        double[] tmp = task.getResult().getDenseBlockValues();
                        for (int j = 0; j < n; j++) c[j] = vfun.execute(c[j], tmp[j]);
                    }
                }
                lnnz = out.recomputeNonZeros();
            }
        } catch (Exception ex) {
            throw new DMLRuntimeException(ex);
        }
    }
    // post-processing
    out.setNonZeros(lnnz);
    out.examSparsity();
    return out;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 52 with CompressedMatrixBlock

use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project incubator-systemml by apache.

the class SpoofOuterProduct method execute.

@Override
public MatrixBlock execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, MatrixBlock out) {
    // sanity check
    if (inputs == null || inputs.size() < 3 || out == null)
        throw new RuntimeException("Invalid input arguments.");
    // check empty result
    if (// U is empty
    (_outerProductType == OutProdType.LEFT_OUTER_PRODUCT && inputs.get(1).isEmptyBlock(false)) || // V is empty
    (_outerProductType == OutProdType.RIGHT_OUTER_PRODUCT && inputs.get(2).isEmptyBlock(false)) || inputs.get(0).isEmptyBlock(false)) {
        // X is empty
        // turn empty dense into sparse
        out.examSparsity();
        return out;
    }
    // input preparation and result allocation (Allocate the output that is set by Sigma2CPInstruction)
    if (_outerProductType == OutProdType.CELLWISE_OUTER_PRODUCT) {
        // assign it to the time and sparse representation of the major input matrix
        out.reset(inputs.get(0).getNumRows(), inputs.get(0).getNumColumns(), inputs.get(0).isInSparseFormat());
    } else {
        // if left outerproduct gives a value of k*n instead of n*k, change it back to n*k and then transpose the output
        if (_outerProductType == OutProdType.LEFT_OUTER_PRODUCT)
            // n*k
            out.reset(inputs.get(0).getNumColumns(), inputs.get(1).getNumColumns(), false);
        else if (_outerProductType == OutProdType.RIGHT_OUTER_PRODUCT)
            // m*k
            out.reset(inputs.get(0).getNumRows(), inputs.get(1).getNumColumns(), false);
    }
    // check for empty inputs; otherwise allocate result
    if (inputs.get(0).isEmptyBlock(false))
        return out;
    out.allocateBlock();
    // input preparation
    DenseBlock[] ab = getDenseMatrices(prepInputMatrices(inputs, 1, 2, true, false));
    SideInput[] b = prepInputMatrices(inputs, 3, false);
    double[] scalars = prepInputScalars(scalarObjects);
    // core sequential execute
    final int m = inputs.get(0).getNumRows();
    final int n = inputs.get(0).getNumColumns();
    // rank
    final int k = inputs.get(1).getNumColumns();
    MatrixBlock a = inputs.get(0);
    switch(_outerProductType) {
        case LEFT_OUTER_PRODUCT:
        case RIGHT_OUTER_PRODUCT:
            if (a instanceof CompressedMatrixBlock)
                executeCompressed((CompressedMatrixBlock) a, ab[0], ab[1], b, scalars, out.getDenseBlock(), m, n, k, _outerProductType, 0, m, 0, ((CompressedMatrixBlock) a).getNumColGroups());
            else if (!a.isInSparseFormat())
                executeDense(a.getDenseBlock(), ab[0], ab[1], b, scalars, out.getDenseBlock(), m, n, k, _outerProductType, 0, m, 0, n);
            else
                executeSparse(a.getSparseBlock(), ab[0], ab[1], b, scalars, out.getDenseBlock(), m, n, k, a.getNonZeros(), _outerProductType, 0, m, 0, n);
            break;
        case CELLWISE_OUTER_PRODUCT:
            if (a instanceof CompressedMatrixBlock)
                executeCellwiseCompressed((CompressedMatrixBlock) a, ab[0], ab[1], b, scalars, out, m, n, k, _outerProductType, 0, m, 0, n);
            else if (!a.isInSparseFormat())
                executeCellwiseDense(a.getDenseBlock(), ab[0], ab[1], b, scalars, out.getDenseBlock(), m, n, k, _outerProductType, 0, m, 0, n);
            else
                executeCellwiseSparse(a.getSparseBlock(), ab[0], ab[1], b, scalars, out, m, n, k, a.getNonZeros(), _outerProductType, 0, m, 0, n);
            break;
        case AGG_OUTER_PRODUCT:
            throw new DMLRuntimeException("Wrong codepath for aggregate outer product.");
    }
    // post-processing
    if (a instanceof CompressedMatrixBlock && out.isInSparseFormat() && _outerProductType == OutProdType.CELLWISE_OUTER_PRODUCT)
        out.sortSparseRows();
    out.recomputeNonZeros();
    out.examSparsity();
    return out;
}
Also used : DenseBlock(org.apache.sysml.runtime.matrix.data.DenseBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 53 with CompressedMatrixBlock

use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project incubator-systemml by apache.

the class SpoofRowwise method execute.

@Override
public MatrixBlock execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, MatrixBlock out, int k) {
    // redirect to serial execution
    if (k <= 1 || (_type.isColumnAgg() && !LibMatrixMult.checkParColumnAgg(inputs.get(0), k, false)) || getTotalInputSize(inputs) < PAR_NUMCELL_THRESHOLD) {
        return execute(inputs, scalarObjects, out);
    }
    // sanity check
    if (inputs == null || inputs.size() < 1 || out == null)
        throw new RuntimeException("Invalid input arguments.");
    // result allocation and preparations
    final int m = inputs.get(0).getNumRows();
    final int n = inputs.get(0).getNumColumns();
    final int n2 = _type.isConstDim2(_constDim2) ? (int) _constDim2 : _type.isRowTypeB1() || hasMatrixSideInput(inputs) ? getMinColsMatrixSideInputs(inputs) : -1;
    allocateOutputMatrix(m, n, n2, out);
    final boolean flipOut = _type.isRowTypeB1ColumnAgg() && LibSpoofPrimitives.isFlipOuter(out.getNumRows(), out.getNumColumns());
    // input preparation
    MatrixBlock a = inputs.get(0);
    SideInput[] b = prepInputMatrices(inputs, 1, inputs.size() - 1, false, _tB1);
    double[] scalars = prepInputScalars(scalarObjects);
    // core parallel execute
    ExecutorService pool = CommonThreadPool.get(k);
    ArrayList<Integer> blklens = (a instanceof CompressedMatrixBlock) ? LibMatrixMult.getAlignedBlockSizes(m, k, BitmapEncoder.BITMAP_BLOCK_SZ) : LibMatrixMult.getBalancedBlockSizesDefault(m, k, (long) m * n < 16 * PAR_NUMCELL_THRESHOLD);
    try {
        if (_type.isColumnAgg() || _type == RowType.FULL_AGG) {
            // execute tasks
            ArrayList<ParColAggTask> tasks = new ArrayList<>();
            int outLen = out.getNumRows() * out.getNumColumns();
            for (int i = 0, lb = 0; i < blklens.size(); lb += blklens.get(i), i++) tasks.add(new ParColAggTask(a, b, scalars, n, n2, outLen, lb, lb + blklens.get(i)));
            List<Future<DenseBlock>> taskret = pool.invokeAll(tasks);
            // aggregate partial results
            int len = _type.isColumnAgg() ? out.getNumRows() * out.getNumColumns() : 1;
            for (Future<DenseBlock> task : taskret) LibMatrixMult.vectAdd(task.get().valuesAt(0), out.getDenseBlockValues(), 0, 0, len);
            out.recomputeNonZeros();
        } else {
            // execute tasks
            ArrayList<ParExecTask> tasks = new ArrayList<>();
            for (int i = 0, lb = 0; i < blklens.size(); lb += blklens.get(i), i++) tasks.add(new ParExecTask(a, b, out, scalars, n, n2, lb, lb + blklens.get(i)));
            List<Future<Long>> taskret = pool.invokeAll(tasks);
            // aggregate nnz, no need to aggregate results
            long nnz = 0;
            for (Future<Long> task : taskret) nnz += task.get();
            out.setNonZeros(nnz);
        }
        pool.shutdown();
        if (flipOut) {
            fixTransposeDimensions(out);
            out = LibMatrixReorg.transpose(out, new MatrixBlock(out.getNumColumns(), out.getNumRows(), false));
        }
        out.examSparsity();
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    return out;
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) ArrayList(java.util.ArrayList) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) DenseBlock(org.apache.sysml.runtime.matrix.data.DenseBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 54 with CompressedMatrixBlock

use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project incubator-systemml by apache.

the class AggregateBinaryCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    // get inputs
    MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
    MatrixBlock matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
    // compute matrix multiplication
    AggregateBinaryOperator ab_op = (AggregateBinaryOperator) _optr;
    MatrixBlock main = (matBlock2 instanceof CompressedMatrixBlock) ? matBlock2 : matBlock1;
    MatrixBlock ret = main.aggregateBinaryOperations(matBlock1, matBlock2, new MatrixBlock(), ab_op);
    // release inputs/outputs
    ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
    ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
    ec.setMatrixOutput(output.getName(), ret, getExtendedOpcode());
}
Also used : CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) AggregateBinaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator)

Example 55 with CompressedMatrixBlock

use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project incubator-systemml by apache.

the class CompressionCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    // get matrix block input
    MatrixBlock in = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
    // compress the matrix block
    MatrixBlock out = new CompressedMatrixBlock(in).compress(OptimizerUtils.getConstrainedNumThreads(-1));
    // set output and release input
    ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
    ec.setMatrixOutput(output.getName(), out, getExtendedOpcode());
}
Also used : CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock)

Aggregations

CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)77 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)75 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)18 AggregateBinaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator)18 AggregateOperator (org.apache.sysml.runtime.matrix.operators.AggregateOperator)16 ArrayList (java.util.ArrayList)10 ExecutorService (java.util.concurrent.ExecutorService)10 Future (java.util.concurrent.Future)10 DenseBlock (org.apache.sysml.runtime.matrix.data.DenseBlock)10 AggregateUnaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator)6 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)4 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)4 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)4 RightScalarOperator (org.apache.sysml.runtime.matrix.operators.RightScalarOperator)4 ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 Checkpoint (org.apache.sysml.lops.Checkpoint)2