Search in sources :

Example 26 with KahanPlus

use of org.apache.sysml.runtime.functionobjects.KahanPlus in project incubator-systemml by apache.

the class LibMatrixAgg method aggregateBinaryMatrixLastColDenseGeneric.

private static void aggregateBinaryMatrixLastColDenseGeneric(MatrixBlock in, MatrixBlock aggVal) {
    if (in.denseBlock == null || in.isEmptyBlock(false))
        return;
    final int m = in.rlen;
    final int n = in.clen;
    double[] a = in.getDenseBlockValues();
    KahanObject buffer = new KahanObject(0, 0);
    KahanPlus akplus = KahanPlus.getKahanPlusFnObject();
    // incl implicit nnz maintenance
    for (int i = 0, ix = 0; i < m; i++, ix += n) for (int j = 0; j < n - 1; j++) {
        buffer._sum = aggVal.quickGetValue(i, j);
        buffer._correction = aggVal.quickGetValue(i, n - 1);
        akplus.execute(buffer, a[ix + j], a[ix + j + 1]);
        aggVal.quickSetValue(i, j, buffer._sum);
        aggVal.quickSetValue(i, n - 1, buffer._correction);
    }
    // note: nnz of aggVal maintained internally
    aggVal.examSparsity();
}
Also used : KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus)

Example 27 with KahanPlus

use of org.apache.sysml.runtime.functionobjects.KahanPlus in project incubator-systemml by apache.

the class LibMatrixAgg method aggregateBinaryMatrixSparseDense.

private static void aggregateBinaryMatrixSparseDense(MatrixBlock in, MatrixBlock aggVal, MatrixBlock aggCorr) {
    if (in.isEmptyBlock(false))
        return;
    // allocate output arrays (if required)
    // should always stay in dense
    aggVal.allocateDenseBlock();
    // should always stay in dense
    aggCorr.allocateDenseBlock();
    SparseBlock a = in.getSparseBlock();
    double[] c = aggVal.getDenseBlockValues();
    double[] cc = aggCorr.getDenseBlockValues();
    KahanObject buffer1 = new KahanObject(0, 0);
    KahanPlus akplus = KahanPlus.getKahanPlusFnObject();
    final int m = in.rlen;
    final int n = in.clen;
    final int rlen = Math.min(a.numRows(), m);
    for (int i = 0, cix = 0; i < rlen; i++, cix += n) {
        if (!a.isEmpty(i)) {
            int apos = a.pos(i);
            int alen = a.size(i);
            int[] aix = a.indexes(i);
            double[] avals = a.values(i);
            for (int j = apos; j < apos + alen; j++) {
                int ix = cix + aix[j];
                buffer1._sum = c[ix];
                buffer1._correction = cc[ix];
                akplus.execute2(buffer1, avals[j]);
                c[ix] = buffer1._sum;
                cc[ix] = buffer1._correction;
            }
        }
    }
    aggVal.recomputeNonZeros();
    aggCorr.recomputeNonZeros();
}
Also used : KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus)

Example 28 with KahanPlus

use of org.apache.sysml.runtime.functionobjects.KahanPlus in project incubator-systemml by apache.

the class MatrixBlock method incrementalAggregate.

