Search in sources :

Example 26 with KahanFunction

use of org.apache.sysml.runtime.functionobjects.KahanFunction in project 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 27 with KahanFunction

use of org.apache.sysml.runtime.functionobjects.KahanFunction in project systemml by apache.

the class SpoofCellwise method executeCompressedAggSum.

private double executeCompressedAggSum(CompressedMatrixBlock a, SideInput[] b, double[] scalars, int m, int n, boolean sparseSafe, int rl, int ru) {
    KahanFunction kplus = (KahanFunction) getAggFunction();
    KahanObject kbuff = new KahanObject(0, 0);
    KahanObject kbuff2 = new KahanObject(0, 0);
    // special case: computation over value-tuples only
    if (sparseSafe && b.length == 0 && !a.hasUncompressedColGroup()) {
        // note: all remaining groups are guaranteed ColGroupValue
        boolean entireGrp = (rl == 0 && ru == a.getNumRows());
        int maxNumVals = a.getColGroups().stream().mapToInt(g -> ((ColGroupValue) g).getNumValues()).max().orElse(0);
        int[] counts = new int[maxNumVals];
        for (ColGroup grp : a.getColGroups()) {
            ColGroupValue grpv = (ColGroupValue) grp;
            counts = entireGrp ? grpv.getCounts(counts) : grpv.getCounts(rl, ru, counts);
            for (int k = 0; k < grpv.getNumValues(); k++) {
                kbuff2.set(0, 0);
                double in = grpv.sumValues(k, kplus, kbuff2);
                double out = genexec(in, b, scalars, m, n, -1, -1);
                kplus.execute3(kbuff, out, counts[k]);
            }
        }
    } else // general case of arbitrary side inputs
    {
        Iterator<IJV> iter = a.getIterator(rl, ru, !sparseSafe);
        while (iter.hasNext()) {
            IJV cell = iter.next();
            double val = genexec(cell.getV(), b, scalars, m, n, cell.getI(), cell.getJ());
            kplus.execute2(kbuff, val);
        }
    }
    return kbuff._sum;
}
Also used : ColGroup(org.apache.sysml.runtime.compress.ColGroup) IJV(org.apache.sysml.runtime.matrix.data.IJV) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) ColGroupValue(org.apache.sysml.runtime.compress.ColGroupValue)

Example 28 with KahanFunction

use of org.apache.sysml.runtime.functionobjects.KahanFunction in project systemml by apache.

the class SpoofCellwise method executeCompressedColAggSum.

private long executeCompressedColAggSum(CompressedMatrixBlock a, SideInput[] b, double[] scalars, double[] c, int m, int n, boolean sparseSafe, int rl, int ru) {
    KahanFunction kplus = (KahanFunction) getAggFunction();
    KahanObject kbuff = new KahanObject(0, 0);
    double[] corr = new double[n];
    Iterator<IJV> iter = a.getIterator(rl, ru, !sparseSafe);
    while (iter.hasNext()) {
        IJV cell = iter.next();
        double val = genexec(cell.getV(), b, scalars, m, n, cell.getI(), cell.getJ());
        kbuff.set(c[cell.getJ()], corr[cell.getJ()]);
        kplus.execute2(kbuff, val);
        c[cell.getJ()] = kbuff._sum;
        corr[cell.getJ()] = kbuff._correction;
    }
    return -1;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject)

Example 29 with KahanFunction

use of org.apache.sysml.runtime.functionobjects.KahanFunction in project systemml by apache.

the class SpoofCellwise method execute.

