use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.
the class OperationsOnMatrixValues method performShift.
public static void performShift(IndexedMatrixValue in, IndexRange ixrange, int brlen, int bclen, long rlen, long clen, ArrayList<IndexedMatrixValue> outlist) {
MatrixIndexes ix = in.getIndexes();
MatrixBlock mb = (MatrixBlock) in.getValue();
long start_lhs_globalRowIndex = ixrange.rowStart + (ix.getRowIndex() - 1) * brlen;
long start_lhs_globalColIndex = ixrange.colStart + (ix.getColumnIndex() - 1) * bclen;
long end_lhs_globalRowIndex = start_lhs_globalRowIndex + mb.getNumRows() - 1;
long end_lhs_globalColIndex = start_lhs_globalColIndex + mb.getNumColumns() - 1;
long start_lhs_rowIndex = UtilFunctions.computeBlockIndex(start_lhs_globalRowIndex, brlen);
long end_lhs_rowIndex = UtilFunctions.computeBlockIndex(end_lhs_globalRowIndex, brlen);
long start_lhs_colIndex = UtilFunctions.computeBlockIndex(start_lhs_globalColIndex, bclen);
long end_lhs_colIndex = UtilFunctions.computeBlockIndex(end_lhs_globalColIndex, bclen);
for (long leftRowIndex = start_lhs_rowIndex; leftRowIndex <= end_lhs_rowIndex; leftRowIndex++) {
for (long leftColIndex = start_lhs_colIndex; leftColIndex <= end_lhs_colIndex; leftColIndex++) {
// Calculate global index of right hand side block
long lhs_rl = Math.max((leftRowIndex - 1) * brlen + 1, start_lhs_globalRowIndex);
long lhs_ru = Math.min(leftRowIndex * brlen, end_lhs_globalRowIndex);
long lhs_cl = Math.max((leftColIndex - 1) * bclen + 1, start_lhs_globalColIndex);
long lhs_cu = Math.min(leftColIndex * bclen, end_lhs_globalColIndex);
int lhs_lrl = UtilFunctions.computeCellInBlock(lhs_rl, brlen);
int lhs_lru = UtilFunctions.computeCellInBlock(lhs_ru, brlen);
int lhs_lcl = UtilFunctions.computeCellInBlock(lhs_cl, bclen);
int lhs_lcu = UtilFunctions.computeCellInBlock(lhs_cu, bclen);
long rhs_rl = lhs_rl - ixrange.rowStart + 1;
long rhs_ru = rhs_rl + (lhs_ru - lhs_rl);
long rhs_cl = lhs_cl - ixrange.colStart + 1;
long rhs_cu = rhs_cl + (lhs_cu - lhs_cl);
int rhs_lrl = UtilFunctions.computeCellInBlock(rhs_rl, brlen);
int rhs_lru = UtilFunctions.computeCellInBlock(rhs_ru, brlen);
int rhs_lcl = UtilFunctions.computeCellInBlock(rhs_cl, bclen);
int rhs_lcu = UtilFunctions.computeCellInBlock(rhs_cu, bclen);
MatrixBlock slicedRHSBlk = mb.slice(rhs_lrl, rhs_lru, rhs_lcl, rhs_lcu, new MatrixBlock());
int lbrlen = UtilFunctions.computeBlockSize(rlen, leftRowIndex, brlen);
int lbclen = UtilFunctions.computeBlockSize(clen, leftColIndex, bclen);
MatrixBlock resultBlock = new MatrixBlock(lbrlen, lbclen, false);
resultBlock = resultBlock.leftIndexingOperations(slicedRHSBlk, lhs_lrl, lhs_lru, lhs_lcl, lhs_lcu, null, UpdateType.COPY);
outlist.add(new IndexedMatrixValue(new MatrixIndexes(leftRowIndex, leftColIndex), resultBlock));
}
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-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));
}
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class AggregateBinaryInstruction method processMapMultInstruction.
/**
* Helper function to perform map-side matrix-matrix multiplication.
*
* @param valueClass matrix value class
* @param cachedValues cached value map
* @param in1 indexed matrix value 1
* @param in2 indexed matrix value 2
* @param blockRowFactor ?
* @param blockColFactor ?
*/
private void processMapMultInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue in1, IndexedMatrixValue in2, int blockRowFactor, int blockColFactor) {
boolean removeOutput = true;
if (_cacheType.isRight()) {
DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input2);
long in2_cols = dcInput.getNumCols();
long in2_colBlocks = (long) Math.ceil(((double) in2_cols) / dcInput.getNumColsPerBlock());
for (int bidx = 1; bidx <= in2_colBlocks; bidx++) {
// Matrix multiply A[i,k] %*% B[k,bid]
// Setup input2 block
IndexedMatrixValue in2Block = dcInput.getDataBlock((int) in1.getIndexes().getColumnIndex(), bidx);
MatrixValue in2BlockValue = in2Block.getValue();
MatrixIndexes in2BlockIndex = in2Block.getIndexes();
// allocate space for the output value
IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
// process instruction
OperationsOnMatrixValues.performAggregateBinary(in1.getIndexes(), (MatrixBlock) in1.getValue(), in2BlockIndex, (MatrixBlock) in2BlockValue, out.getIndexes(), (MatrixBlock) out.getValue(), ((AggregateBinaryOperator) optr));
removeOutput &= (!_outputEmptyBlocks && out.getValue().isEmpty());
}
} else {
DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input1);
long in1_rows = dcInput.getNumRows();
long in1_rowsBlocks = (long) Math.ceil(((double) in1_rows) / dcInput.getNumRowsPerBlock());
for (int bidx = 1; bidx <= in1_rowsBlocks; bidx++) {
// Matrix multiply A[i,k] %*% B[k,bid]
// Setup input2 block
IndexedMatrixValue in1Block = dcInput.getDataBlock(bidx, (int) in2.getIndexes().getRowIndex());
MatrixValue in1BlockValue = in1Block.getValue();
MatrixIndexes in1BlockIndex = in1Block.getIndexes();
// allocate space for the output value
IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
// process instruction
OperationsOnMatrixValues.performAggregateBinary(in1BlockIndex, (MatrixBlock) in1BlockValue, in2.getIndexes(), (MatrixBlock) in2.getValue(), out.getIndexes(), (MatrixBlock) out.getValue(), ((AggregateBinaryOperator) optr));
removeOutput &= (!_outputEmptyBlocks && out.getValue().isEmpty());
}
}
// empty block output filter (enabled by compiler consumer operation is in CP)
if (removeOutput)
cachedValues.remove(output);
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class AppendGInstruction method processInstruction.
@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int brlen, int bclen) {
// setup basic meta data
int blen = _cbind ? bclen : brlen;
// Step 1: handle first input (forward blocks, change dim of last block)
ArrayList<IndexedMatrixValue> blkList1 = cachedValues.get(input1);
if (blkList1 != null)
for (IndexedMatrixValue in1 : blkList1) {
if (in1 == null)
continue;
if (_offset % blen == 0) {
// special case: forward only
cachedValues.add(output, in1);
} else // general case: change dims and forward
{
MatrixIndexes tmpix = in1.getIndexes();
// always block
MatrixBlock tmpval = (MatrixBlock) in1.getValue();
if (// border block
_cbind && _offset / blen + 1 == tmpix.getColumnIndex() || !_cbind && _offset / blen + 1 == tmpix.getRowIndex()) {
IndexedMatrixValue data = cachedValues.holdPlace(output, valueClass);
// always block
MatrixBlock tmpvalNew = (MatrixBlock) data.getValue();
int lrlen = _cbind ? tmpval.getNumRows() : Math.min(blen, (int) (_len - (tmpix.getRowIndex() - 1) * blen));
int lclen = _cbind ? Math.min(blen, (int) (_len - (tmpix.getColumnIndex() - 1) * blen)) : tmpval.getNumColumns();
tmpvalNew.reset(lrlen, lclen);
tmpvalNew.copy(0, tmpval.getNumRows() - 1, 0, tmpval.getNumColumns() - 1, tmpval, true);
data.getIndexes().setIndexes(tmpix);
} else // inner block
{
cachedValues.add(output, in1);
}
}
}
// Step 2: handle second input (split/forward blocks with new index)
ArrayList<IndexedMatrixValue> blkList2 = cachedValues.get(input2);
if (blkList2 != null)
for (IndexedMatrixValue in2 : blkList2) {
if (in2 == null)
continue;
MatrixIndexes tmpix = in2.getIndexes();
// always block
MatrixBlock tmpval = (MatrixBlock) in2.getValue();
if (// special case no split
_offset % bclen == 0) {
IndexedMatrixValue data = cachedValues.holdPlace(output, valueClass);
MatrixIndexes ix1 = data.getIndexes();
long rix = _cbind ? tmpix.getRowIndex() : _offset / blen + tmpix.getRowIndex();
long cix = _cbind ? _offset / blen + tmpix.getColumnIndex() : tmpix.getColumnIndex();
ix1.setIndexes(rix, cix);
data.set(ix1, in2.getValue());
} else // general case: split and forward
{
IndexedMatrixValue data1 = cachedValues.holdPlace(output, valueClass);
MatrixIndexes ix1 = data1.getIndexes();
// always block
MatrixBlock tmpvalNew = (MatrixBlock) data1.getValue();
if (_cbind) {
// first half
int cix1 = (int) (_offset / blen + tmpix.getColumnIndex());
int cols1 = Math.min(blen, (int) (_len - (long) (cix1 - 1) * blen));
ix1.setIndexes(tmpix.getRowIndex(), cix1);
tmpvalNew.reset(tmpval.getNumRows(), cols1);
tmpvalNew.copy(0, tmpval.getNumRows() - 1, (int) ((_offset + 1) % blen) - 1, cols1 - 1, tmpval.slice(0, tmpval.getNumRows() - 1, 0, (int) (cols1 - ((_offset) % blen) - 1), new MatrixBlock()), true);
data1.getIndexes().setIndexes(ix1);
if (cols1 - ((_offset) % blen) < tmpval.getNumColumns()) {
// second half (if required)
IndexedMatrixValue data2 = cachedValues.holdPlace(output, valueClass);
MatrixIndexes ix2 = data2.getIndexes();
// always block
MatrixBlock tmpvalNew2 = (MatrixBlock) data2.getValue();
int cix2 = (int) (_offset / blen + 1 + tmpix.getColumnIndex());
int cols2 = Math.min(blen, (int) (_len - (long) (cix2 - 1) * blen));
ix2.setIndexes(tmpix.getRowIndex(), cix2);
tmpvalNew2.reset(tmpval.getNumRows(), cols2);
tmpvalNew2.copy(0, tmpval.getNumRows() - 1, 0, cols2 - 1, tmpval.slice(0, tmpval.getNumRows() - 1, (int) (cols1 - ((_offset) % blen)), tmpval.getNumColumns() - 1, new MatrixBlock()), true);
data2.getIndexes().setIndexes(ix2);
}
} else // rbind
{
// first half
int rix1 = (int) (_offset / blen + tmpix.getRowIndex());
int rows1 = Math.min(blen, (int) (_len - (long) (rix1 - 1) * blen));
ix1.setIndexes(rix1, tmpix.getColumnIndex());
tmpvalNew.reset(rows1, tmpval.getNumColumns());
tmpvalNew.copy((int) ((_offset + 1) % blen) - 1, rows1 - 1, 0, tmpval.getNumColumns() - 1, tmpval.slice(0, (int) (rows1 - ((_offset) % blen) - 1), 0, tmpval.getNumColumns() - 1, new MatrixBlock()), true);
data1.getIndexes().setIndexes(ix1);
if (rows1 - ((_offset) % blen) < tmpval.getNumRows()) {
// second half (if required)
IndexedMatrixValue data2 = cachedValues.holdPlace(output, valueClass);
MatrixIndexes ix2 = data2.getIndexes();
// always block
MatrixBlock tmpvalNew2 = (MatrixBlock) data2.getValue();
int rix2 = (int) (_offset / blen + 1 + tmpix.getRowIndex());
int rows2 = Math.min(blen, (int) (_len - (long) (rix2 - 1) * blen));
ix2.setIndexes(rix2, tmpix.getColumnIndex());
tmpvalNew2.reset(rows2, tmpval.getNumColumns());
tmpvalNew2.copy(0, rows2 - 1, 0, tmpval.getNumColumns() - 1, tmpval.slice((int) (rows1 - ((_offset) % blen)), tmpval.getNumRows() - 1, 0, tmpval.getNumColumns() - 1, new MatrixBlock()), true);
data2.getIndexes().setIndexes(ix2);
}
}
}
}
}
use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project systemml by apache.
the class BinUaggChainInstruction 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(input);
if (blkList == null)
return;
for (IndexedMatrixValue imv : blkList) {
if (imv == null)
continue;
MatrixIndexes inIx = imv.getIndexes();
MatrixValue inVal = imv.getValue();
// allocate space for the intermediate and output value
IndexedMatrixValue iout = cachedValues.holdPlace(output, valueClass);
MatrixIndexes outIx = iout.getIndexes();
MatrixValue outVal = iout.getValue();
// process instruction
OperationsOnMatrixValues.performAggregateUnary(inIx, inVal, _tmpIx, _tmpVal, _uaggOp, blockRowFactor, blockColFactor);
((MatrixBlock) _tmpVal).dropLastRowsOrColumns(_uaggOp.aggOp.correctionLocation);
OperationsOnMatrixValues.performBinaryIgnoreIndexes(inVal, _tmpVal, outVal, _bOp);
outIx.setIndexes(inIx);
}
}
Aggregations