@Override
public void incrementalAggregate(AggregateOperator aggOp, MatrixValue newWithCorrection) {
    // assert(aggOp.correctionExists);
    MatrixBlock newWithCor = checkType(newWithCorrection);
    KahanObject buffer = new KahanObject(0, 0);
    if (aggOp.correctionLocation == CorrectionLocationType.LASTROW) {
        if (aggOp.increOp.fn instanceof KahanPlus) {
            LibMatrixAgg.aggregateBinaryMatrix(newWithCor, this, aggOp);
        } else {
            for (int r = 0; r < rlen - 1; r++) for (int c = 0; c < clen; c++) {
                buffer._sum = this.quickGetValue(r, c);
                buffer._correction = this.quickGetValue(r + 1, c);
                buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, newWithCor.quickGetValue(r, c), newWithCor.quickGetValue(r + 1, c));
                quickSetValue(r, c, buffer._sum);
                quickSetValue(r + 1, c, buffer._correction);
            }
        }
    } else if (aggOp.correctionLocation == CorrectionLocationType.LASTCOLUMN) {
        if (aggOp.increOp.fn instanceof Builtin && (((Builtin) (aggOp.increOp.fn)).bFunc == Builtin.BuiltinCode.MAXINDEX || ((Builtin) (aggOp.increOp.fn)).bFunc == Builtin.BuiltinCode.MININDEX)) {
            // modified, the other needs to be changed to match.
            for (int r = 0; r < rlen; r++) {
                double currMaxValue = quickGetValue(r, 1);
                long newMaxIndex = (long) newWithCor.quickGetValue(r, 0);
                double newMaxValue = newWithCor.quickGetValue(r, 1);
                double update = aggOp.increOp.fn.execute(newMaxValue, currMaxValue);
                if (2.0 == update) {
                    // Return value of 2 ==> both values the same, break ties
                    // in favor of higher index.
                    long curMaxIndex = (long) quickGetValue(r, 0);
                    quickSetValue(r, 0, Math.max(curMaxIndex, newMaxIndex));
                } else if (1.0 == update) {
                    // Return value of 1 ==> new value is better; use its index
                    quickSetValue(r, 0, newMaxIndex);
                    quickSetValue(r, 1, newMaxValue);
                } else {
                // Other return value ==> current answer is best
                }
            }
        // *** END HACK ***
        } else {
            if (aggOp.increOp.fn instanceof KahanPlus) {
                LibMatrixAgg.aggregateBinaryMatrix(newWithCor, this, aggOp);
            } else {
                for (int r = 0; r < rlen; r++) for (int c = 0; c < clen - 1; c++) {
                    buffer._sum = this.quickGetValue(r, c);
                    buffer._correction = this.quickGetValue(r, c + 1);
                    buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, newWithCor.quickGetValue(r, c), newWithCor.quickGetValue(r, c + 1));
                    quickSetValue(r, c, buffer._sum);
                    quickSetValue(r, c + 1, buffer._correction);
                }
            }
        }
    } else if (aggOp.correctionLocation == CorrectionLocationType.LASTTWOROWS) {
        double n, n2, mu2;
        for (int r = 0; r < rlen - 2; r++) for (int c = 0; c < clen; c++) {
            buffer._sum = this.quickGetValue(r, c);
            n = this.quickGetValue(r + 1, c);
            buffer._correction = this.quickGetValue(r + 2, c);
            mu2 = newWithCor.quickGetValue(r, c);
            n2 = newWithCor.quickGetValue(r + 1, c);
            n = n + n2;
            double toadd = (mu2 - buffer._sum) * n2 / n;
            buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, toadd);
            quickSetValue(r, c, buffer._sum);
            quickSetValue(r + 1, c, n);
            quickSetValue(r + 2, c, buffer._correction);
        }
    } else if (aggOp.correctionLocation == CorrectionLocationType.LASTTWOCOLUMNS) {
        double n, n2, mu2;
        for (int r = 0; r < rlen; r++) for (int c = 0; c < clen - 2; c++) {
            buffer._sum = this.quickGetValue(r, c);
            n = this.quickGetValue(r, c + 1);
            buffer._correction = this.quickGetValue(r, c + 2);
            mu2 = newWithCor.quickGetValue(r, c);
            n2 = newWithCor.quickGetValue(r, c + 1);
            n = n + n2;
            double toadd = (mu2 - buffer._sum) * n2 / n;
            buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, toadd);
            quickSetValue(r, c, buffer._sum);
            quickSetValue(r, c + 1, n);
            quickSetValue(r, c + 2, buffer._correction);
        }
    } else if (aggOp.correctionLocation == CorrectionLocationType.LASTFOURROWS && aggOp.increOp.fn instanceof CM && ((CM) aggOp.increOp.fn).getAggOpType() == AggregateOperationTypes.VARIANCE) {
        // create buffers to store results
        CM_COV_Object cbuff_curr = new CM_COV_Object();
        CM_COV_Object cbuff_part = new CM_COV_Object();
        // perform incremental aggregation
        for (int r = 0; r < rlen - 4; r++) for (int c = 0; c < clen; c++) {
            // extract current values: { var | mean, count, m2 correction, mean correction }
            // note: m2 = var * (n - 1)
            // count
            cbuff_curr.w = quickGetValue(r + 2, c);
            // m2
            cbuff_curr.m2._sum = quickGetValue(r, c) * (cbuff_curr.w - 1);
            // mean
            cbuff_curr.mean._sum = quickGetValue(r + 1, c);
            cbuff_curr.m2._correction = quickGetValue(r + 3, c);
            cbuff_curr.mean._correction = quickGetValue(r + 4, c);
            // extract partial values: { var | mean, count, m2 correction, mean correction }
            // note: m2 = var * (n - 1)
            // count
            cbuff_part.w = newWithCor.quickGetValue(r + 2, c);
            // m2
            cbuff_part.m2._sum = newWithCor.quickGetValue(r, c) * (cbuff_part.w - 1);
            // mean
            cbuff_part.mean._sum = newWithCor.quickGetValue(r + 1, c);
            cbuff_part.m2._correction = newWithCor.quickGetValue(r + 3, c);
            cbuff_part.mean._correction = newWithCor.quickGetValue(r + 4, c);
            // calculate incremental aggregated variance
            cbuff_curr = (CM_COV_Object) aggOp.increOp.fn.execute(cbuff_curr, cbuff_part);
            // store updated values: { var | mean, count, m2 correction, mean correction }
            double var = cbuff_curr.getRequiredResult(AggregateOperationTypes.VARIANCE);
            quickSetValue(r, c, var);
            // mean
            quickSetValue(r + 1, c, cbuff_curr.mean._sum);
            // count
            quickSetValue(r + 2, c, cbuff_curr.w);
            quickSetValue(r + 3, c, cbuff_curr.m2._correction);
            quickSetValue(r + 4, c, cbuff_curr.mean._correction);
        }
    } else if (aggOp.correctionLocation == CorrectionLocationType.LASTFOURCOLUMNS && aggOp.increOp.fn instanceof CM && ((CM) aggOp.increOp.fn).getAggOpType() == AggregateOperationTypes.VARIANCE) {
        // create buffers to store results
        CM_COV_Object cbuff_curr = new CM_COV_Object();
        CM_COV_Object cbuff_part = new CM_COV_Object();
        // perform incremental aggregation
        for (int r = 0; r < rlen; r++) for (int c = 0; c < clen - 4; c++) {
            // extract current values: { var | mean, count, m2 correction, mean correction }
            // note: m2 = var * (n - 1)
            // count
            cbuff_curr.w = quickGetValue(r, c + 2);
            // m2
            cbuff_curr.m2._sum = quickGetValue(r, c) * (cbuff_curr.w - 1);
            // mean
            cbuff_curr.mean._sum = quickGetValue(r, c + 1);
            cbuff_curr.m2._correction = quickGetValue(r, c + 3);
            cbuff_curr.mean._correction = quickGetValue(r, c + 4);
            // extract partial values: { var | mean, count, m2 correction, mean correction }
            // note: m2 = var * (n - 1)
            // count
            cbuff_part.w = newWithCor.quickGetValue(r, c + 2);
            // m2
            cbuff_part.m2._sum = newWithCor.quickGetValue(r, c) * (cbuff_part.w - 1);
            // mean
            cbuff_part.mean._sum = newWithCor.quickGetValue(r, c + 1);
            cbuff_part.m2._correction = newWithCor.quickGetValue(r, c + 3);
            cbuff_part.mean._correction = newWithCor.quickGetValue(r, c + 4);
            // calculate incremental aggregated variance
            cbuff_curr = (CM_COV_Object) aggOp.increOp.fn.execute(cbuff_curr, cbuff_part);
            // store updated values: { var | mean, count, m2 correction, mean correction }
            double var = cbuff_curr.getRequiredResult(AggregateOperationTypes.VARIANCE);
            quickSetValue(r, c, var);
            // mean
            quickSetValue(r, c + 1, cbuff_curr.mean._sum);
            // count
            quickSetValue(r, c + 2, cbuff_curr.w);
            quickSetValue(r, c + 3, cbuff_curr.m2._correction);
            quickSetValue(r, c + 4, cbuff_curr.mean._correction);
        }
    } else
        throw new DMLRuntimeException("unrecognized correctionLocation: " + aggOp.correctionLocation);
}
Also used : CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) CM(org.apache.sysml.runtime.functionobjects.CM) Builtin(org.apache.sysml.runtime.functionobjects.Builtin) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 29 with KahanPlus

