use of org.apache.sysml.runtime.matrix.data.MatrixBlock in project incubator-systemml by apache.
the class QuaternaryCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
QuaternaryOperator qop = (QuaternaryOperator) _optr;
MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName());
MatrixBlock matBlock2 = ec.getMatrixInput(input2.getName());
MatrixBlock matBlock3 = ec.getMatrixInput(input3.getName());
MatrixBlock matBlock4 = null;
if (qop.hasFourInputs()) {
if (input4.getDataType() == DataType.SCALAR) {
matBlock4 = new MatrixBlock(1, 1, false);
final double eps = ec.getScalarInput(input4.getName(), input4.getValueType(), input4.isLiteral()).getDoubleValue();
matBlock4.quickSetValue(0, 0, eps);
} else {
matBlock4 = ec.getMatrixInput(input4.getName());
}
}
//core execute
MatrixValue out = matBlock1.quaternaryOperations(qop, matBlock2, matBlock3, matBlock4, new MatrixBlock(), _numThreads);
//release inputs and output
ec.releaseMatrixInput(input1.getName());
ec.releaseMatrixInput(input2.getName());
ec.releaseMatrixInput(input3.getName());
if (qop.wtype1 != null || qop.wtype4 != null) {
//wsloss/wcemm
if ((qop.wtype1 != null && qop.wtype1.hasFourInputs()) || (qop.wtype4 != null && qop.wtype4.hasFourInputs()))
if (input4.getDataType() == DataType.MATRIX) {
ec.releaseMatrixInput(input4.getName());
}
ec.setVariable(output.getName(), new DoubleObject(out.getValue(0, 0)));
} else {
//wsigmoid / wdivmm / wumm
if (qop.wtype3 != null && qop.wtype3.hasFourInputs())
if (input4.getDataType() == DataType.MATRIX) {
ec.releaseMatrixInput(input4.getName());
}
ec.setMatrixOutput(output.getName(), (MatrixBlock) out);
}
}
use of org.apache.sysml.runtime.matrix.data.MatrixBlock in project incubator-systemml by apache.
the class MultiReturnParameterizedBuiltinCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
//obtain and pin input frame
FrameBlock fin = ec.getFrameInput(input1.getName());
String spec = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getStringValue();
String[] colnames = fin.getColumnNames();
//execute block transform encode
Encoder encoder = EncoderFactory.createEncoder(spec, colnames, fin.getNumColumns(), null);
//build and apply
MatrixBlock data = encoder.encode(fin, new MatrixBlock(fin.getNumRows(), fin.getNumColumns(), false));
FrameBlock meta = encoder.getMetaData(new FrameBlock(fin.getNumColumns(), ValueType.STRING));
meta.setColumnNames(colnames);
//release input and outputs
ec.releaseFrameInput(input1.getName());
ec.setMatrixOutput(getOutput(0).getName(), data);
ec.setFrameOutput(getOutput(1).getName(), meta);
}
use of org.apache.sysml.runtime.matrix.data.MatrixBlock in project incubator-systemml by apache.
the class MMTSJCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
//get inputs
MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName());
//execute operations
MatrixBlock ret = (MatrixBlock) matBlock1.transposeSelfMatrixMultOperations(new MatrixBlock(), _type, _numThreads);
//set output and release inputs
ec.setMatrixOutput(output.getName(), ret);
ec.releaseMatrixInput(input1.getName());
}
use of org.apache.sysml.runtime.matrix.data.MatrixBlock in project incubator-systemml by apache.
the class MatrixAppendCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
//get inputs
MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName());
MatrixBlock matBlock2 = ec.getMatrixInput(input2.getName());
//check input dimensions
if (_type == AppendType.CBIND && matBlock1.getNumRows() != matBlock2.getNumRows()) {
throw new DMLRuntimeException("Append-cbind is not possible for input matrices " + input1.getName() + " and " + input2.getName() + " with different number of rows: " + matBlock1.getNumRows() + " vs " + matBlock2.getNumRows());
} else if (_type == AppendType.RBIND && matBlock1.getNumColumns() != matBlock2.getNumColumns()) {
throw new DMLRuntimeException("Append-rbind is not possible for input matrices " + input1.getName() + " and " + input2.getName() + " with different number of columns: " + matBlock1.getNumColumns() + " vs " + matBlock2.getNumColumns());
}
//execute append operations (append both inputs to initially empty output)
MatrixBlock ret = matBlock1.appendOperations(matBlock2, new MatrixBlock(), _type == AppendType.CBIND);
//set output and release inputs
ec.setMatrixOutput(output.getName(), ret);
ec.releaseMatrixInput(input1.getName());
ec.releaseMatrixInput(input2.getName());
}
use of org.apache.sysml.runtime.matrix.data.MatrixBlock in project incubator-systemml by apache.
the class WriterBinaryBlock method writeBinaryBlockMatrixToSequenceFile.
@SuppressWarnings("deprecation")
protected final void writeBinaryBlockMatrixToSequenceFile(Path path, JobConf job, FileSystem fs, MatrixBlock src, int brlen, int bclen, int rl, int ru) throws DMLRuntimeException, IOException {
boolean sparse = src.isInSparseFormat();
int rlen = src.getNumRows();
int clen = src.getNumColumns();
// 1) create sequence file writer, with right replication factor
// (config via MRConfigurationNames.DFS_REPLICATION not possible since sequence file internally calls fs.getDefaultReplication())
SequenceFile.Writer writer = null;
if (//if replication specified (otherwise default)
_replication > 0) {
//copy of SequenceFile.Writer(fs, job, path, MatrixIndexes.class, MatrixBlock.class), except for replication
writer = new SequenceFile.Writer(fs, job, path, MatrixIndexes.class, MatrixBlock.class, job.getInt(MRConfigurationNames.IO_FILE_BUFFER_SIZE, 4096), (short) _replication, fs.getDefaultBlockSize(), null, new SequenceFile.Metadata());
} else {
writer = new SequenceFile.Writer(fs, job, path, MatrixIndexes.class, MatrixBlock.class);
}
try {
// 2) bound check for src block
if (src.getNumRows() > rlen || src.getNumColumns() > clen) {
throw new IOException("Matrix block [1:" + src.getNumRows() + ",1:" + src.getNumColumns() + "] " + "out of overall matrix range [1:" + rlen + ",1:" + clen + "].");
}
//3) reblock and write
MatrixIndexes indexes = new MatrixIndexes();
if (//opt for single block
rlen <= brlen && clen <= bclen && rl == 0) {
//directly write single block
indexes.setIndexes(1, 1);
writer.append(indexes, src);
} else //general case
{
//initialize blocks for reuse (at most 4 different blocks required)
MatrixBlock[] blocks = createMatrixBlocksForReuse(rlen, clen, brlen, bclen, sparse, src.getNonZeros());
//create and write subblocks of matrix
for (int blockRow = rl / brlen; blockRow < (int) Math.ceil(ru / (double) brlen); blockRow++) for (int blockCol = 0; blockCol < (int) Math.ceil(src.getNumColumns() / (double) bclen); blockCol++) {
int maxRow = (blockRow * brlen + brlen < src.getNumRows()) ? brlen : src.getNumRows() - blockRow * brlen;
int maxCol = (blockCol * bclen + bclen < src.getNumColumns()) ? bclen : src.getNumColumns() - blockCol * bclen;
int row_offset = blockRow * brlen;
int col_offset = blockCol * bclen;
//get reuse matrix block
MatrixBlock block = getMatrixBlockForReuse(blocks, maxRow, maxCol, brlen, bclen);
//copy submatrix to block
src.sliceOperations(row_offset, row_offset + maxRow - 1, col_offset, col_offset + maxCol - 1, block);
//append block to sequence file
indexes.setIndexes(blockRow + 1, blockCol + 1);
writer.append(indexes, block);
//reset block for later reuse
block.reset();
}
}
} finally {
IOUtilFunctions.closeSilently(writer);
}
}
Aggregations