Search in sources :

Example 1 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.

the class InterProceduralAnalysis method populateLocalVariableMapForFunctionCall.

private void populateLocalVariableMapForFunctionCall(FunctionStatement fstmt, FunctionOp fop, LocalVariableMap callvars, LocalVariableMap vars, Set<Long> inputSafeNNZ, Integer numCalls) throws HopsException {
    ArrayList<DataIdentifier> inputVars = fstmt.getInputParams();
    ArrayList<Hop> inputOps = fop.getInput();
    for (int i = 0; i < inputVars.size(); i++) {
        //create mapping between input hops and vars
        DataIdentifier dat = inputVars.get(i);
        Hop input = inputOps.get(i);
        if (input.getDataType() == DataType.MATRIX) {
            //propagate matrix characteristics
            MatrixObject mo = new MatrixObject(ValueType.DOUBLE, null);
            MatrixCharacteristics mc = new MatrixCharacteristics(input.getDim1(), input.getDim2(), ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize(), inputSafeNNZ.contains(input.getHopID()) ? input.getNnz() : -1);
            MatrixFormatMetaData meta = new MatrixFormatMetaData(mc, null, null);
            mo.setMetaData(meta);
            vars.put(dat.getName(), mo);
        } else if (input.getDataType() == DataType.SCALAR) {
            //(for multiple calls, literal equivalence already checked)
            if (input instanceof LiteralOp) {
                vars.put(dat.getName(), ScalarObjectFactory.createScalarObject(input.getValueType(), (LiteralOp) input));
            } else //and input scalar is existing variable in symbol table
            if (PROPAGATE_SCALAR_VARS_INTO_FUN && numCalls != null && numCalls == 1 && input instanceof DataOp) {
                Data scalar = callvars.get(input.getName());
                if (scalar != null && scalar instanceof ScalarObject) {
                    vars.put(dat.getName(), scalar);
                }
            }
        }
    }
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) DataIdentifier(org.apache.sysml.parser.DataIdentifier) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Hop(org.apache.sysml.hops.Hop) Data(org.apache.sysml.runtime.instructions.cp.Data) MatrixFormatMetaData(org.apache.sysml.runtime.matrix.MatrixFormatMetaData) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) MatrixFormatMetaData(org.apache.sysml.runtime.matrix.MatrixFormatMetaData) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 2 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.

the class ExternalFunctionProgramBlock method verifyAndAttachOutputs.