use of org.apache.sysml.runtime.functionobjects.KahanPlus in project incubator-systemml by apache.

the class MatrixBlock method aggregateTernaryOperations.

public MatrixBlock aggregateTernaryOperations(MatrixBlock m1, MatrixBlock m2, MatrixBlock m3, MatrixBlock ret, AggregateTernaryOperator op, boolean inCP) {
    // check input dimensions and operators
    if (m1.rlen != m2.rlen || m1.clen != m2.clen || (m3 != null && (m2.rlen != m3.rlen || m2.clen != m3.clen)))
        throw new DMLRuntimeException("Invalid dimensions for aggregate ternary (" + m1.rlen + "x" + m1.clen + ", " + m2.rlen + "x" + m2.clen + ", " + m3.rlen + "x" + m3.clen + ").");
    if (!(op.aggOp.increOp.fn instanceof KahanPlus && op.binaryFn instanceof Multiply))
        throw new DMLRuntimeException("Unsupported operator for aggregate ternary operations.");
    // create output matrix block w/ corrections
    int rl = (op.indexFn instanceof ReduceRow) ? 2 : 1;
    int cl = (op.indexFn instanceof ReduceRow) ? m1.clen : 2;
    if (ret == null)
        ret = new MatrixBlock(rl, cl, false);
    else
        ret.reset(rl, cl, false);
    // execute ternary aggregate function
    if (op.getNumThreads() > 1)
        ret = LibMatrixAgg.aggregateTernary(m1, m2, m3, ret, op, op.getNumThreads());
    else
        ret = LibMatrixAgg.aggregateTernary(m1, m2, m3, ret, op);
    if (op.aggOp.correctionExists && inCP)
        ret.dropLastRowsOrColumns(op.aggOp.correctionLocation);
    return ret;
}
Also used : Multiply(org.apache.sysml.runtime.functionobjects.Multiply) MinusMultiply(org.apache.sysml.runtime.functionobjects.MinusMultiply) PlusMultiply(org.apache.sysml.runtime.functionobjects.PlusMultiply) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) ReduceRow(org.apache.sysml.runtime.functionobjects.ReduceRow) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)29 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)25 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)11 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)8 ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)7 CM (org.apache.sysml.runtime.functionobjects.CM)5 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)5 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)5 ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)5 ReduceRow (org.apache.sysml.runtime.functionobjects.ReduceRow)5 DenseBlock (org.apache.sysml.runtime.matrix.data.DenseBlock)5 Mean (org.apache.sysml.runtime.functionobjects.Mean)4 ReduceDiag (org.apache.sysml.runtime.functionobjects.ReduceDiag)4 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)4 CM_COV_Object (org.apache.sysml.runtime.instructions.cp.CM_COV_Object)4 Multiply (org.apache.sysml.runtime.functionobjects.Multiply)3 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2