use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class ResourceOptimizer method getProgramCosts.
private static double getProgramCosts(ProgramBlock pb) {
double val = 0;
if (COST_INDIVIDUAL_BLOCKS) {
LocalVariableMap vars = new LocalVariableMap();
collectReadVariables(pb.getStatementBlock().getHops(), vars);
ExecutionContext ec = ExecutionContextFactory.createContext(false, null);
ec.setVariables(vars);
val = CostEstimationWrapper.getTimeEstimate(pb, ec, false);
} else {
// we need to cost the entire program in order to take in-memory status into account
ExecutionContext ec = ExecutionContextFactory.createContext();
val = CostEstimationWrapper.getTimeEstimate(pb.getProgram(), ec);
}
_cntCostPB++;
return val;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap 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);
}
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project 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;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project systemml by apache.
the class ProgramConverter method parseVariables.
private static LocalVariableMap parseVariables(String in) {
LocalVariableMap ret = null;
if (in.length() > PARFOR_VARS_BEGIN.length() + PARFOR_VARS_END.length()) {
String varStr = in.substring(PARFOR_VARS_BEGIN.length(), in.length() - PARFOR_VARS_END.length()).trim();
ret = LocalVariableMap.deserialize(varStr);
} else // empty input symbol table
{
ret = new LocalVariableMap();
}
return ret;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project systemml by apache.
the class ProgramConverter method parseExecutionContext.
private static ExecutionContext parseExecutionContext(String in, Program prog) {
ExecutionContext ec = null;
String lin = in.substring(PARFOR_EC_BEGIN.length(), in.length() - PARFOR_EC_END.length()).trim();
if (!lin.equals(EMPTY)) {
LocalVariableMap vars = parseVariables(lin);
ec = ExecutionContextFactory.createContext(false, prog);
ec.setVariables(vars);
}
return ec;
}
Aggregations