use of org.apache.sysml.api.DMLException in project incubator-systemml by apache.
the class ResultVariables method getFrame.
/**
* Obtain the frame represented by the given output variable.
*
* @param varname output variable name
* @return frame as a two-dimensional string array
* @throws DMLException if DMLException occurs
*/
public String[][] getFrame(String varname) throws DMLException {
if (!_out.containsKey(varname))
throw new DMLException("Non-existent output variable: " + varname);
Data dat = _out.get(varname);
//basic checks for data type
if (!(dat instanceof FrameObject))
throw new DMLException("Expected frame result '" + varname + "' not a frame.");
//convert output matrix to double array
FrameObject fo = (FrameObject) dat;
FrameBlock frame = fo.acquireRead();
String[][] ret = DataConverter.convertToStringFrame(frame);
fo.release();
return ret;
}
use of org.apache.sysml.api.DMLException in project incubator-systemml by apache.
the class Connection method prepareScript.
/**
* Prepares (precompiles) a script, sets input parameter values, and registers input and output variables.
*
* @param script string representing the DML or PyDML script
* @param args map of input parameters ($) and their values
* @param inputs string array of input variables to register
* @param outputs string array of output variables to register
* @param parsePyDML {@code true} if PyDML, {@code false} if DML
* @return PreparedScript object representing the precompiled script
*/
public PreparedScript prepareScript(String script, Map<String, String> args, String[] inputs, String[] outputs, boolean parsePyDML) {
DMLScript.SCRIPT_TYPE = parsePyDML ? ScriptType.PYDML : ScriptType.DML;
// check for valid names of passed arguments
String[] invalidArgs = args.keySet().stream().filter(k -> k == null || !k.startsWith("$")).toArray(String[]::new);
if (invalidArgs.length > 0)
throw new LanguageException("Invalid argument names: " + Arrays.toString(invalidArgs));
// check for valid names of input and output variables
String[] invalidVars = UtilFunctions.asSet(inputs, outputs).stream().filter(k -> k == null || k.startsWith("$")).toArray(String[]::new);
if (invalidVars.length > 0)
throw new LanguageException("Invalid variable names: " + Arrays.toString(invalidVars));
setLocalConfigs();
// simplified compilation chain
Program rtprog = null;
try {
// parsing
ParserWrapper parser = ParserFactory.createParser(parsePyDML ? ScriptType.PYDML : ScriptType.DML);
DMLProgram prog = parser.parse(null, script, args);
// language validate
DMLTranslator dmlt = new DMLTranslator(prog);
dmlt.liveVariableAnalysis(prog);
dmlt.validateParseTree(prog);
// hop construct/rewrite
dmlt.constructHops(prog);
dmlt.rewriteHopsDAG(prog);
// rewrite persistent reads/writes
RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs);
ProgramRewriter rewriter2 = new ProgramRewriter(rewrite);
rewriter2.rewriteProgramHopDAGs(prog);
// lop construct and runtime prog generation
dmlt.constructLops(prog);
rtprog = dmlt.getRuntimeProgram(prog, _dmlconf);
// final cleanup runtime prog
JMLCUtils.cleanupRuntimeProgram(rtprog, outputs);
} catch (ParseException pe) {
// don't chain ParseException (for cleaner error output)
throw pe;
} catch (Exception ex) {
throw new DMLException(ex);
}
// return newly create precompiled script
return new PreparedScript(rtprog, inputs, outputs, _dmlconf, _cconf);
}
use of org.apache.sysml.api.DMLException in project incubator-systemml by apache.
the class PreparedScript method setMatrix.
/**
* Binds a matrix object to a registered input variable.
* If reuse requested, then the input is guaranteed to be
* preserved over multiple <code>executeScript</code> calls.
*
* @param varname input variable name
* @param matrix matrix represented as a MatrixBlock
* @param reuse if {@code true}, preserve value over multiple {@code executeScript} calls
*/
public void setMatrix(String varname, MatrixBlock matrix, boolean reuse) {
if (!_inVarnames.contains(varname))
throw new DMLException("Unspecified input variable: " + varname);
int blocksize = ConfigurationManager.getBlocksize();
// create new matrix object
MatrixCharacteristics mc = new MatrixCharacteristics(matrix.getNumRows(), matrix.getNumColumns(), blocksize, blocksize);
MetaDataFormat meta = new MetaDataFormat(mc, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
MatrixObject mo = new MatrixObject(ValueType.DOUBLE, OptimizerUtils.getUniqueTempFileName(), meta);
mo.acquireModify(matrix);
mo.release();
// put create matrix wrapper into symbol table
_vars.put(varname, mo);
if (reuse) {
// prevent cleanup
mo.enableCleanup(false);
_inVarReuse.put(varname, mo);
}
}
use of org.apache.sysml.api.DMLException in project incubator-systemml by apache.
the class PreparedScript method setFrame.
/**
* Binds a frame object to a registered input variable.
* If reuse requested, then the input is guaranteed to be
* preserved over multiple <code>executeScript</code> calls.
*
* @param varname input variable name
* @param frame frame represented as a FrameBlock
* @param reuse if {@code true}, preserve value over multiple {@code executeScript} calls
*/
public void setFrame(String varname, FrameBlock frame, boolean reuse) {
if (!_inVarnames.contains(varname))
throw new DMLException("Unspecified input variable: " + varname);
// create new frame object
MatrixCharacteristics mc = new MatrixCharacteristics(frame.getNumRows(), frame.getNumColumns(), -1, -1);
MetaDataFormat meta = new MetaDataFormat(mc, OutputInfo.BinaryCellOutputInfo, InputInfo.BinaryCellInputInfo);
FrameObject fo = new FrameObject(OptimizerUtils.getUniqueTempFileName(), meta);
fo.acquireModify(frame);
fo.release();
// put create matrix wrapper into symbol table
_vars.put(varname, fo);
if (reuse) {
// prevent cleanup
fo.enableCleanup(false);
_inVarReuse.put(varname, fo);
}
}
use of org.apache.sysml.api.DMLException in project incubator-systemml by apache.
the class ResultVariables method getFrameBlock.
/**
* Obtain the frame represented by the given output variable.
* Calling this method avoids unnecessary output conversions.
*
* @param varname output variable name
* @return frame as a frame block
*/
public FrameBlock getFrameBlock(String varname) {
Data dat = _out.get(varname);
if (dat == null)
throw new DMLException("Non-existent output variable: " + varname);
// basic checks for data type
if (!(dat instanceof FrameObject))
throw new DMLException("Expected frame result '" + varname + "' not a frame.");
// convert output matrix to double array
FrameObject fo = (FrameObject) dat;
FrameBlock fb = fo.acquireRead();
fo.release();
return fb;
}
Aggregations