use of org.apache.sysml.runtime.instructions.cp.KahanObject 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) throws DMLRuntimeException {
final int m = in.rlen;
final int n = in.clen;
double[] a = in.getDenseBlock();
double[] c = out.getDenseBlock();
switch(optype) {
case //SUM/TRACE via k+,
KAHAN_SUM:
{
KahanObject kbuff = new KahanObject(0, 0);
if (// SUM
ixFn instanceof ReduceAll)
d_uakp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//ROWSUM
ixFn instanceof ReduceCol)
d_uarkp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//COLSUM
ixFn instanceof ReduceRow)
d_uackp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//TRACE
ixFn instanceof ReduceDiag)
d_uakptrace(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
break;
}
case //SUM_SQ via k+,
KAHAN_SUM_SQ:
{
KahanObject kbuff = new KahanObject(0, 0);
if (//SUM_SQ
ixFn instanceof ReduceAll)
d_uasqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (//ROWSUM_SQ
ixFn instanceof ReduceCol)
d_uarsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (//COLSUM_SQ
ixFn instanceof ReduceRow)
d_uacsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
break;
}
case //CUMSUM
CUM_KAHAN_SUM:
{
KahanObject kbuff = new KahanObject(0, 0);
KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
d_ucumkp(a, null, c, m, n, kbuff, kplus, rl, ru);
break;
}
case //CUMPROD
CUM_PROD:
{
d_ucumm(a, null, c, m, n, rl, ru);
break;
}
case CUM_MIN:
case CUM_MAX:
{
double init = Double.MAX_VALUE * ((optype == AggType.CUM_MAX) ? -1 : 1);
d_ucummxx(a, null, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN:
case //MAX/MIN
MAX:
{
double init = Double.MAX_VALUE * ((optype == AggType.MAX) ? -1 : 1);
if (// MIN/MAX
ixFn instanceof ReduceAll)
d_uamxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
else if (//ROWMIN/ROWMAX
ixFn instanceof ReduceCol)
d_uarmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
else if (//COLMIN/COLMAX
ixFn instanceof ReduceRow)
d_uacmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MAX_INDEX:
{
double init = -Double.MAX_VALUE;
if (//ROWINDEXMAX
ixFn instanceof ReduceCol)
d_uarimxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN_INDEX:
{
double init = Double.MAX_VALUE;
if (//ROWINDEXMIN
ixFn instanceof ReduceCol)
d_uarimin(a, c, m, 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, m, n, kbuff, (Mean) vFn, rl, ru);
else if (//ROWMEAN
ixFn instanceof ReduceCol)
d_uarmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
else if (//COLMEAN
ixFn instanceof ReduceRow)
d_uacmean(a, c, m, 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, m, n, cbuff, (CM) vFn, rl, ru);
else if (//ROWVAR
ixFn instanceof ReduceCol)
d_uarvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
else if (//COLVAR
ixFn instanceof ReduceRow)
d_uacvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
break;
}
case //PROD
PROD:
{
if (// PROD
ixFn instanceof ReduceAll)
d_uam(a, c, m, n, rl, ru);
break;
}
default:
throw new DMLRuntimeException("Unsupported aggregation type: " + optype);
}
}
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) throws DMLRuntimeException {
final int m = in.rlen;
final int n = in.clen;
SparseBlock a = in.getSparseBlock();
double[] c = out.getDenseBlock();
switch(optype) {
case //CUMSUM
CUM_KAHAN_SUM:
{
KahanObject kbuff = new KahanObject(0, 0);
KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
s_ucumkp(a, agg, c, m, n, kbuff, kplus, rl, ru);
break;
}
case //CUMPROD
CUM_PROD:
{
s_ucumm(a, agg, c, m, n, rl, ru);
break;
}
case CUM_MIN:
case CUM_MAX:
{
double init = Double.MAX_VALUE * ((optype == AggType.CUM_MAX) ? -1 : 1);
s_ucummxx(a, agg, c, m, 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 aggregateBinaryMatrixLastColSparseGeneric.
private static void aggregateBinaryMatrixLastColSparseGeneric(MatrixBlock in, MatrixBlock aggVal) throws DMLRuntimeException {
//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 aggregateUnaryMatrixSparse.
private static void aggregateUnaryMatrixSparse(MatrixBlock in, MatrixBlock out, AggType optype, ValueFunction vFn, IndexFunction ixFn, int rl, int ru) throws DMLRuntimeException {
final int m = in.rlen;
final int n = in.clen;
SparseBlock a = in.getSparseBlock();
double[] c = out.getDenseBlock();
switch(optype) {
case //SUM via k+
KAHAN_SUM:
{
KahanObject kbuff = new KahanObject(0, 0);
if (// SUM
ixFn instanceof ReduceAll)
s_uakp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//ROWSUM
ixFn instanceof ReduceCol)
s_uarkp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//COLSUM
ixFn instanceof ReduceRow)
s_uackp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
else if (//TRACE
ixFn instanceof ReduceDiag)
s_uakptrace(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
break;
}
case //SUM_SQ via k+
KAHAN_SUM_SQ:
{
KahanObject kbuff = new KahanObject(0, 0);
if (//SUM_SQ
ixFn instanceof ReduceAll)
s_uasqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (//ROWSUM_SQ
ixFn instanceof ReduceCol)
s_uarsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
else if (//COLSUM_SQ
ixFn instanceof ReduceRow)
s_uacsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
break;
}
case //CUMSUM
CUM_KAHAN_SUM:
{
KahanObject kbuff = new KahanObject(0, 0);
KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
s_ucumkp(a, null, c, m, n, kbuff, kplus, rl, ru);
break;
}
case //CUMPROD
CUM_PROD:
{
s_ucumm(a, null, c, m, n, rl, ru);
break;
}
case CUM_MIN:
case CUM_MAX:
{
double init = Double.MAX_VALUE * ((optype == AggType.CUM_MAX) ? -1 : 1);
s_ucummxx(a, null, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN:
case //MAX/MIN
MAX:
{
double init = Double.MAX_VALUE * ((optype == AggType.MAX) ? -1 : 1);
if (// MIN/MAX
ixFn instanceof ReduceAll)
s_uamxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
else if (//ROWMIN/ROWMAX
ixFn instanceof ReduceCol)
s_uarmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
else if (//COLMIN/COLMAX
ixFn instanceof ReduceRow)
s_uacmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MAX_INDEX:
{
double init = -Double.MAX_VALUE;
if (//ROWINDEXMAX
ixFn instanceof ReduceCol)
s_uarimxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MIN_INDEX:
{
double init = Double.MAX_VALUE;
if (//ROWINDEXMAX
ixFn instanceof ReduceCol)
s_uarimin(a, c, m, n, init, (Builtin) vFn, rl, ru);
break;
}
case MEAN:
{
KahanObject kbuff = new KahanObject(0, 0);
if (// MEAN
ixFn instanceof ReduceAll)
s_uamean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
else if (//ROWMEAN
ixFn instanceof ReduceCol)
s_uarmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
else if (//COLMEAN
ixFn instanceof ReduceRow)
s_uacmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
break;
}
case //VAR
VAR:
{
CM_COV_Object cbuff = new CM_COV_Object();
if (//VAR
ixFn instanceof ReduceAll)
s_uavar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
else if (//ROWVAR
ixFn instanceof ReduceCol)
s_uarvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
else if (//COLVAR
ixFn instanceof ReduceRow)
s_uacvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
break;
}
case //PROD
PROD:
{
if (// PROD
ixFn instanceof ReduceAll)
s_uam(a, c, m, n, rl, ru);
break;
}
default:
throw new DMLRuntimeException("Unsupported 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) throws DMLRuntimeException {
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.getDenseBlock();
double[] c = aggVal.getDenseBlock();
double[] cc = aggCorr.getDenseBlock();
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;
}
Aggregations