use of org.apache.sysml.runtime.instructions.cp.KahanObject in project incubator-systemml by apache.
the class LibMatrixAgg method aggregateBinaryMatrixLastColSparseGeneric.
private static void aggregateBinaryMatrixLastColSparseGeneric(MatrixBlock in, MatrixBlock aggVal) {
// sparse-safe operation
if (in.isEmptyBlock(false))
return;
SparseBlock a = in.getSparseBlock();
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; i < rlen; i++) {
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 && aix[j] < n - 1; j++) {
int jix = aix[j];
double corr = in.quickGetValue(i, n - 1);
buffer1._sum = aggVal.quickGetValue(i, jix);
buffer1._correction = aggVal.quickGetValue(i, n - 1);
akplus.execute(buffer1, avals[j], corr);
aggVal.quickSetValue(i, jix, buffer1._sum);
aggVal.quickSetValue(i, n - 1, buffer1._correction);
}
}
}
// note: nnz of aggVal/aggCorr maintained internally
aggVal.examSparsity();
}
use of org.apache.sysml.runtime.instructions.cp.KahanObject in project incubator-systemml by apache.
the class LibMatrixAgg method cumaggregateUnaryMatrixSparse.
private static void cumaggregateUnaryMatrixSparse(MatrixBlock in, MatrixBlock out, AggType optype, ValueFunction vFn, double[] agg, int rl, int ru) {
final int m = in.rlen;
final int n = in.clen;
SparseBlock a = in.getSparseBlock();
DenseBlock dc = out.getDenseBlock();
double[] c = out.getDenseBlockValues();
switch(optype) {
case CUM_KAHAN_SUM:
{
// CUMSUM
KahanObject kbuff = new KahanObject(0, 0);
KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
s_ucumkp(a, agg, dc, m, n, kbuff, kplus, rl, ru);
break;
}
case CUM_PROD:
{
// CUMPROD
s_ucumm(a, agg, c, n, rl, ru);
break;
}
case CUM_MIN:
case CUM_MAX:
{
double init = (optype == AggType.CUM_MAX) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
s_ucummxx(a, agg, c, n, init, (Builtin) vFn, rl, ru);
break;
}
default:
throw new DMLRuntimeException("Unsupported cumulative aggregation type: " + optype);
}
}
use of org.apache.sysml.runtime.instructions.cp.KahanObject in project incubator-systemml by apache.
the class LibMatrixAgg method aggregateBinaryMatrixAllDense.
private static void aggregateBinaryMatrixAllDense(MatrixBlock in, MatrixBlock aggVal, MatrixBlock aggCorr) {
if (in.denseBlock == null || in.isEmptyBlock(false))
return;
// allocate output arrays (if required)
// should always stay in dense
aggVal.allocateDenseBlock();
// should always stay in dense
aggCorr.allocateDenseBlock();
double[] a = in.getDenseBlockValues();
double[] c = aggVal.getDenseBlockValues();
double[] cc = aggCorr.getDenseBlockValues();
KahanObject buffer1 = new KahanObject(0, 0);
KahanPlus akplus = KahanPlus.getKahanPlusFnObject();
final int len = Math.min(a.length, in.rlen * in.clen);
int nnzC = 0;
int nnzCC = 0;
for (int i = 0; i < len; i++) {
buffer1._sum = c[i];
buffer1._correction = cc[i];
akplus.execute2(buffer1, a[i]);
c[i] = buffer1._sum;
cc[i] = buffer1._correction;
nnzC += (buffer1._sum != 0) ? 1 : 0;
nnzCC += (buffer1._correction != 0) ? 1 : 0;
}
aggVal.nonZeros = nnzC;
aggCorr.nonZeros = nnzCC;
}
use of org.apache.sysml.runtime.instructions.cp.KahanObject in project incubator-systemml by apache.
the class LibMatrixAgg method aggregateBinaryMatrixSparseGeneric.
private static void aggregateBinaryMatrixSparseGeneric(MatrixBlock in, MatrixBlock aggVal, MatrixBlock aggCorr) {
if (in.isEmptyBlock(false))
return;
SparseBlock a = in.getSparseBlock();
KahanObject buffer1 = new KahanObject(0, 0);
KahanPlus akplus = KahanPlus.getKahanPlusFnObject();
final int m = in.rlen;
final int rlen = Math.min(a.numRows(), m);
for (int i = 0; i < rlen; i++) {
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 jix = aix[j];
buffer1._sum = aggVal.quickGetValue(i, jix);
buffer1._correction = aggCorr.quickGetValue(i, jix);
akplus.execute2(buffer1, avals[j]);
aggVal.quickSetValue(i, jix, buffer1._sum);
aggCorr.quickSetValue(i, jix, buffer1._correction);
}
}
}
// note: nnz of aggVal/aggCorr maintained internally
if (aggVal.sparse)
aggVal.examSparsity(false);
if (aggCorr.sparse)
aggCorr.examSparsity(false);
}
use of org.apache.sysml.runtime.instructions.cp.KahanObject in project incubator-systemml by apache.
the class MatrixBlock method incrementalAggregateUnaryHelp.
private static void incrementalAggregateUnaryHelp(AggregateOperator aggOp, MatrixBlock result, int row, int column, double newvalue, KahanObject buffer) {
if (aggOp.correctionExists) {
if (aggOp.correctionLocation == CorrectionLocationType.LASTROW || aggOp.correctionLocation == CorrectionLocationType.LASTCOLUMN) {
int corRow = row, corCol = column;
if (// extra row
aggOp.correctionLocation == CorrectionLocationType.LASTROW)
corRow++;
else if (aggOp.correctionLocation == CorrectionLocationType.LASTCOLUMN)
corCol++;
else
throw new DMLRuntimeException("unrecognized correctionLocation: " + aggOp.correctionLocation);
buffer._sum = result.quickGetValue(row, column);
buffer._correction = result.quickGetValue(corRow, corCol);
buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, newvalue);
result.quickSetValue(row, column, buffer._sum);
result.quickSetValue(corRow, corCol, buffer._correction);
} else if (aggOp.correctionLocation == CorrectionLocationType.NONE) {
throw new DMLRuntimeException("unrecognized correctionLocation: " + aggOp.correctionLocation);
} else // for mean
{
int corRow = row, corCol = column;
int countRow = row, countCol = column;
if (aggOp.correctionLocation == CorrectionLocationType.LASTTWOROWS) {
countRow++;
corRow += 2;
} else if (aggOp.correctionLocation == CorrectionLocationType.LASTTWOCOLUMNS) {
countCol++;
corCol += 2;
} else
throw new DMLRuntimeException("unrecognized correctionLocation: " + aggOp.correctionLocation);
buffer._sum = result.quickGetValue(row, column);
buffer._correction = result.quickGetValue(corRow, corCol);
double count = result.quickGetValue(countRow, countCol) + 1.0;
buffer = (KahanObject) aggOp.increOp.fn.execute(buffer, newvalue, count);
result.quickSetValue(row, column, buffer._sum);
result.quickSetValue(corRow, corCol, buffer._correction);
result.quickSetValue(countRow, countCol, count);
}
} else {
newvalue = aggOp.increOp.fn.execute(result.quickGetValue(row, column), newvalue);
result.quickSetValue(row, column, newvalue);
}
}
Aggregations