Search in sources :

Example 56 with FunctionProgramBlock

use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.

the class GridEnumerationMemory method getMemoryEstimates.

private void getMemoryEstimates(ProgramBlock pb, ArrayList<Long> mem) {
    if (pb instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        getMemoryEstimates(fpb.getChildBlocks(), mem);
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock fpb = (WhileProgramBlock) pb;
        getMemoryEstimates(fpb.getChildBlocks(), mem);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock fpb = (IfProgramBlock) pb;
        getMemoryEstimates(fpb.getChildBlocksIfBody(), mem);
        getMemoryEstimates(fpb.getChildBlocksElseBody(), mem);
    } else if (// incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        getMemoryEstimates(fpb.getChildBlocks(), mem);
    } else {
        StatementBlock sb = pb.getStatementBlock();
        if (sb != null && sb.getHops() != null) {
            Hop.resetVisitStatus(sb.getHops());
            for (Hop hop : sb.getHops()) getMemoryEstimates(hop, mem);
        }
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) Hop(org.apache.sysml.hops.Hop) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) StatementBlock(org.apache.sysml.parser.StatementBlock)

Example 57 with FunctionProgramBlock

use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.

the class ResourceOptimizer method compileProgram.

private static ArrayList<ProgramBlock> compileProgram(ProgramBlock pb, ArrayList<ProgramBlock> B, double cp, double mr) {
    if (pb instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        compileProgram(fpb.getChildBlocks(), B, cp, mr);
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
            ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
            wpb.setPredicate(inst);
            B.add(wpb);
            _cntCompilePB++;
        }
        compileProgram(wpb.getChildBlocks(), B, cp, mr);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock sb = (IfStatementBlock) ipb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
            ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
            ipb.setPredicate(inst);
            B.add(ipb);
            _cntCompilePB++;
        }
        compileProgram(ipb.getChildBlocksIfBody(), B, cp, mr);
        compileProgram(ipb.getChildBlocksElseBody(), B, cp, mr);
    } else if (// incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock sb = (ForStatementBlock) fpb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null) {
            if (sb.getFromHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getFromHops(), new LocalVariableMap(), null, false, false, 0);
                fpb.setFromInstructions(inst);
            }
            if (sb.getToHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getToHops(), new LocalVariableMap(), null, false, false, 0);
                fpb.setToInstructions(inst);
            }
            if (sb.getIncrementHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getIncrementHops(), new LocalVariableMap(), null, false, false, 0);
                fpb.setIncrementInstructions(inst);
            }
            B.add(fpb);
            _cntCompilePB++;
        }
        compileProgram(fpb.getChildBlocks(), B, cp, mr);
    } else {
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb, sb.getHops(), new LocalVariableMap(), null, false, false, 0);
        pb.setInstructions(inst);
        B.add(pb);
        _cntCompilePB++;
    }
    return B;
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ArrayList(java.util.ArrayList) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 58 with FunctionProgramBlock

use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project systemml by apache.

the class OptTreeConverter method rContainsMRJobInstruction.

public static boolean rContainsMRJobInstruction(ProgramBlock pb, boolean inclFunctions) {
    boolean ret = false;
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock tmp = (WhileProgramBlock) pb;
        ret = containsMRJobInstruction(tmp.getPredicate(), true, true);
        if (ret)
            return ret;
        for (ProgramBlock pb2 : tmp.getChildBlocks()) {
            ret = rContainsMRJobInstruction(pb2, inclFunctions);
            if (ret)
                return ret;
        }
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock tmp = (IfProgramBlock) pb;
        ret = containsMRJobInstruction(tmp.getPredicate(), true, true);
        if (ret)
            return ret;
        for (ProgramBlock pb2 : tmp.getChildBlocksIfBody()) {
            ret = rContainsMRJobInstruction(pb2, inclFunctions);
            if (ret)
                return ret;
        }
        for (ProgramBlock pb2 : tmp.getChildBlocksElseBody()) {
            ret = rContainsMRJobInstruction(pb2, inclFunctions);
            if (ret)
                return ret;
        }
    } else if (// includes ParFORProgramBlock
    pb instanceof ForProgramBlock) {
        ForProgramBlock tmp = (ForProgramBlock) pb;
        ret = containsMRJobInstruction(tmp.getFromInstructions(), true, true);
        ret |= containsMRJobInstruction(tmp.getToInstructions(), true, true);
        ret |= containsMRJobInstruction(tmp.getIncrementInstructions(), true, true);
        if (ret)
            return ret;
        for (ProgramBlock pb2 : tmp.getChildBlocks()) {
            ret = rContainsMRJobInstruction(pb2, inclFunctions);
            if (ret)
                return ret;
        }
    } else if (// includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP)
    pb instanceof FunctionProgramBlock) {
    // do nothing
    } else {
        ret = containsMRJobInstruction(pb, true, true) || (inclFunctions && containsFunctionCallInstruction(pb));
    }
    return ret;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock)

Example 59 with FunctionProgramBlock

use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project systemml by apache.