@Override
public ScalarObject execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, int k) {
    // sanity check
    if (inputs == null || inputs.size() < 1)
        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;
    }
    double ret = 0;
    if (// SINGLE-THREADED
    k <= 1) {
        if (inputs.get(0) instanceof CompressedMatrixBlock)
            ret = executeCompressedAndAgg((CompressedMatrixBlock) a, b, scalars, m, n, sparseSafe, 0, m);
        else if (!inputs.get(0).isInSparseFormat())
            ret = executeDenseAndAgg(a.getDenseBlock(), b, scalars, m, n, sparseSafe, 0, m);
        else
            ret = executeSparseAndAgg(a.getSparseBlock(), b, scalars, m, n, sparseSafe, 0, m);
    } else // MULTI-THREADED
    {
        try {
            ExecutorService pool = CommonThreadPool.get(k);
            ArrayList<ParAggTask> tasks = new ArrayList<>();
            int nk = (a instanceof CompressedMatrixBlock) ? k : 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 ParAggTask(a, b, scalars, m, n, sparseSafe, i * blklen, Math.min((i + 1) * blklen, m)));
            // execute tasks
            List<Future<Double>> taskret = pool.invokeAll(tasks);
            pool.shutdown();
            // aggregate partial results
            ValueFunction vfun = getAggFunction();
            if (vfun instanceof KahanFunction) {
                KahanObject kbuff = new KahanObject(0, 0);
                KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
                for (Future<Double> task : taskret) kplus.execute2(kbuff, task.get());
                ret = kbuff._sum;
            } else {
                for (Future<Double> task : taskret) ret = vfun.execute(ret, task.get());
            }
        } catch (Exception ex) {
            throw new DMLRuntimeException(ex);
        }
    }
    // correction for min/max
    if ((_aggOp == AggOp.MIN || _aggOp == AggOp.MAX) && sparseSafe && a.getNonZeros() < a.getNumRows() * a.getNumColumns())
        // unseen 0 might be max or min value
        ret = getAggFunction().execute(ret, 0);
    return new DoubleObject(ret);
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) ArrayList(java.util.ArrayList) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ExecutorService(java.util.concurrent.ExecutorService) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) Future(java.util.concurrent.Future) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus)

Example 30 with KahanFunction

use of org.apache.sysml.runtime.functionobjects.KahanFunction in project systemml by apache.

the class SpoofCellwise method executeSparseRowAggSum.

private long executeSparseRowAggSum(SparseBlock sblock, SideInput[] b, double[] scalars, MatrixBlock out, int m, int n, boolean sparseSafe, int rl, int ru) {
    KahanFunction kplus = (KahanFunction) getAggFunction();
    KahanObject kbuff = new KahanObject(0, 0);
    // note: sequential scan algorithm for both sparse-safe and -unsafe
    // in order to avoid binary search for sparse-unsafe
    double[] c = out.getDenseBlockValues();
    long lnnz = 0;
    for (int i = rl; i < ru; i++) {
        kbuff.set(0, 0);
        int lastj = -1;
        // handle non-empty rows
        if (sblock != null && !sblock.isEmpty(i)) {
            int apos = sblock.pos(i);
            int alen = sblock.size(i);
            int[] aix = sblock.indexes(i);
            double[] avals = sblock.values(i);
            for (int k = apos; k < apos + alen; k++) {
                // process zeros before current non-zero
                if (!sparseSafe)
                    for (int j = lastj + 1; j < aix[k]; j++) kplus.execute2(kbuff, genexec(0, b, scalars, m, n, i, j));
                // process current non-zero
                lastj = aix[k];
                kplus.execute2(kbuff, genexec(avals[k], b, scalars, m, n, i, lastj));
            }
        }
        // process empty rows or remaining zeros
        if (!sparseSafe)
            for (int j = lastj + 1; j < n; j++) kplus.execute2(kbuff, genexec(0, b, scalars, m, n, i, j));
        lnnz += ((c[i] = kbuff._sum) != 0) ? 1 : 0;
    }
    return lnnz;
}
Also used : KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject)

Aggregations

KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)32 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)28 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)10 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)10 ArrayList (java.util.ArrayList)6 ExecutorService (java.util.concurrent.ExecutorService)6 Future (java.util.concurrent.Future)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 IJV (org.apache.sysml.runtime.matrix.data.IJV)6 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)6 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)4 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)4 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)4 ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)4 ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)4 IOException (java.io.IOException)2 ColGroup (org.apache.sysml.runtime.compress.ColGroup)2 ColGroupValue (org.apache.sysml.runtime.compress.ColGroupValue)2 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)2 BuiltinCode (org.apache.sysml.runtime.functionobjects.Builtin.BuiltinCode)2