use of org.apache.sysml.udf.Scalar in project incubator-systemml by apache.
the class TimeWrapper method execute.
@Override
public void execute() {
long present = System.currentTimeMillis();
_ret = new Scalar(ScalarValueType.Double, String.valueOf(present));
}
use of org.apache.sysml.udf.Scalar in project systemml by apache.
the class DynamicWriteMatrixCP method execute.
@Override
public void execute() {
boolean success = false;
try {
Matrix mat = (Matrix) this.getFunctionInput(0);
String fname = ((Scalar) this.getFunctionInput(1)).getValue();
String format = ((Scalar) this.getFunctionInput(2)).getValue();
MatrixObject mo = mat.getMatrixObject();
MatrixCharacteristics mc = mo.getMatrixCharacteristics();
OutputInfo oi = OutputInfo.stringToOutputInfo(format);
MatrixBlock mb = mo.acquireRead();
DataConverter.writeMatrixToHDFS(mb, fname, oi, mc);
mo.release();
success = true;
} catch (Exception e) {
throw new RuntimeException("Error executing dynamic write of matrix", e);
}
_success = new Scalar(ScalarValueType.Boolean, String.valueOf(success));
}
use of org.apache.sysml.udf.Scalar in project systemml by apache.
the class MultiInputCbind method execute.
@Override
public void execute() {
int numInputs = Integer.parseInt(((Scalar) getFunctionInput(0)).getValue());
spagetize = Boolean.parseBoolean(((Scalar) getFunctionInput(1)).getValue());
// Compute output dimensions
numRetCols = 0;
if (spagetize) {
// Assumption the inputs are of same shape
MatrixBlock in = ((Matrix) getFunctionInput(2)).getMatrixObject().acquireRead();
numRetRows = in.getNumRows() * in.getNumColumns();
numRetCols = numInputs;
((Matrix) getFunctionInput(2)).getMatrixObject().release();
} else {
for (int inputID = 2; inputID < numInputs + 2; inputID++) {
MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
numRetRows = in.getNumRows();
numRetCols += in.getNumColumns();
((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
}
}
allocateOutput();
// Performs cbind (cbind (cbind ( X1, X2 ), X3 ), X4)
double[] retData = retMB.getDenseBlockValues();
int startColumn = 0;
for (int inputID = 2; inputID < numInputs + 2; inputID++) {
MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
if (spagetize && in.getNumRows() * in.getNumColumns() != numRetRows) {
throw new RuntimeException("Expected the inputs to be of same size when spagetization is turned on.");
}
int inputNumCols = in.getNumColumns();
if (in.isInSparseFormat()) {
Iterator<IJV> iter = in.getSparseBlockIterator();
while (iter.hasNext()) {
IJV ijv = iter.next();
if (spagetize) {
// Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
// Output Column ID = inputID-2 for all elements of inputs
int outputRowIndex = ijv.getI() * inputNumCols + ijv.getJ();
int outputColIndex = inputID - 2;
retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
} else {
// Traditional cbind
// Row ID remains the same as that of input
int outputRowIndex = ijv.getI();
int outputColIndex = ijv.getJ() + startColumn;
retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
}
}
} else {
double[] denseBlock = in.getDenseBlockValues();
if (denseBlock != null) {
if (spagetize) {
// Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
// Output Column ID = inputID-2 for all elements of inputs
int j = inputID - 2;
for (int i = 0; i < numRetRows; i++) {
retData[(int) (i * numRetCols + j)] = denseBlock[i];
}
} else {
// Row ID remains the same as that of input
for (int i = 0; i < retMB.getNumRows(); i++) {
for (int j = 0; j < inputNumCols; j++) {
int outputColIndex = j + startColumn;
retData[(int) (i * numRetCols + outputColIndex)] = denseBlock[i * inputNumCols + j];
}
}
}
}
}
((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
startColumn += inputNumCols;
}
retMB.recomputeNonZeros();
try {
retMB.examSparsity();
ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
} catch (DMLRuntimeException e) {
throw new RuntimeException("Error while executing MultiInputCbind", e);
} catch (IOException e) {
throw new RuntimeException("Error while executing MultiInputCbind", e);
}
}
use of org.apache.sysml.udf.Scalar in project systemml by apache.
the class CumSumProd method execute.
@Override
public void execute() {
X = ((Matrix) getFunctionInput(0)).getMatrixObject().acquireRead();
C = ((Matrix) getFunctionInput(1)).getMatrixObject().acquireRead();
if (X.getNumRows() != C.getNumRows())
throw new RuntimeException("Number of rows of X and C should match");
if (X.getNumColumns() != C.getNumColumns() && C.getNumColumns() != 1)
throw new RuntimeException("Incorrect Number of columns of X and C (Expected C to be of same dimension or a vector)");
start = Double.parseDouble(((Scalar) getFunctionInput(2)).getValue());
isReverse = Boolean.parseBoolean(((Scalar) getFunctionInput(3)).getValue());
numRetRows = X.getNumRows();
numRetCols = X.getNumColumns();
allocateOutput();
// Copy X to Y
denseBlock = retMB.getDenseBlockValues();
if (X.isInSparseFormat()) {
Iterator<IJV> iter = X.getSparseBlockIterator();
while (iter.hasNext()) {
IJV ijv = iter.next();
denseBlock[ijv.getI() * numRetCols + ijv.getJ()] = ijv.getV();
}
} else {
if (X.getDenseBlock() != null)
System.arraycopy(X.getDenseBlockValues(), 0, denseBlock, 0, denseBlock.length);
}
if (!isReverse) {
// Y [1, ] = X [1, ] + C [1, ] * start;
// Y [i+1, ] = X [i+1, ] + C [i+1, ] * Y [i, ]
addCNConstant(0, start);
for (int i = 1; i < numRetRows; i++) {
addC(i, true);
}
} else {
// Y [m, ] = X [m, ] + C [m, ] * start;
// Y [i-1, ] = X [i-1, ] + C [i-1, ] * Y [i, ]
addCNConstant(numRetRows - 1, start);
for (int i = numRetRows - 2; i >= 0; i--) {
addC(i, false);
}
}
((Matrix) getFunctionInput(1)).getMatrixObject().release();
((Matrix) getFunctionInput(0)).getMatrixObject().release();
retMB.recomputeNonZeros();
try {
retMB.examSparsity();
ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
} catch (DMLRuntimeException e) {
throw new RuntimeException("Error while executing CumSumProd", e);
} catch (IOException e) {
throw new RuntimeException("Error while executing CumSumProd", e);
}
}
Aggregations