Search in sources :

Example 21 with Connection

use of org.apache.sysml.api.jmlc.Connection in project systemml by apache.

the class BuildLiteExecution method jmlcReadMatrix.

public static void jmlcReadMatrix() throws Exception {
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript("x=read('" + getRoot() + "x.csv',format='csv');y=x*2;print(toString(y));", new String[] {}, new String[] {}, false);
    script.executeScript();
    String scriptString = "m = read('" + getRoot() + "m.csv',format='csv')\n" + "print(toString(m))\n" + "print('min:' + min(m))\n" + "print('max:' + max(m))\n" + "print('sum:' + sum(m))\n" + "mRowSums = rowSums(m)\n" + "for (i in 1:nrow(mRowSums)) {\n" + "    print('row ' + i + ' sum:' + as.scalar(mRowSums[i,1]))\n" + "}\n" + "mColSums = colSums(m)\n" + "for (i in 1:ncol(mColSums)) {\n" + "    print('col ' + i + ' sum:' + as.scalar(mColSums[1,i]))\n" + "}\n";
    script = conn.prepareScript(scriptString, new String[] {}, new String[] {}, false);
    script.executeScript();
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection)

Example 22 with Connection

use of org.apache.sysml.api.jmlc.Connection in project systemml by apache.

the class BuildLiteExecution method jmlcKmeans.

public static void jmlcKmeans() throws Exception {
    Connection conn = getConfiguredConnection();
    Map<String, String> m = new HashMap<>();
    m.put("$k", "5");
    m.put("$isY", "TRUE");
    m.put("$verb", "TRUE");
    String kMeans = conn.readScript("scripts/algorithms/Kmeans.dml");
    PreparedScript kMeansScript = conn.prepareScript(kMeans, m, new String[] { "X" }, new String[] { "C", "Y" }, false);
    double[][] x = randomMatrix(50, 50, -1, 1, 0.1);
    kMeansScript.setMatrix("X", x);
    log.debug("X:");
    log.debug(displayMatrix(x));
    ResultVariables kMeansResults = kMeansScript.executeScript();
    double[][] c = kMeansResults.getMatrix("C");
    log.debug("C:");
    log.debug(displayMatrix(c));
    double[][] y = kMeansResults.getMatrix("Y");
    log.debug("Y:");
    log.debug(displayMatrix(y));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) HashMap(java.util.HashMap) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) Connection(org.apache.sysml.api.jmlc.Connection)

Example 23 with Connection

use of org.apache.sysml.api.jmlc.Connection in project systemml by apache.

the class FrameLeftIndexingTest method execDMLScriptviaJMLC.

private static 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) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Example 24 with Connection

use of org.apache.sysml.api.jmlc.Connection in project systemml by apache.

the class FrameReadMetaTest method runJMLCReadMetaTest.

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) IOException(java.io.IOException) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock)

Example 25 with Connection

use of org.apache.sysml.api.jmlc.Connection in project systemml by apache.

the class InputToStringTest method testScalartoString.

@Test
public void testScalartoString() throws DMLException {
    try (Connection conn = new Connection()) {
        PreparedScript pscript = conn.prepareScript("s = read(\"tmp\", data_type=\"scalar\"); print(toString(s));", new String[] { "s" }, new String[] {});
        pscript.setScalar("s", 7);
        pscript.executeScript();
    }
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) Test(org.junit.Test)

Aggregations

Connection (org.apache.sysml.api.jmlc.Connection)75 PreparedScript (org.apache.sysml.api.jmlc.PreparedScript)73 Test (org.junit.Test)28 ResultVariables (org.apache.sysml.api.jmlc.ResultVariables)27 IOException (java.io.IOException)25 HashMap (java.util.HashMap)22 ArrayList (java.util.ArrayList)19 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)17 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)4 DMLException (org.apache.sysml.api.DMLException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 SparkConf (org.apache.spark.SparkConf)2 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)2 DMLScript (org.apache.sysml.api.DMLScript)2 MLContext (org.apache.sysml.api.mlcontext.MLContext)2