use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project systemml by apache.
the class CompressionCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) {
// get matrix block input
MatrixBlock in = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
// compress the matrix block
MatrixBlock out = new CompressedMatrixBlock(in).compress(OptimizerUtils.getConstrainedNumThreads(-1));
// set output and release input
ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
ec.setMatrixOutput(output.getName(), out, getExtendedOpcode());
}
use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project systemml by apache.
the class SpoofCellwise method execute.
@Override
public MatrixBlock execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, MatrixBlock out, int k) {
// sanity check
if (inputs == null || inputs.size() < 1 || out == null)
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;
}
// result allocation and preparations
boolean sparseOut = _type == CellType.NO_AGG && sparseSafe && a.isInSparseFormat();
switch(_type) {
case NO_AGG:
out.reset(m, n, sparseOut);
break;
case ROW_AGG:
out.reset(m, 1, false);
break;
case COL_AGG:
out.reset(1, n, false);
break;
default:
throw new DMLRuntimeException("Invalid cell type: " + _type);
}
out.allocateBlock();
long lnnz = 0;
if (// SINGLE-THREADED
k <= 1) {
if (inputs.get(0) instanceof CompressedMatrixBlock)
lnnz = executeCompressed((CompressedMatrixBlock) a, b, scalars, out, m, n, sparseSafe, 0, m);
else if (!inputs.get(0).isInSparseFormat())
lnnz = executeDense(a.getDenseBlock(), b, scalars, out, m, n, sparseSafe, 0, m);
else
lnnz = executeSparse(a.getSparseBlock(), b, scalars, out, m, n, sparseSafe, 0, m);
} else // MULTI-THREADED
{
try {
ExecutorService pool = CommonThreadPool.get(k);
ArrayList<ParExecTask> tasks = new ArrayList<>();
int nk = 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 ParExecTask(a, b, scalars, out, m, n, sparseSafe, i * blklen, Math.min((i + 1) * blklen, m)));
// execute tasks
List<Future<Long>> taskret = pool.invokeAll(tasks);
pool.shutdown();
// aggregate nnz and error handling
for (Future<Long> task : taskret) lnnz += task.get();
if (_type == CellType.COL_AGG) {
// aggregate partial results
double[] c = out.getDenseBlockValues();
ValueFunction vfun = getAggFunction();
if (vfun instanceof KahanFunction) {
for (ParExecTask task : tasks) LibMatrixMult.vectAdd(task.getResult().getDenseBlockValues(), c, 0, 0, n);
} else {
for (ParExecTask task : tasks) {
double[] tmp = task.getResult().getDenseBlockValues();
for (int j = 0; j < n; j++) c[j] = vfun.execute(c[j], tmp[j]);
}
}
lnnz = out.recomputeNonZeros();
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
}
// post-processing
out.setNonZeros(lnnz);
out.examSparsity();
return out;
}
use of org.apache.sysml.runtime.compress.CompressedMatrixBlock 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);
}
use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project systemml by apache.
the class SpoofMultiAggregate method execute.
@Override
public MatrixBlock execute(ArrayList<MatrixBlock> inputs, ArrayList<ScalarObject> scalarObjects, MatrixBlock out, int k) {
// sanity check
if (inputs == null || inputs.size() < 1)
throw new RuntimeException("Invalid input arguments.");
long inputSize = isSparseSafe() ? getTotalInputNnz(inputs) : getTotalInputSize(inputs);
if (inputSize < PAR_NUMCELL_THRESHOLD) {
// serial execution
k = 1;
}
// result allocation and preparations
out.reset(1, _aggOps.length, false);
out.allocateDenseBlock();
// 1x<num_agg>
double[] c = out.getDenseBlockValues();
setInitialOutputValues(c);
// input preparation
SideInput[] b = prepInputMatrices(inputs);
double[] scalars = prepInputScalars(scalarObjects);
final int m = inputs.get(0).getNumRows();
final int n = inputs.get(0).getNumColumns();
boolean sparseSafe = isSparseSafe();
if (// SINGLE-THREADED
k <= 1) {
if (inputs.get(0) instanceof CompressedMatrixBlock)
executeCompressed((CompressedMatrixBlock) inputs.get(0), b, scalars, c, m, n, 0, m);
else if (!inputs.get(0).isInSparseFormat())
executeDense(inputs.get(0).getDenseBlock(), b, scalars, c, m, n, sparseSafe, 0, m);
else
executeSparse(inputs.get(0).getSparseBlock(), b, scalars, c, m, n, sparseSafe, 0, m);
} else // MULTI-THREADED
{
try {
ExecutorService pool = CommonThreadPool.get(k);
ArrayList<ParAggTask> tasks = new ArrayList<>();
int nk = UtilFunctions.roundToNext(Math.min(8 * k, m / 32), k);
int blklen = (int) (Math.ceil((double) m / nk));
for (int i = 0; i < nk & i * blklen < m; i++) tasks.add(new ParAggTask(inputs.get(0), 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
ArrayList<double[]> pret = new ArrayList<>();
for (Future<double[]> task : taskret) pret.add(task.get());
aggregatePartialResults(c, pret);
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
}
// post-processing
out.recomputeNonZeros();
out.examSparsity();
return out;
}
use of org.apache.sysml.runtime.compress.CompressedMatrixBlock in project systemml by apache.
the class SpoofOperator method prepInputMatrices.
protected SideInput[] prepInputMatrices(ArrayList<MatrixBlock> inputs, int offset, int len, boolean denseOnly, boolean tB1) {
SideInput[] b = new SideInput[len];
for (int i = offset; i < offset + len; i++) {
// decompress if necessary
if (inputs.get(i) instanceof CompressedMatrixBlock)
inputs.set(i, ((CompressedMatrixBlock) inputs.get(i)).decompress());
// transpose if necessary
int clen = inputs.get(i).getNumColumns();
MatrixBlock in = (tB1 && i == 1) ? LibMatrixReorg.transpose(inputs.get(i), new MatrixBlock(clen, inputs.get(i).getNumRows(), false)) : inputs.get(i);
// create side input
if (denseOnly && (in.isInSparseFormat() || !in.isAllocated())) {
// this in place because this block might be used by multiple threads)
if (// dense empty
in.getNumColumns() == 1 && in.isEmptyBlock(false))
b[i - offset] = new SideInput(null, null, clen);
else {
b[i - offset] = new SideInput(DataConverter.convertToDenseBlock(in, false), null, clen);
LOG.warn(getClass().getName() + ": Converted " + in.getNumRows() + "x" + in.getNumColumns() + ", nnz=" + in.getNonZeros() + " sideways input matrix from sparse to dense.");
}
} else if (in.isInSparseFormat() || !in.isAllocated()) {
b[i - offset] = new SideInput(null, in, clen);
} else {
b[i - offset] = new SideInput(in.getDenseBlock(), null, clen);
}
}
return b;
}
Aggregations