use of org.apache.sysml.runtime.functionobjects.KahanPlus in project incubator-systemml by apache.
the class LibMatrixAgg method aggregateUnaryMatrixDense.
private static void aggregateUnaryMatrixDense(MatrixBlock in, MatrixBlock out, AggType optype, ValueFunction vFn, IndexFunction ixFn, int rl, int ru) {
final int n = in.clen;
// note: due to corrections, even the output might be a large dense block
DenseBlock a = in.getDenseBlock();
DenseBlock c = out.getDenseBlock();
switch(optype) {
case KAHAN_SUM:
{
// SUM/TRACE via k+,
KahanObject kbuff = new KahanObject(0, 0);
if (// SUM
ixFn instanceof ReduceAll)
d_uakp(a, c, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (// ROWSUM
ixFn instanceof ReduceCol)
d_uarkp(a, c, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (// COLSUM
ixFn instanceof ReduceRow)
d_uackp(a, c, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (// TRACE
ixFn instanceof ReduceDiag)
d_uakptrace(a, c, n, kbuff, (KahanPlus) vFn, rl, ru);
break;
}
case KAHAN_SUM_SQ:
{
// SUM_SQ via k+,
KahanObject kbuff = new KahanObject(0, 0);
if (// SUM_SQ
ixFn instanceof ReduceAll)
d_uasqkp(a, c, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (// ROWSUM_SQ
ixFn instanceof ReduceCol)
d_uarsqkp(a, c, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (// COLSUM_SQ
ixFn instanceof ReduceRow)
d_uacsqkp(a, c, n, kbuff, (KahanPlusSq) vFn, rl, ru);
break;
}
case CUM_KAHAN_SUM:
{
// CUMSUM
KahanObject kbuff = new KahanObject(0, 0);
KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
d_ucumkp(in.getDenseBlock(), null, out.getDenseBlock(), n, kbuff, kplus, rl, ru);
break;
}
case CUM_PROD:
{
// CUMPROD
d_ucumm(in.getDenseBlockValues(), null, out.getDenseBlockValues(), n, rl, ru);
break;
}
case CUM_MIN:
case CUM_MAX:
{
double init = (optype == AggType.CUM_MAX) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
d_ucummxx(in.getDenseBlockValues(), null, out.getDenseBlockValues(), n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN:
case MAX:
{
// MAX/MIN
double init = (optype == AggType.MAX) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
if (// MIN/MAX
ixFn instanceof ReduceAll)
d_uamxx(a, c, n, init, (Builtin) vFn, rl, ru);
else if (// ROWMIN/ROWMAX
ixFn instanceof ReduceCol)
d_uarmxx(a, c, n, init, (Builtin) vFn, rl, ru);
else if (// COLMIN/COLMAX
ixFn instanceof ReduceRow)
d_uacmxx(a, c, n, init, (Builtin) vFn, rl, ru);
break;
}
case MAX_INDEX:
{
double init = Double.NEGATIVE_INFINITY;
if (// ROWINDEXMAX
ixFn instanceof ReduceCol)
d_uarimxx(a, c, n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN_INDEX:
{
double init = Double.POSITIVE_INFINITY;
if (// ROWINDEXMIN
ixFn instanceof ReduceCol)
d_uarimin(a, c, n, init, (Builtin) vFn, rl, ru);
break;
}
case MEAN:
{
// MEAN
KahanObject kbuff = new KahanObject(0, 0);
if (// MEAN
ixFn instanceof ReduceAll)
d_uamean(a, c, n, kbuff, (Mean) vFn, rl, ru);
else if (// ROWMEAN
ixFn instanceof ReduceCol)
d_uarmean(a, c, n, kbuff, (Mean) vFn, rl, ru);
else if (// COLMEAN
ixFn instanceof ReduceRow)
d_uacmean(a, c, n, kbuff, (Mean) vFn, rl, ru);
break;
}
case VAR:
{
// VAR
CM_COV_Object cbuff = new CM_COV_Object();
if (// VAR
ixFn instanceof ReduceAll)
d_uavar(a, c, n, cbuff, (CM) vFn, rl, ru);
else if (// ROWVAR
ixFn instanceof ReduceCol)
d_uarvar(a, c, n, cbuff, (CM) vFn, rl, ru);
else if (// COLVAR
ixFn instanceof ReduceRow)
d_uacvar(a, c, n, cbuff, (CM) vFn, rl, ru);
break;
}
case PROD:
{
// PROD
if (// PROD
ixFn instanceof ReduceAll)
d_uam(a, c, n, rl, ru);
break;
}
default:
throw new DMLRuntimeException("Unsupported aggregation type: " + optype);
}
}
use of org.apache.sysml.runtime.functionobjects.KahanPlus 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.functionobjects.KahanPlus 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.functionobjects.KahanPlus 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.functionobjects.KahanPlus 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);
}
Aggregations