Search in sources :

Example 46 with ValueFunction

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

the class SpoofCellwise method executeDenseAggMxx.

private double executeDenseAggMxx(DenseBlock a, SideInput[] b, double[] scalars, int m, int n, boolean sparseSafe, int rl, int ru) {
    // safe aggregation for min/max w/ handling of zero entries
    // note: sparse safe with zero value as min/max handled outside
    double ret = (_aggOp == AggOp.MIN) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
    ValueFunction vfun = getAggFunction();
    if (a == null && !sparseSafe) {
        for (int i = rl; i < ru; i++) for (int j = 0; j < n; j++) ret = vfun.execute(ret, genexec(0, b, scalars, m, n, i, j));
    } else if (a != null) {
        for (int i = rl; i < ru; i++) {
            double[] avals = a.values(i);
            int aix = a.pos(i);
            for (int j = 0; j < n; j++) {
                double aval = avals[aix + j];
                if (aval != 0 || !sparseSafe)
                    ret = vfun.execute(ret, genexec(aval, b, scalars, m, n, i, j));
            }
        }
    }
    return ret;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction)

Example 47 with ValueFunction

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

the class SpoofCellwise method executeCompressedRowAggMxx.

private long executeCompressedRowAggMxx(CompressedMatrixBlock a, SideInput[] b, double[] scalars, double[] c, int m, int n, boolean sparseSafe, int rl, int ru) {
    Arrays.fill(c, rl, ru, (_aggOp == AggOp.MIN) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
    ValueFunction vfun = getAggFunction();
    long lnnz = 0;
    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());
        c[cell.getI()] = vfun.execute(c[cell.getI()], val);
    }
    for (int i = rl; i < ru; i++) lnnz += (c[i] != 0) ? 1 : 0;
    return lnnz;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 48 with ValueFunction

use of org.apache.sysml.runtime.functionobjects.ValueFunction 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 49 with ValueFunction

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

the class SpoofCellwise method executeCompressedColAggMxx.

private long executeCompressedColAggMxx(CompressedMatrixBlock a, SideInput[] b, double[] scalars, double[] c, int m, int n, boolean sparseSafe, int rl, int ru) {
    Arrays.fill(c, rl, ru, (_aggOp == AggOp.MIN) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
    ValueFunction vfun = getAggFunction();
    long lnnz = 0;
    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());
        c[cell.getI()] = vfun.execute(c[cell.getI()], val);
    }
    for (int i = rl; i < ru; i++) lnnz += (c[i] != 0) ? 1 : 0;
    return lnnz;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 50 with ValueFunction

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

the class SpoofMultiAggregate method aggregatePartialResults.

private void aggregatePartialResults(double[] c, ArrayList<double[]> pret) {
    ValueFunction[] vfun = getAggFunctions(_aggOps);
    for (int k = 0; k < _aggOps.length; k++) {
        if (vfun[k] instanceof KahanFunction) {
            KahanObject kbuff = new KahanObject(0, 0);
            KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
            for (double[] tmp : pret) kplus.execute2(kbuff, tmp[k]);
            c[k] = kbuff._sum;
        } else {
            for (double[] tmp : pret) c[k] = vfun[k].execute(c[k], tmp[k]);
        }
    }
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus)

Aggregations

ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)52 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)17 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)10 SimpleOperator (org.apache.sysml.runtime.matrix.operators.SimpleOperator)9 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)8 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)7 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)6 IJV (org.apache.sysml.runtime.matrix.data.IJV)6 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 BinaryOperator (org.apache.sysml.runtime.matrix.operators.BinaryOperator)4 Operator (org.apache.sysml.runtime.matrix.operators.Operator)4 UnaryOperator (org.apache.sysml.runtime.matrix.operators.UnaryOperator)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)2 BuiltinCode (org.apache.sysml.runtime.functionobjects.Builtin.BuiltinCode)2