Search in sources :

Example 6 with WhileStatementBlock

use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.

the class ProgramConverter method createDeepCopyWhileProgramBlock.

public static WhileProgramBlock createDeepCopyWhileProgramBlock(WhileProgramBlock wpb, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) throws DMLRuntimeException {
    ArrayList<Instruction> predinst = createDeepCopyInstructionSet(wpb.getPredicate(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true);
    WhileProgramBlock tmpPB = new WhileProgramBlock(prog, predinst);
    tmpPB.setPredicateResultVar(wpb.getPredicateResultVar());
    tmpPB.setStatementBlock(createWhileStatementBlockCopy((WhileStatementBlock) wpb.getStatementBlock(), pid, plain, forceDeepCopy));
    tmpPB.setThreadID(pid);
    tmpPB.setExitInstructions2(createDeepCopyInstructionSet(wpb.getExitInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
    tmpPB.setChildBlocks(rcreateDeepCopyProgramBlocks(wpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, forceDeepCopy));
    return tmpPB;
}
Also used : WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) ExternalFunctionInvocationInstruction(org.apache.sysml.udf.ExternalFunctionInvocationInstruction) SpoofCPInstruction(org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock)

Example 7 with WhileStatementBlock

use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.

the class ProgramRewriter method rewriteStatementBlockHopDAGs.

public void rewriteStatementBlockHopDAGs(StatementBlock current, ProgramRewriteStatus state) throws LanguageException, HopsException {
    //ensure robustness for calls from outside
    if (state == null)
        state = new ProgramRewriteStatus();
    if (current instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) current;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sb : fstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
    } else if (current instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) current;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wsb.setPredicateHops(rewriteHopDAG(wsb.getPredicateHops(), state));
        for (StatementBlock sb : wstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
    } else if (current instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) current;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        isb.setPredicateHops(rewriteHopDAG(isb.getPredicateHops(), state));
        for (StatementBlock sb : istmt.getIfBody()) rewriteStatementBlockHopDAGs(sb, state);
        for (StatementBlock sb : istmt.getElseBody()) rewriteStatementBlockHopDAGs(sb, state);
    } else if (//incl parfor
    current instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) current;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fsb.setFromHops(rewriteHopDAG(fsb.getFromHops(), state));
        fsb.setToHops(rewriteHopDAG(fsb.getToHops(), state));
        fsb.setIncrementHops(rewriteHopDAG(fsb.getIncrementHops(), state));
        for (StatementBlock sb : fstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
    } else //generic (last-level)
    {
        current.set_hops(rewriteHopDAGs(current.get_hops(), state));
    }
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 8 with WhileStatementBlock

use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.

the class ProgramRewriter method rewriteStatementBlock.

private ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) throws HopsException {
    ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
    ret.add(sb);
    //recursive invocation
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wstmt.setBody(rewriteStatementBlocks(wstmt.getBody(), status));
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        istmt.setIfBody(rewriteStatementBlocks(istmt.getIfBody(), status));
        istmt.setElseBody(rewriteStatementBlocks(istmt.getElseBody(), status));
    } else if (//incl parfor
    sb instanceof ForStatementBlock) {
        //maintain parfor context information (e.g., for checkpointing)
        boolean prestatus = status.isInParforContext();
        if (sb instanceof ParForStatementBlock)
            status.setInParforContext(true);
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
        status.setInParforContext(prestatus);
    }
    //apply rewrite rules
    for (StatementBlockRewriteRule r : _sbRuleSet) {
        ArrayList<StatementBlock> tmp = new ArrayList<StatementBlock>();
        for (StatementBlock sbc : ret) tmp.addAll(r.rewriteStatementBlock(sbc, status));
        //take over set of rewritten sbs		
        ret.clear();
        ret.addAll(tmp);
    }
    return ret;
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ArrayList(java.util.ArrayList) WhileStatement(org.apache.sysml.parser.WhileStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatement(org.apache.sysml.parser.ForStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 9 with WhileStatementBlock

use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.

the class WhileProgramBlock method executePredicate.

private BooleanObject executePredicate(ExecutionContext ec) throws DMLRuntimeException {
    BooleanObject result = null;
    try {
        if (_predicate != null && !_predicate.isEmpty()) {
            if (_sb != null) {
                if (//set program block specific remote memory
                DMLScript.isActiveAM())
                    DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
                WhileStatementBlock wsb = (WhileStatementBlock) _sb;
                Hop predicateOp = wsb.getPredicateHops();
                boolean recompile = wsb.requiresPredicateRecompilation();
                result = (BooleanObject) executePredicate(_predicate, predicateOp, recompile, ValueType.BOOLEAN, ec);
            } else
                result = (BooleanObject) executePredicate(_predicate, null, false, ValueType.BOOLEAN, ec);
        } else {
            //get result var
            ScalarObject scalarResult = null;
            Data resultData = ec.getVariable(_predicateResultVar);
            if (resultData == null) {
                //note: resultvar is a literal (can it be of any value type other than String, hence no literal/varname conflict) 
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, true);
            } else {
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, false);
            }
            //check for invalid type String 
            if (scalarResult instanceof StringObject)
                throw new DMLRuntimeException(this.printBlockErrorLocation() + "\nWhile predicate variable " + _predicateResultVar + " evaluated to string " + scalarResult + " which is not allowed for predicates in DML");
            //process result
            if (scalarResult instanceof BooleanObject)
                result = (BooleanObject) scalarResult;
            else
                //auto casting
                result = new BooleanObject(scalarResult.getBooleanValue());
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Failed to evaluate the while predicate.", ex);
    }
    //(guaranteed to be non-null, see executePredicate/getScalarInput)
    return result;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) Hop(org.apache.sysml.hops.Hop) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) Data(org.apache.sysml.runtime.instructions.cp.Data) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 10 with WhileStatementBlock

use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.

the class ResourceConfig method addProgramBlock.

private void addProgramBlock(ProgramBlock pb, long init) throws HopsException {
    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

WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)30 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)27 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)26 StatementBlock (org.apache.sysml.parser.StatementBlock)26 ForStatement (org.apache.sysml.parser.ForStatement)14 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)13 IfStatement (org.apache.sysml.parser.IfStatement)13 WhileStatement (org.apache.sysml.parser.WhileStatement)13 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)13 ArrayList (java.util.ArrayList)12 Hop (org.apache.sysml.hops.Hop)12 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)12 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)12 FunctionStatement (org.apache.sysml.parser.FunctionStatement)8 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)8 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)7 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)5 Instruction (org.apache.sysml.runtime.instructions.Instruction)5 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)4 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)4