Search in sources :

Example 1 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class FrameReadMetaTest method runJMLCReadMetaTest.

/**
	 * 
	 * @param sparseM1
	 * @param sparseM2
	 * @param instType
	 * @throws IOException 
	 */
private void runJMLCReadMetaTest(String testname, boolean modelReuse, boolean readFrame, boolean useSpec) throws IOException {
    String TEST_NAME = testname;
    TestConfiguration config = getTestConfiguration(TEST_NAME);
    loadTestConfiguration(config);
    //establish connection to SystemML
    Connection conn = new Connection();
    //read meta data frame 
    String spec = MapReduceTool.readStringFromHDFSFile(SCRIPT_DIR + TEST_DIR + "tfmtd_example2/spec.json");
    FrameBlock M = readFrame ? DataConverter.convertToFrameBlock(conn.readStringFrame(SCRIPT_DIR + TEST_DIR + "tfmtd_frame_example/tfmtd_frame")) : conn.readTransformMetaDataFromFile(spec, SCRIPT_DIR + TEST_DIR + "tfmtd_example2/");
    try {
        //generate data based on recode maps
        HashMap<String, Long>[] RC = getRecodeMaps(spec, M);
        double[][] X = generateData(rows, cols, RC);
        String[][] F = null;
        //prepare input arguments
        HashMap<String, String> args = new HashMap<String, String>();
        args.put("$TRANSFORM_SPEC", spec);
        //read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, args, new String[] { "X", "M" }, new String[] { "F" }, false);
        if (modelReuse)
            pstmt.setFrame("M", M, true);
        //execute script multiple times (2 runs)
        for (int i = 0; i < 2; i++) {
            //bind input parameters
            if (!modelReuse)
                pstmt.setFrame("M", M, false);
            pstmt.setMatrix("X", X);
            //execute script
            ResultVariables rs = pstmt.executeScript();
            //get output parameter
            F = rs.getFrame("F");
        }
        //for all generated data, probe recode maps and compare versus output
        for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) if (RC[j] != null) {
            Assert.assertEquals("Wrong result: " + F[i][j] + ".", Double.valueOf(X[i][j]), Double.valueOf(RC[j].get(F[i][j]).toString()));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        IOUtilFunctions.closeSilently(conn);
    }
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) Connection(org.apache.sysml.api.jmlc.Connection) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock)

Example 2 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class FrameTransformTest method execDMLScriptviaJMLC.

/**
	 * 
	 * @param X
	 * @return
	 * @throws DMLException
	 * @throws IOException
	 */
