use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.
the class LibMatrixReorg method reshape.
/**
* MR/SPARK reshape interface - for reshape we cannot view blocks independently, and hence,
* there are different CP and MR interfaces.
*
* @param in indexed matrix value
* @param mcIn input matrix characteristics
* @param out list of indexed matrix values
* @param mcOut output matrix characteristics
* @param rowwise if true, reshape by row
* @return list of indexed matrix values
*/
public static ArrayList<IndexedMatrixValue> reshape(IndexedMatrixValue in, MatrixCharacteristics mcIn, ArrayList<IndexedMatrixValue> out, MatrixCharacteristics mcOut, boolean rowwise) {
// prepare inputs
MatrixIndexes ixIn = in.getIndexes();
MatrixBlock mbIn = (MatrixBlock) in.getValue();
// prepare result blocks (no reuse in order to guarantee mem constraints)
Collection<MatrixIndexes> rix = computeAllResultBlockIndexes(ixIn, mcIn, mcOut, rowwise);
HashMap<MatrixIndexes, MatrixBlock> rblk = createAllResultBlocks(rix, mbIn.nonZeros, mcIn, mcOut, rowwise, out);
// basic algorithm
long row_offset = (ixIn.getRowIndex() - 1) * mcIn.getRowsPerBlock();
long col_offset = (ixIn.getColumnIndex() - 1) * mcIn.getColsPerBlock();
if (mbIn.sparse)
reshapeSparse(mbIn, row_offset, col_offset, rblk, mcIn, mcOut, rowwise);
else
// dense
reshapeDense(mbIn, row_offset, col_offset, rblk, mcIn, mcOut, rowwise);
// prepare output
out = new ArrayList<>();
for (Entry<MatrixIndexes, MatrixBlock> e : rblk.entrySet()) {
// ensure correct format
e.getValue().examSparsity();
out.add(new IndexedMatrixValue(e.getKey(), e.getValue()));
}
return out;
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.
the class OperationsOnMatrixValues method performMapGroupedAggregate.
public static void performMapGroupedAggregate(Operator op, IndexedMatrixValue inTarget, MatrixBlock groups, int ngroups, int brlen, int bclen, ArrayList<IndexedMatrixValue> outlist) {
MatrixIndexes ix = inTarget.getIndexes();
MatrixBlock target = (MatrixBlock) inTarget.getValue();
// execute grouped aggregate operations
MatrixBlock out = groups.groupedAggOperations(target, null, new MatrixBlock(), ngroups, op);
if (out.getNumRows() <= brlen && out.getNumColumns() <= bclen) {
// single output block
outlist.add(new IndexedMatrixValue(new MatrixIndexes(1, ix.getColumnIndex()), out));
} else {
// multiple output blocks (by op def, single column block )
for (int blockRow = 0; blockRow < (int) Math.ceil(out.getNumRows() / (double) brlen); blockRow++) {
int maxRow = (blockRow * brlen + brlen < out.getNumRows()) ? brlen : out.getNumRows() - blockRow * brlen;
int row_offset = blockRow * brlen;
// copy submatrix to block
MatrixBlock tmp = out.slice(row_offset, row_offset + maxRow - 1, 0, out.getNumColumns() - 1, new MatrixBlock());
// append block to result cache
outlist.add(new IndexedMatrixValue(new MatrixIndexes(blockRow + 1, ix.getColumnIndex()), tmp));
}
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.
the class OperationsOnMatrixValues method performSlice.
@SuppressWarnings("rawtypes")
public static ArrayList performSlice(IndexRange ixrange, int brlen, int bclen, int iix, int jix, MatrixBlock in) {
IndexedMatrixValue imv = new IndexedMatrixValue(new MatrixIndexes(iix, jix), (MatrixBlock) in);
ArrayList<IndexedMatrixValue> outlist = new ArrayList<>();
performSlice(imv, ixrange, brlen, bclen, outlist);
return SparkUtils.fromIndexedMatrixBlockToPair(outlist);
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class TernaryInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
MatrixBlock lm1 = input1.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput1).getValue() : m1;
MatrixBlock lm2 = input2.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput2).getValue() : m2;
MatrixBlock lm3 = input3.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput3).getValue() : m3;
MatrixIndexes ixin = input1.isMatrix() ? cachedValues.getFirst(ixinput1).getIndexes() : input2.isMatrix() ? cachedValues.getFirst(ixinput2).getIndexes() : cachedValues.getFirst(ixinput3).getIndexes();
// prepare output
IndexedMatrixValue out = new IndexedMatrixValue(new MatrixIndexes(), new MatrixBlock());
out.getIndexes().setIndexes(ixin);
// process instruction
TernaryOperator op = (TernaryOperator) optr;
lm1.ternaryOperations(op, lm2, lm3, (MatrixBlock) out.getValue());
// put the output value in the cache
cachedValues.add(ixoutput, out);
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class UaggOuterChainInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
ArrayList<IndexedMatrixValue> blkList = null;
boolean rightCached = (_uaggOp.indexFn instanceof ReduceCol || _uaggOp.indexFn instanceof ReduceAll || !LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp));
// get the main data input
if (rightCached)
blkList = cachedValues.get(input1);
else
// ReduceRow
blkList = cachedValues.get(input2);
if (blkList == null)
return;
for (IndexedMatrixValue imv : blkList) {
if (imv == null)
continue;
MatrixIndexes in1Ix = imv.getIndexes();
MatrixValue in1Val = imv.getValue();
// allocate space for the intermediate and output value
IndexedMatrixValue iout = cachedValues.holdPlace(output, valueClass);
MatrixIndexes outIx = iout.getIndexes();
MatrixValue outVal = iout.getValue();
MatrixBlock corr = null;
// get the distributed cache input
byte dcInputIx = rightCached ? input2 : input1;
DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(dcInputIx);
// process instruction
if (LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp)) {
if ((LibMatrixOuterAgg.isRowIndexMax(_uaggOp)) || (LibMatrixOuterAgg.isRowIndexMin(_uaggOp))) {
if (_bv == null) {
if (rightCached)
_bv = dcInput.getRowVectorArray();
else
_bv = dcInput.getColumnVectorArray();
_bvi = LibMatrixOuterAgg.prepareRowIndices(_bv.length, _bv, _bOp, _uaggOp);
}
} else {
// step 1: prepare sorted rhs input (once per task)
if (_bv == null) {
if (rightCached)
_bv = dcInput.getRowVectorArray();
else
_bv = dcInput.getColumnVectorArray();
Arrays.sort(_bv);
}
}
LibMatrixOuterAgg.resetOutputMatrix(in1Ix, (MatrixBlock) in1Val, outIx, (MatrixBlock) outVal, _uaggOp);
LibMatrixOuterAgg.aggregateMatrix((MatrixBlock) in1Val, (MatrixBlock) outVal, _bv, _bvi, _bOp, _uaggOp);
} else // default case
{
long in2_cols = dcInput.getNumCols();
long in2_colBlocks = (long) Math.ceil(((double) in2_cols) / dcInput.getNumColsPerBlock());
for (int bidx = 1; bidx <= in2_colBlocks; bidx++) {
IndexedMatrixValue imv2 = dcInput.getDataBlock(1, bidx);
MatrixValue in2Val = imv2.getValue();
// outer block operation
OperationsOnMatrixValues.performBinaryIgnoreIndexes(in1Val, in2Val, _tmpVal1, _bOp);
// unary aggregate operation
OperationsOnMatrixValues.performAggregateUnary(in1Ix, _tmpVal1, outIx, _tmpVal2, _uaggOp, blockRowFactor, blockColFactor);
// aggregate over all rhs blocks
if (corr == null) {
outVal.reset(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
corr = new MatrixBlock(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
}
if (_aggOp.correctionExists)
OperationsOnMatrixValues.incrementalAggregation(outVal, corr, _tmpVal2, _aggOp, true);
else
OperationsOnMatrixValues.incrementalAggregation(outVal, null, _tmpVal2, _aggOp, true);
}
}
}
}
Aggregations