use of org.apache.sysml.udf.Matrix in project systemml by apache.
the class DynamicReadMatrixCP method execute.
@Override
public void execute() {
try {
String fname = ((Scalar) this.getFunctionInput(0)).getValue();
Integer m = Integer.parseInt(((Scalar) this.getFunctionInput(1)).getValue());
Integer n = Integer.parseInt(((Scalar) this.getFunctionInput(2)).getValue());
String format = ((Scalar) this.getFunctionInput(3)).getValue();
InputInfo ii = InputInfo.stringToInputInfo(format);
OutputInfo oi = OutputInfo.BinaryBlockOutputInfo;
MatrixBlock mbTmp = DataConverter.readMatrixFromHDFS(fname, ii, m, n, ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize());
String fnameTmp = createOutputFilePathAndName("TMP");
_ret = new Matrix(fnameTmp, m, n, ValueType.Double);
_ret.setMatrixDoubleArray(mbTmp, oi, ii);
// NOTE: The packagesupport wrapper creates a new MatrixObjectNew with the given
// matrix block. This leads to a dirty state of the new object. Hence, the resulting
// intermediate plan variable will be exported in front of MR jobs and during this export
// the format will be changed to binary block (the contract of external functions),
// no matter in which format the original matrix was.
} catch (Exception e) {
throw new RuntimeException("Error executing dynamic read of matrix", e);
}
}
use of org.apache.sysml.udf.Matrix in project systemml by apache.
the class DynamicReadMatrixRcCP method execute.
@Override
public void execute() {
try {
String fname = ((Scalar) this.getFunctionInput(0)).getValue();
Integer m = Integer.parseInt(((Scalar) this.getFunctionInput(1)).getValue());
Integer n = Integer.parseInt(((Scalar) this.getFunctionInput(2)).getValue());
String format = ((Scalar) this.getFunctionInput(3)).getValue();
InputInfo ii = InputInfo.stringToInputInfo(format);
OutputInfo oi = OutputInfo.BinaryBlockOutputInfo;
String fnameTmp = createOutputFilePathAndName("TMP");
_ret = new Matrix(fnameTmp, m, n, ValueType.Double);
MatrixBlock mbTmp = DataConverter.readMatrixFromHDFS(fname, ii, m, n, ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize());
_ret.setMatrixDoubleArray(mbTmp, oi, ii);
_rc = new Scalar(ScalarValueType.Integer, "0");
// NOTE: The packagesupport wrapper creates a new MatrixObjectNew with the given
// matrix block. This leads to a dirty state of the new object. Hence, the resulting
// intermediate plan variable will be exported in front of MR jobs and during this export
// the format will be changed to binary block (the contract of external functions),
// no matter in which format the original matrix was.
} catch (Exception e) {
_rc = new Scalar(ScalarValueType.Integer, "1");
// throw new PackageRuntimeException("Error executing dynamic read of matrix",e);
}
}
use of org.apache.sysml.udf.Matrix in project systemml by apache.
the class EvalFunction method execute.
@Override
public void execute(ExecutionContext ec) {
String fname = ((Scalar) getFunctionInput(0)).getValue();
MatrixObject in = ((Matrix) getFunctionInput(1)).getMatrixObject();
ArrayList<String> inputs = new ArrayList<>();
inputs.add("A");
ArrayList<String> outputs = new ArrayList<>();
outputs.add("B");
ExecutionContext ec2 = ExecutionContextFactory.createContext(ec.getProgram());
CPOperand inName = new CPOperand("TMP", org.apache.sysml.parser.Expression.ValueType.DOUBLE, DataType.MATRIX);
ec2.setVariable("TMP", in);
FunctionCallCPInstruction fcpi = new FunctionCallCPInstruction(null, fname, new CPOperand[] { inName }, inputs, outputs, "eval func");
fcpi.processInstruction(ec2);
MatrixObject out = (MatrixObject) ec2.getVariable("B");
_ret = new Matrix(out, ValueType.Double);
}
use of org.apache.sysml.udf.Matrix in project systemml by apache.
the class MultiInputCbind method allocateOutput.
private void allocateOutput() {
String dir = createOutputFilePathAndName("TMP");
ret = new Matrix(dir, numRetRows, numRetCols, ValueType.Double);
retMB = new MatrixBlock((int) numRetRows, (int) numRetCols, false);
retMB.allocateDenseBlock();
}
use of org.apache.sysml.udf.Matrix in project systemml by apache.
the class SGDNesterovUpdate method execute.
@Override
public void execute() {
try {
MatrixBlock X = ((Matrix) getFunctionInput(0)).getMatrixObject().acquireRead();
MatrixBlock dX = ((Matrix) getFunctionInput(1)).getMatrixObject().acquireRead();
double lr = Double.parseDouble(((Scalar) getFunctionInput(2)).getValue());
double mu = Double.parseDouble(((Scalar) getFunctionInput(3)).getValue());
MatrixBlock v = ((Matrix) getFunctionInput(4)).getMatrixObject().acquireRead();
double lambda = Double.parseDouble(((Scalar) getFunctionInput(5)).getValue());
// v = mu * v - lr * dX - lr*lambda*X
updatedV = new Matrix("tmp_" + rand.nextLong(), v.getNumRows(), v.getNumColumns(), ValueType.Double);
MatrixBlock updatedVMB = allocateDenseMatrixBlock(updatedV);
double[] updatedVData = updatedVMB.getDenseBlockValues();
if (isDense(v) && isDense(dX) && isDense(X)) {
double[] vArr = v.getDenseBlockValues();
double[] dXArr = dX.getDenseBlockValues();
double[] XArr = X.getDenseBlockValues();
int nnz = 0;
for (int i = 0; i < updatedVData.length; i++) {
updatedVData[i] = mu * vArr[i] - lr * dXArr[i] - lr * lambda * XArr[i];
nnz += (updatedVData[i] != 0) ? 1 : 0;
}
updatedVMB.setNonZeros(nnz);
} else {
multiplyByConstant(v, mu, updatedVData);
multiplyByConstant(dX, -lr, updatedVData);
multiplyByConstant(X, -lr * lambda, updatedVData);
updatedVMB.recomputeNonZeros();
}
updatedV.setMatrixDoubleArray(updatedVMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
// X = X - mu * v_prev + (1 + mu) * v
updatedX = new Matrix("tmp_" + rand.nextLong(), X.getNumRows(), X.getNumColumns(), ValueType.Double);
MatrixBlock updatedXMB = allocateDenseMatrixBlock(updatedX);
double[] updatedXData = updatedXMB.getDenseBlockValues();
if (isDense(X) && isDense(v)) {
double[] XArr = X.getDenseBlockValues();
double[] vPrevArr = v.getDenseBlockValues();
int nnz = 0;
double muPlus1 = mu + 1;
for (int i = 0; i < updatedXData.length; i++) {
updatedXData[i] = XArr[i] - mu * vPrevArr[i] + muPlus1 * updatedVData[i];
nnz += (updatedXData[i] != 0) ? 1 : 0;
}
updatedXMB.setNonZeros(nnz);
} else if (isDense(v)) {
copy(X, updatedXData);
double[] vPrevArr = v.getDenseBlockValues();
int nnz = 0;
double muPlus1 = mu + 1;
for (int i = 0; i < updatedXData.length; i++) {
updatedXData[i] += -mu * vPrevArr[i] + muPlus1 * updatedVData[i];
nnz += (updatedXData[i] != 0) ? 1 : 0;
}
updatedXMB.setNonZeros(nnz);
} else {
copy(X, updatedXData);
multiplyByConstant(v, -mu, updatedXData);
multiplyByConstant(updatedVData, 1 + mu, updatedXData);
updatedXMB.recomputeNonZeros();
}
updatedX.setMatrixDoubleArray(updatedXMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
((Matrix) getFunctionInput(0)).getMatrixObject().release();
((Matrix) getFunctionInput(1)).getMatrixObject().release();
((Matrix) getFunctionInput(4)).getMatrixObject().release();
} catch (IOException e) {
throw new RuntimeException("Exception while executing SGDNesterovUpdate", e);
}
}
Aggregations