private ArrayList<double[][]> execDMLScriptviaJMLC(String testname, String[][] X, String[][] M, boolean modelReuse) throws IOException {
    Timing time = new Timing(true);
    ArrayList<double[][]> ret = new ArrayList<double[][]>();
    //establish connection to SystemML
    Connection conn = new Connection();
    try {
        //prepare input arguments
        HashMap<String, String> args = new HashMap<String, String>();
        args.put("$TRANSFORM_SPEC", "{ \"ids\": true ,\"recode\": [ 1, 2, 3] }");
        //read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, args, new String[] { "X", "M" }, new String[] { "Y" }, false);
        if (modelReuse)
            pstmt.setFrame("M", M, true);
        //execute script multiple times
        for (int i = 0; i < nRuns; i++) {
            //bind input parameters
            if (!modelReuse)
                pstmt.setFrame("M", M);
            pstmt.setFrame("X", X);
            //execute script
            ResultVariables rs = pstmt.executeScript();
            //get output parameter
            double[][] Y = rs.getMatrix("Y");
            //keep result for comparison
            ret.add(Y);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        IOUtilFunctions.closeSilently(conn);
    }
    System.out.println("JMLC scoring w/ " + nRuns + " runs in " + time.stop() + "ms.");
    return ret;
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) ArrayList(java.util.ArrayList) Connection(org.apache.sysml.api.jmlc.Connection) IOException(java.io.IOException) IOException(java.io.IOException) DMLException(org.apache.sysml.api.DMLException) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Example 3 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class JMLCInputOutputTest method testScalarInputDouble.

@Test
public void testScalarInputDouble() throws IOException, DMLException {
    Connection conn = new Connection();
    String str = conn.readScript(baseDirectory + File.separator + "scalar-input.dml");
    PreparedScript script = conn.prepareScript(str, new String[] { "inScalar1", "inScalar2" }, new String[] {}, false);
    double inScalar1 = 3.5;
    double inScalar2 = 4.5;
    script.setScalar("inScalar1", inScalar1);
    script.setScalar("inScalar2", inScalar2);
    setExpectedStdOut("total:8.0");
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) Test(org.junit.Test)

Example 4 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class JMLCInputOutputTest method testScalarOutputDouble.

@Test
public void testScalarOutputDouble() throws DMLException {
    Connection conn = new Connection();
    String str = "outDouble = 1.23;\nwrite(outDouble, './tmp/outDouble');";
    PreparedScript script = conn.prepareScript(str, new String[] {}, new String[] { "outDouble" }, false);
    double result = script.executeScript().getDouble("outDouble");
    Assert.assertEquals(1.23, result, 0);
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) Test(org.junit.Test)

Example 5 with PreparedScript

use of org.apache.sysml.api.jmlc.PreparedScript in project incubator-systemml by apache.

the class FrameIndexingAppendTest method execDMLScriptviaJMLC.

/**
	 * 
	 * @param X
	 * @return
	 * @throws DMLException
	 * @throws IOException
	 */
private ArrayList<String[][]> execDMLScriptviaJMLC(String testname, String[][] F1, String[][] M, boolean modelReuse) throws IOException {
    Timing time = new Timing(true);
    ArrayList<String[][]> ret = new ArrayList<String[][]>();
    //establish connection to SystemML
    Connection conn = new Connection();
    try {
        //prepare input arguments
        HashMap<String, String> args = new HashMap<String, String>();
        args.put("$TRANSFORM_SPEC1", "{ \"ids\": true ,\"recode\": [ 1, 2] }");
        args.put("$TRANSFORM_SPEC2", "{ \"ids\": true ,\"recode\": [ 1] }");
        //read and precompile script
        String script = conn.readScript(SCRIPT_DIR + TEST_DIR + testname + ".dml");
        PreparedScript pstmt = conn.prepareScript(script, args, new String[] { "F1", "M" }, new String[] { "F2" }, false);
        if (modelReuse)
            pstmt.setFrame("M", M, true);
        //execute script multiple times
        for (int i = 0; i < nRuns; i++) {
            //bind input parameters
            if (!modelReuse)
                pstmt.setFrame("M", M);
            pstmt.setFrame("F1", F1);
            //execute script
            ResultVariables rs = pstmt.executeScript();
            //get output parameter
            String[][] Y = rs.getFrame("F2");
            //keep result for comparison
            ret.add(Y);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IOException(ex);
    } finally {
        IOUtilFunctions.closeSilently(conn);
    }
    System.out.println("JMLC scoring w/ " + nRuns + " runs in " + time.stop() + "ms.");
    return ret;
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) ArrayList(java.util.ArrayList) Connection(org.apache.sysml.api.jmlc.Connection) IOException(java.io.IOException) IOException(java.io.IOException) DMLException(org.apache.sysml.api.DMLException) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Aggregations

Connection (org.apache.sysml.api.jmlc.Connection)19 PreparedScript (org.apache.sysml.api.jmlc.PreparedScript)19 Test (org.junit.Test)10 IOException (java.io.IOException)9 ResultVariables (org.apache.sysml.api.jmlc.ResultVariables)9 ArrayList (java.util.ArrayList)8 DMLException (org.apache.sysml.api.DMLException)8 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)8 HashMap (java.util.HashMap)7 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)1 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)1 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)1 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)1