Search in sources :

Example 1 with DMLException

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;
}
Also used : DMLException(org.apache.sysml.api.DMLException) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) Data(org.apache.sysml.runtime.instructions.cp.Data) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject)

Example 2 with DMLException

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);
}
Also used : ParserFactory(org.apache.sysml.parser.ParserFactory) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) Arrays(java.util.Arrays) DMLTranslator(org.apache.sysml.parser.DMLTranslator) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite) FileSystem(org.apache.hadoop.fs.FileSystem) SpoofCompiler(org.apache.sysml.hops.codegen.SpoofCompiler) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) FrameReader(org.apache.sysml.runtime.io.FrameReader) HashMap(java.util.HashMap) DataConverter(org.apache.sysml.runtime.util.DataConverter) CompilerConfig(org.apache.sysml.conf.CompilerConfig) DMLException(org.apache.sysml.api.DMLException) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) TfMetaUtils(org.apache.sysml.runtime.transform.meta.TfMetaUtils) TfUtils(org.apache.sysml.runtime.transform.TfUtils) MatrixReader(org.apache.sysml.runtime.io.MatrixReader) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Map(java.util.Map) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) Path(org.apache.hadoop.fs.Path) DMLProgram(org.apache.sysml.parser.DMLProgram) DMLConfig(org.apache.sysml.conf.DMLConfig) Program(org.apache.sysml.runtime.controlprogram.Program) LanguageException(org.apache.sysml.parser.LanguageException) ParserWrapper(org.apache.sysml.parser.ParserWrapper) RUNTIME_PLATFORM(org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM) ConfigurationManager(org.apache.sysml.conf.ConfigurationManager) MatrixReaderFactory(org.apache.sysml.runtime.io.MatrixReaderFactory) FrameReaderFactory(org.apache.sysml.runtime.io.FrameReaderFactory) IOException(java.io.IOException) ScriptType(org.apache.sysml.api.mlcontext.ScriptType) IOUtilFunctions(org.apache.sysml.runtime.io.IOUtilFunctions) InputStreamReader(java.io.InputStreamReader) ConfigType(org.apache.sysml.conf.CompilerConfig.ConfigType) Closeable(java.io.Closeable) JSONObject(org.apache.wink.json4j.JSONObject) DMLScript(org.apache.sysml.api.DMLScript) ParseException(org.apache.sysml.parser.ParseException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) UtilFunctions(org.apache.sysml.runtime.util.UtilFunctions) InputStream(java.io.InputStream) DataExpression(org.apache.sysml.parser.DataExpression) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) DMLException(org.apache.sysml.api.DMLException) DMLTranslator(org.apache.sysml.parser.DMLTranslator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Example 3 with DMLException

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);
    }
}
Also used : MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLException(org.apache.sysml.api.DMLException) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 4 with DMLException

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);
    }
}
Also used : MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) DMLException(org.apache.sysml.api.DMLException) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 5 with DMLException

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;
}
Also used : DMLException(org.apache.sysml.api.DMLException) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) Data(org.apache.sysml.runtime.instructions.cp.Data) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject)

Aggregations

DMLException (org.apache.sysml.api.DMLException)13 Data (org.apache.sysml.runtime.instructions.cp.Data)6 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)5 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)5 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)4 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)4 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)4 IOException (java.io.IOException)3 BufferedReader (java.io.BufferedReader)2 Closeable (java.io.Closeable)2 FileReader (java.io.FileReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 DMLScript (org.apache.sysml.api.DMLScript)2