Search in sources :

Example 16 with Matrix

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);
    }
}
Also used : OutputInfo(org.apache.sysml.runtime.matrix.data.OutputInfo) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Matrix(org.apache.sysml.udf.Matrix) Scalar(org.apache.sysml.udf.Scalar)

Example 17 with Matrix

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);
    }
}
Also used : OutputInfo(org.apache.sysml.runtime.matrix.data.OutputInfo) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Matrix(org.apache.sysml.udf.Matrix) Scalar(org.apache.sysml.udf.Scalar)

Example 18 with Matrix

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);
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Matrix(org.apache.sysml.udf.Matrix) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) ArrayList(java.util.ArrayList) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) Scalar(org.apache.sysml.udf.Scalar)

Example 19 with Matrix

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();
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) Matrix(org.apache.sysml.udf.Matrix)

Example 20 with Matrix

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);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) Matrix(org.apache.sysml.udf.Matrix) IOException(java.io.IOException)

Aggregations

Matrix (org.apache.sysml.udf.Matrix)33 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)19 Scalar (org.apache.sysml.udf.Scalar)14 IOException (java.io.IOException)8 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)8 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 StringTokenizer (java.util.StringTokenizer)4 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)3 DataOutputStream (java.io.DataOutputStream)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 LongWritable (org.apache.hadoop.io.LongWritable)2 Text (org.apache.hadoop.io.Text)2 InputSplit (org.apache.hadoop.mapred.InputSplit)2 JobConf (org.apache.hadoop.mapred.JobConf)2 TextInputFormat (org.apache.hadoop.mapred.TextInputFormat)2