/**
	 * Method to verify that function outputs match with declared outputs
	 * 
	 * @param ec execution context
	 * @param returnFunc package function
	 * @param outputParams output parameters
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
protected void verifyAndAttachOutputs(ExecutionContext ec, PackageFunction returnFunc, String outputParams) throws DMLRuntimeException {
    ArrayList<String> outputs = getParameters(outputParams);
    if (outputs.size() != returnFunc.getNumFunctionOutputs()) {
        throw new DMLRuntimeException("Number of function outputs (" + returnFunc.getNumFunctionOutputs() + ") " + "does not match with declaration (" + outputs.size() + ").");
    }
    // iterate over each output and verify that type matches
    for (int i = 0; i < outputs.size(); i++) {
        StringTokenizer tk = new StringTokenizer(outputs.get(i), ":");
        ArrayList<String> tokens = new ArrayList<String>();
        while (tk.hasMoreTokens()) {
            tokens.add(tk.nextToken());
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Matrix) {
            Matrix m = (Matrix) returnFunc.getFunctionOutput(i);
            if (!(tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Matrix))) || !(tokens.get(2).equals(getMatrixValueTypeString(m.getValueType())))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // add result to variableMapping
            String varName = tokens.get(1);
            MatrixObject newVar = createOutputMatrixObject(m);
            newVar.setVarName(varName);
            //getVariables().put(varName, newVar); //put/override in local symbol table
            ec.setVariable(varName, newVar);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Scalar) {
            Scalar s = (Scalar) returnFunc.getFunctionOutput(i);
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Scalar)) || !tokens.get(2).equals(getScalarValueTypeString(s.getScalarType()))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // allocate and set appropriate object based on type
            ScalarObject scalarObject = null;
            ScalarValueType type = s.getScalarType();
            switch(type) {
                case Integer:
                    scalarObject = new IntObject(tokens.get(1), Long.parseLong(s.getValue()));
                    break;
                case Double:
                    scalarObject = new DoubleObject(tokens.get(1), Double.parseDouble(s.getValue()));
                    break;
                case Boolean:
                    scalarObject = new BooleanObject(tokens.get(1), Boolean.parseBoolean(s.getValue()));
                    break;
                case Text:
                    scalarObject = new StringObject(tokens.get(1), s.getValue());
                    break;
                default:
                    throw new DMLRuntimeException("Unknown scalar value type '" + type + "' of output '" + outputs.get(i) + "'.");
            }
            //this.getVariables().put(tokens.get(1), scalarObject);
            ec.setVariable(tokens.get(1), scalarObject);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Object) {
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Object))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            throw new DMLRuntimeException("Object types not yet supported");
        // continue;
        }
        throw new DMLRuntimeException("Unknown data type '" + returnFunc.getFunctionOutput(i).getType() + "' " + "of output '" + outputs.get(i) + "'.");
    }
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Scalar(org.apache.sysml.udf.Scalar) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) StringTokenizer(java.util.StringTokenizer) Matrix(org.apache.sysml.udf.Matrix) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ScalarValueType(org.apache.sysml.udf.Scalar.ScalarValueType) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject)

Example 3 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.

the class ProgramConverter method serializeDataObject.

public static String serializeDataObject(String key, Data dat) {
    // SCHEMA: <name>|<datatype>|<valuetype>|value
    // (scalars are serialize by value, matrices by filename)
    StringBuilder sb = new StringBuilder();
    // prepare data for serialization
    String name = key;
    DataType datatype = dat.getDataType();
    ValueType valuetype = dat.getValueType();
    String value = null;
    String[] matrixMetaData = null;
    switch(datatype) {
        case SCALAR:
            ScalarObject so = (ScalarObject) dat;
            // name = so.getName();
            value = so.getStringValue();
            break;
        case MATRIX:
            MatrixObject mo = (MatrixObject) dat;
            MetaDataFormat md = (MetaDataFormat) dat.getMetaData();
            MatrixCharacteristics mc = md.getMatrixCharacteristics();
            value = mo.getFileName();
            PartitionFormat partFormat = (mo.getPartitionFormat() != null) ? new PartitionFormat(mo.getPartitionFormat(), mo.getPartitionSize()) : PartitionFormat.NONE;
            matrixMetaData = new String[9];
            matrixMetaData[0] = String.valueOf(mc.getRows());
            matrixMetaData[1] = String.valueOf(mc.getCols());
            matrixMetaData[2] = String.valueOf(mc.getRowsPerBlock());
            matrixMetaData[3] = String.valueOf(mc.getColsPerBlock());
            matrixMetaData[4] = String.valueOf(mc.getNonZeros());
            matrixMetaData[5] = InputInfo.inputInfoToString(md.getInputInfo());
            matrixMetaData[6] = OutputInfo.outputInfoToString(md.getOutputInfo());
            matrixMetaData[7] = String.valueOf(partFormat);
            matrixMetaData[8] = String.valueOf(mo.getUpdateType());
            break;
        default:
            throw new DMLRuntimeException("Unable to serialize datatype " + datatype);
    }
    // serialize data
    sb.append(name);
    sb.append(DATA_FIELD_DELIM);
    sb.append(datatype);
    sb.append(DATA_FIELD_DELIM);
    sb.append(valuetype);
    sb.append(DATA_FIELD_DELIM);
    sb.append(value);
    if (matrixMetaData != null)
        for (int i = 0; i < matrixMetaData.length; i++) {
            sb.append(DATA_FIELD_DELIM);
            sb.append(matrixMetaData[i]);
        }
    return sb.toString();
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) ValueType(org.apache.sysml.parser.Expression.ValueType) DataType(org.apache.sysml.parser.Expression.DataType) PartitionFormat(org.apache.sysml.runtime.controlprogram.ParForProgramBlock.PartitionFormat) PDataPartitionFormat(org.apache.sysml.runtime.controlprogram.ParForProgramBlock.PDataPartitionFormat) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 4 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.

the class MatrixMatrixAxpyGPUInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    GPUStatistics.incrementNoOfExecutedGPUInst();
    MatrixObject in1 = getMatrixInputForGPUInstruction(ec, _input1.getName());
    MatrixObject in2 = getMatrixInputForGPUInstruction(ec, _input2.getName());
    ScalarObject scalar = ec.getScalarInput(constant.getName(), constant.getValueType(), constant.isLiteral());
    long rlen1 = in1.getNumRows();
    long clen1 = in1.getNumColumns();
    long rlen2 = in2.getNumRows();
    long clen2 = in2.getNumColumns();
    if (isValidMMOperation(rlen1, rlen2, clen1, clen2) || isValidMVOperation(rlen1, rlen2, clen1, clen2)) {
        ec.setMetaData(_output.getName(), (int) rlen1, (int) clen1);
    } else {
        throw new DMLRuntimeException("Incorrect dimensions of inputs in GPU axpy operation. input1:" + rlen1 + " X " + clen1 + " and input2:" + rlen2 + " X " + clen2);
    }
    LibMatrixCUDA.axpy(ec, ec.getGPUContext(0), getExtendedOpcode(), in1, in2, _output.getName(), multiplier * scalar.getDoubleValue());
    ec.releaseMatrixInputForGPUInstruction(_input1.getName());
    ec.releaseMatrixInputForGPUInstruction(_input2.getName());
    ec.releaseMatrixOutputForGPUInstruction(_output.getName());
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 5 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.

the class ScalarMatrixBuiltinGPUInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    GPUStatistics.incrementNoOfExecutedGPUInst();
    String opcode = getOpcode();
    CPOperand mat = (input1.getDataType() == DataType.MATRIX) ? input1 : input2;
    CPOperand scalar = (input1.getDataType() == DataType.MATRIX) ? input2 : input1;
    MatrixObject in1 = getMatrixInputForGPUInstruction(ec, mat.getName());
    ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral());
    if (opcode.equals("max")) {
        ec.setMetaData(output.getName(), in1.getNumRows(), in1.getNumColumns());
        double constVal = constant.getDoubleValue();
        if (constVal == 0)
            LibMatrixCuDNN.relu(ec, ec.getGPUContext(0), getExtendedOpcode(), in1, output.getName());
        else
            LibMatrixCUDA.matrixScalarOp(ec, ec.getGPUContext(0), getExtendedOpcode(), in1, output.getName(), false, InstructionUtils.parseScalarBinaryOperator(opcode, false, constVal));
    } else if (opcode.equals("min")) {
        ec.setMetaData(output.getName(), in1.getNumRows(), in1.getNumColumns());
        double constVal = constant.getDoubleValue();
        LibMatrixCUDA.matrixScalarOp(ec, ec.getGPUContext(0), getExtendedOpcode(), in1, output.getName(), false, InstructionUtils.parseScalarBinaryOperator(opcode, false, constVal));
    } else {
        throw new DMLRuntimeException("Unsupported GPU operator:" + opcode);
    }
    ec.releaseMatrixInputForGPUInstruction(mat.getName());
    ec.releaseMatrixOutputForGPUInstruction(output.getName());
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)42 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)23 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)17 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)14 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)12 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)12 LiteralOp (org.apache.sysml.hops.LiteralOp)11 IntObject (org.apache.sysml.runtime.instructions.cp.IntObject)10 DataOp (org.apache.sysml.hops.DataOp)9 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)9 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)8 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)8 Data (org.apache.sysml.runtime.instructions.cp.Data)7 ArrayList (java.util.ArrayList)6 UnaryOp (org.apache.sysml.hops.UnaryOp)6 BooleanObject (org.apache.sysml.runtime.instructions.cp.BooleanObject)6 StringObject (org.apache.sysml.runtime.instructions.cp.StringObject)6 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)6 ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)6 Hop (org.apache.sysml.hops.Hop)5