the class FunctionCallCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Executing instruction : " + this.toString());
    }
    // get the function program block (stored in the Program object)
    FunctionProgramBlock fpb = ec.getProgram().getFunctionProgramBlock(_namespace, _functionName);
    // sanity check number of function parameters
    if (_boundInputs.length < fpb.getInputParams().size()) {
        throw new DMLRuntimeException("Number of bound input parameters does not match the function signature " + "(" + _boundInputs.length + ", but " + fpb.getInputParams().size() + " expected)");
    }
    // create bindings to formal parameters for given function call
    // These are the bindings passed to the FunctionProgramBlock for function execution
    LocalVariableMap functionVariables = new LocalVariableMap();
    for (int i = 0; i < fpb.getInputParams().size(); i++) {
        // error handling non-existing variables
        CPOperand input = _boundInputs[i];
        if (!input.isLiteral() && !ec.containsVariable(input.getName())) {
            throw new DMLRuntimeException("Input variable '" + input.getName() + "' not existing on call of " + DMLProgram.constructFunctionKey(_namespace, _functionName) + " (line " + getLineNum() + ").");
        }
        // get input matrix/frame/scalar
        DataIdentifier currFormalParam = fpb.getInputParams().get(i);
        Data value = ec.getVariable(input);
        // graceful value type conversion for scalar inputs with wrong type
        if (value.getDataType() == DataType.SCALAR && value.getValueType() != currFormalParam.getValueType()) {
            value = ScalarObjectFactory.createScalarObject(currFormalParam.getValueType(), (ScalarObject) value);
        }
        // set input parameter
        functionVariables.put(currFormalParam.getName(), value);
    }
    // Pin the input variables so that they do not get deleted
    // from pb's symbol table at the end of execution of function
    boolean[] pinStatus = ec.pinVariables(_boundInputNames);
    // Create a symbol table under a new execution context for the function invocation,
    // and copy the function arguments into the created table.
    ExecutionContext fn_ec = ExecutionContextFactory.createContext(false, ec.getProgram());
    if (DMLScript.USE_ACCELERATOR) {
        fn_ec.setGPUContexts(ec.getGPUContexts());
        fn_ec.getGPUContext(0).initializeThread();
    }
    fn_ec.setVariables(functionVariables);
    // execute the function block
    try {
        fpb._functionName = this._functionName;
        fpb._namespace = this._namespace;
        fpb.execute(fn_ec);
    } catch (DMLScriptException e) {
        throw e;
    } catch (Exception e) {
        String fname = DMLProgram.constructFunctionKey(_namespace, _functionName);
        throw new DMLRuntimeException("error executing function " + fname, e);
    }
    // cleanup all returned variables w/o binding
    HashSet<String> expectRetVars = new HashSet<>();
    for (DataIdentifier di : fpb.getOutputParams()) expectRetVars.add(di.getName());
    LocalVariableMap retVars = fn_ec.getVariables();
    for (Entry<String, Data> var : retVars.entrySet()) {
        if (expectRetVars.contains(var.getKey()))
            continue;
        // cleanup unexpected return values to avoid leaks
        if (var.getValue() instanceof CacheableData)
            fn_ec.cleanupCacheableData((CacheableData<?>) var.getValue());
    }
    // Unpin the pinned variables
    ec.unpinVariables(_boundInputNames, pinStatus);
    // add the updated binding for each return variable to the variables in original symbol table
    for (int i = 0; i < fpb.getOutputParams().size(); i++) {
        String boundVarName = _boundOutputNames.get(i);
        Data boundValue = retVars.get(fpb.getOutputParams().get(i).getName());
        if (boundValue == null)
            throw new DMLRuntimeException(boundVarName + " was not assigned a return value");
        // cleanup existing data bound to output variable name
        Data exdata = ec.removeVariable(boundVarName);
        if (exdata != null && exdata instanceof CacheableData && exdata != boundValue) {
            ec.cleanupCacheableData((CacheableData<?>) exdata);
        }
        // add/replace data in symbol table
        ec.setVariable(boundVarName, boundValue);
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) HashSet(java.util.HashSet)

Example 60 with FunctionProgramBlock

use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project systemml by apache.

the class ResourceConfig method addProgramBlock.

private void addProgramBlock(ProgramBlock pb, long init) {
    if (pb instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock fpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && wsb != null && wsb.getPredicateHops() != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock fpb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && isb != null && isb.getPredicateHops() != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocksIfBody(), init);
        addProgramBlocks(fpb.getChildBlocksElseBody(), init);
    } else if (// incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && fsb != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else {
        // for objects hash is unique because memory location used
        _mrres.add(init);
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Aggregations

FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)68 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)54 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)52 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)52 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)46 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)34 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)33 ArrayList (java.util.ArrayList)22 StatementBlock (org.apache.sysml.parser.StatementBlock)20 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)18 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)18 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)18 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)16 Instruction (org.apache.sysml.runtime.instructions.Instruction)15 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)12 DMLProgram (org.apache.sysml.parser.DMLProgram)11 Program (org.apache.sysml.runtime.controlprogram.Program)11 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)10 HashSet (java.util.HashSet)9 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)9