use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class QuaternaryInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
QuaternaryOperator qop = (QuaternaryOperator) optr;
ArrayList<IndexedMatrixValue> blkList = cachedValues.get(_input1);
if (blkList != null)
for (IndexedMatrixValue imv : blkList) {
// Step 1: prepare inputs and output
if (imv == null)
continue;
MatrixIndexes inIx = imv.getIndexes();
MatrixBlock inVal = (MatrixBlock) imv.getValue();
// allocate space for the output value
IndexedMatrixValue iout = null;
if (output == _input1)
iout = tempValue;
else
iout = cachedValues.holdPlace(output, valueClass);
MatrixIndexes outIx = iout.getIndexes();
MatrixValue outVal = iout.getValue();
// Step 2: get remaining inputs: Wij, Ui, Vj
MatrixBlock Xij = inVal;
// get Wij if existing (null of WeightsType.NONE or WSigmoid any type)
IndexedMatrixValue iWij = (_input4 != -1) ? cachedValues.getFirst(_input4) : null;
MatrixValue Wij = (iWij != null) ? iWij.getValue() : null;
if (null == Wij && qop.hasFourInputs()) {
MatrixBlock mb = new MatrixBlock(1, 1, false);
String[] parts = InstructionUtils.getInstructionParts(instString);
mb.quickSetValue(0, 0, Double.valueOf(parts[4]));
Wij = mb;
}
// get Ui and Vj, potentially through distributed cache
MatrixValue Ui = // U
(!_cacheU) ? // U
cachedValues.getFirst(_input2).getValue() : MRBaseForCommonInstructions.dcValues.get(_input2).getDataBlock((int) inIx.getRowIndex(), 1).getValue();
MatrixValue Vj = // t(V)
(!_cacheV) ? // t(V)
cachedValues.getFirst(_input3).getValue() : MRBaseForCommonInstructions.dcValues.get(_input3).getDataBlock((int) inIx.getColumnIndex(), 1).getValue();
// handle special input case: //V through shuffle -> t(V)
if (Ui.getNumColumns() != Vj.getNumColumns()) {
Vj = LibMatrixReorg.reorg((MatrixBlock) Vj, new MatrixBlock(Vj.getNumColumns(), Vj.getNumRows(), Vj.isInSparseFormat()), new ReorgOperator(SwapIndex.getSwapIndexFnObject()));
}
// Step 3: process instruction
Xij.quaternaryOperations(qop, (MatrixBlock) Ui, (MatrixBlock) Vj, (MatrixBlock) Wij, (MatrixBlock) outVal);
if (qop.wtype1 != null || qop.wtype4 != null)
// wsloss
outIx.setIndexes(1, 1);
else if (qop.wtype2 != null || qop.wtype5 != null || qop.wtype3 != null && qop.wtype3.isBasic())
// wsigmoid/wdivmm-basic
outIx.setIndexes(inIx);
else {
// wdivmm
boolean left = qop.wtype3.isLeft();
outIx.setIndexes(left ? inIx.getColumnIndex() : inIx.getRowIndex(), 1);
}
// put the output value in the cache
if (iout == tempValue)
cachedValues.add(output, iout);
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class GroupedAggregateMInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
ArrayList<IndexedMatrixValue> blkList = cachedValues.get(input1);
if (blkList == null)
return;
for (IndexedMatrixValue in1 : blkList) {
if (in1 == null)
continue;
DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input2);
// get all inputs
MatrixIndexes ix = in1.getIndexes();
MatrixBlock groups = (MatrixBlock) dcInput.getDataBlock((int) ix.getRowIndex(), 1).getValue();
// output blocked result
int brlen = dcInput.getNumRowsPerBlock();
int bclen = dcInput.getNumColsPerBlock();
// execute map grouped aggregate operations
ArrayList<IndexedMatrixValue> outlist = new ArrayList<>();
OperationsOnMatrixValues.performMapGroupedAggregate(getOperator(), in1, groups, _ngroups, brlen, bclen, outlist);
// output all result blocks
for (IndexedMatrixValue out : outlist) {
cachedValues.add(output, out);
}
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class BinaryMInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
ArrayList<IndexedMatrixValue> blkList = cachedValues.get(input1);
if (blkList == null)
return;
for (IndexedMatrixValue in1 : blkList) {
// allocate space for the output value
// try to avoid coping as much as possible
IndexedMatrixValue out;
if ((output != input1 && output != input2))
out = cachedValues.holdPlace(output, valueClass);
else
out = tempValue;
// get second
DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input2);
IndexedMatrixValue in2 = null;
if (_vectorType == VectorType.COL_VECTOR)
in2 = dcInput.getDataBlock((int) in1.getIndexes().getRowIndex(), 1);
else
// _vectorType == VectorType.ROW_VECTOR
in2 = dcInput.getDataBlock(1, (int) in1.getIndexes().getColumnIndex());
// process instruction
out.getIndexes().setIndexes(in1.getIndexes());
OperationsOnMatrixValues.performBinaryIgnoreIndexes(in1.getValue(), in2.getValue(), out.getValue(), ((BinaryOperator) optr));
// put the output value in the cache
if (out == tempValue)
cachedValues.add(output, out);
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class CumulativeOffsetInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
// original data
IndexedMatrixValue in1 = cachedValues.getFirst(input1);
// offset row vector
IndexedMatrixValue in2 = cachedValues.getFirst(input2);
if (in1 == null || in2 == null)
throw new DMLRuntimeException("Unexpected empty input (left=" + ((in1 == null) ? "null" : in1.getIndexes()) + ", right=" + ((in2 == null) ? "null" : in2.getIndexes()) + ").");
// prepare inputs and outputs
IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
MatrixBlock data = (MatrixBlock) in1.getValue();
MatrixBlock offset = (MatrixBlock) in2.getValue();
MatrixBlock blk = (MatrixBlock) out.getValue();
blk.reset(data.getNumRows(), data.getNumColumns());
// blockwise offset aggregation and prefix sum computation
// cp data
MatrixBlock data2 = new MatrixBlock(data);
// 1-based
MatrixBlock fdata2 = data2.slice(0, 0, 0, data2.getNumColumns() - 1, new MatrixBlock());
// sum offset to first row
fdata2.binaryOperationsInPlace(_bop, offset);
// 0-based
data2.copy(0, 0, 0, data2.getNumColumns() - 1, fdata2, true);
// compute columnwise prefix sums/prod/min/max
data2.unaryOperations(_uop, blk);
// set output indexes
out.getIndexes().setIndexes(in1.getIndexes());
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class LibMatrixReorg method rev.
public static void rev(IndexedMatrixValue in, long rlen, int brlen, ArrayList<IndexedMatrixValue> out) {
// input block reverse
MatrixIndexes inix = in.getIndexes();
MatrixBlock inblk = (MatrixBlock) in.getValue();
MatrixBlock tmpblk = rev(inblk, new MatrixBlock(inblk.getNumRows(), inblk.getNumColumns(), inblk.isInSparseFormat()));
// split and expand block if necessary (at most 2 blocks)
if (// special case: aligned blocks
rlen % brlen == 0) {
int nrblks = (int) Math.ceil((double) rlen / brlen);
out.add(new IndexedMatrixValue(new MatrixIndexes(nrblks - inix.getRowIndex() + 1, inix.getColumnIndex()), tmpblk));
} else // general case: unaligned blocks
{
// compute target positions and sizes
long pos1 = rlen - UtilFunctions.computeCellIndex(inix.getRowIndex(), brlen, tmpblk.getNumRows() - 1) + 1;
long pos2 = pos1 + tmpblk.getNumRows() - 1;
int ipos1 = UtilFunctions.computeCellInBlock(pos1, brlen);
int iposCut = tmpblk.getNumRows() - ipos1 - 1;
int blkix1 = (int) UtilFunctions.computeBlockIndex(pos1, brlen);
int blkix2 = (int) UtilFunctions.computeBlockIndex(pos2, brlen);
int blklen1 = (int) UtilFunctions.computeBlockSize(rlen, blkix1, brlen);
int blklen2 = (int) UtilFunctions.computeBlockSize(rlen, blkix2, brlen);
// slice first block
MatrixIndexes outix1 = new MatrixIndexes(blkix1, inix.getColumnIndex());
MatrixBlock outblk1 = new MatrixBlock(blklen1, inblk.getNumColumns(), inblk.isInSparseFormat());
MatrixBlock tmp1 = tmpblk.slice(0, iposCut, 0, tmpblk.getNumColumns() - 1, new MatrixBlock());
outblk1.leftIndexingOperations(tmp1, ipos1, ipos1 + tmp1.getNumRows() - 1, 0, tmpblk.getNumColumns() - 1, outblk1, UpdateType.INPLACE_PINNED);
out.add(new IndexedMatrixValue(outix1, outblk1));
// slice second block (if necessary)
if (blkix1 != blkix2) {
MatrixIndexes outix2 = new MatrixIndexes(blkix2, inix.getColumnIndex());
MatrixBlock outblk2 = new MatrixBlock(blklen2, inblk.getNumColumns(), inblk.isInSparseFormat());
MatrixBlock tmp2 = tmpblk.slice(iposCut + 1, tmpblk.getNumRows() - 1, 0, tmpblk.getNumColumns() - 1, new MatrixBlock());
outblk2.leftIndexingOperations(tmp2, 0, tmp2.getNumRows() - 1, 0, tmpblk.getNumColumns() - 1, outblk2, UpdateType.INPLACE_PINNED);
out.add(new IndexedMatrixValue(outix2, outblk2));
}
}
}
Aggregations