Search in sources :

Example 16 with Connection

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

the class JMLCParfor2ForCompileTest method runJMLCParFor2ForTest.

private void runJMLCParFor2ForTest(boolean par) throws IOException {
    try {
        Connection conn = !par ? new Connection() : new Connection(ConfigType.PARALLEL_LOCAL_OR_REMOTE_PARFOR);
        String script = "  X = rand(rows=10, cols=10);" + "R = matrix(0, rows=10, cols=1)" + "parfor(i in 1:nrow(X))" + "  R[i,] = sum(X[i,])" + "print(sum(R))";
        DMLScript.STATISTICS = true;
        Statistics.reset();
        PreparedScript pscript = conn.prepareScript(script, new String[] {}, new String[] {}, false);
        pscript.executeScript();
        conn.close();
    } catch (Exception ex) {
        Assert.fail("JMLC parfor test failed: " + ex.getMessage());
    }
    // check for existing or non-existing parfor
    Assert.assertTrue(Statistics.getParforOptCount() == (par ? 1 : 0));
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) IOException(java.io.IOException)

Example 17 with Connection

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

the class APICodegenTest method runMLContextParforDatasetTest.

private void runMLContextParforDatasetTest(boolean jmlc) {
    try {
        double[][] X = getRandomMatrix(rows, cols, -10, 10, sparsity, 76543);
        MatrixBlock mX = DataConverter.convertToMatrixBlock(X);
        String s = "X = read(\"/tmp\");" + "R = colSums(X/rowSums(X));" + "write(R, \"tmp2\")";
        // execute scripts
        if (jmlc) {
            DMLScript.STATISTICS = true;
            Connection conn = new Connection(ConfigType.CODEGEN_ENABLED, ConfigType.ALLOW_DYN_RECOMPILATION);
            PreparedScript pscript = conn.prepareScript(s, new String[] { "X" }, new String[] { "R" }, false);
            pscript.setMatrix("X", mX, false);
            pscript.executeScript();
            conn.close();
            System.out.println(Statistics.display());
        } else {
            SparkConf conf = SparkExecutionContext.createSystemMLSparkConf().setAppName("MLContextTest").setMaster("local");
            JavaSparkContext sc = new JavaSparkContext(conf);
            MLContext ml = new MLContext(sc);
            ml.setConfigProperty(DMLConfig.CODEGEN, "true");
            ml.setStatistics(true);
            Script script = dml(s).in("X", mX).out("R");
            ml.execute(script);
            ml.resetConfig();
            sc.stop();
            ml.close();
        }
        // check for generated operator
        Assert.assertTrue(heavyHittersContainsSubString("spoofRA"));
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) DMLScript(org.apache.sysml.api.DMLScript) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) Connection(org.apache.sysml.api.jmlc.Connection) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) SparkConf(org.apache.spark.SparkConf) MLContext(org.apache.sysml.api.mlcontext.MLContext)

Example 18 with Connection

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

the class BuildLiteExecution method jmlcALS.

public static void jmlcALS() throws Exception {
    Connection conn = getConfiguredConnection();
    String dataGen = conn.readScript("scripts/datagen/genRandData4ALS.dml");
    Map<String, String> m = new HashMap<>();
    m.put("$rows", "1000");
    m.put("$cols", "1000");
    m.put("$rank", "100");
    m.put("$nnz", "10000");
    PreparedScript dataGenScript = conn.prepareScript(dataGen, m, new String[] {}, new String[] { "X", "W", "H" }, false);
    ResultVariables dataGenResults = dataGenScript.executeScript();
    double[][] x = dataGenResults.getMatrix("X");
    log.debug(displayMatrix(x));
    Map<String, String> m2 = new HashMap<>();
    m2.put("$rank", "100");
    String alsCg = conn.readScript("scripts/algorithms/ALS-CG.dml");
    PreparedScript alsCgScript = conn.prepareScript(alsCg, m2, new String[] { "X" }, new String[] { "U", "V" }, false);
    alsCgScript.setMatrix("X", x);
    ResultVariables alsCgResults = alsCgScript.executeScript();
    double[][] u = alsCgResults.getMatrix("U");
    log.debug("u:" + u);
    log.debug(displayMatrix(u));
    double[][] v = alsCgResults.getMatrix("V");
    log.debug("v:" + v);
    log.debug(displayMatrix(v));
    String alsDs = conn.readScript("scripts/algorithms/ALS-DS.dml");
    PreparedScript alsDsScript = conn.prepareScript(alsDs, m2, new String[] { "V" }, new String[] { "L", "Rt" }, false);
    alsDsScript.setMatrix("V", x);
    ResultVariables alsDsResults = alsDsScript.executeScript();
    double[][] l = alsDsResults.getMatrix("L");
    log.debug("l:" + l);
    log.debug(displayMatrix(l));
    double[][] rt = alsDsResults.getMatrix("Rt");
    log.debug("rt:" + rt);
    log.debug(displayMatrix(rt));
    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 19 with Connection

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

the class BuildLiteExecution method jmlcWriteMatrix.

public static void jmlcWriteMatrix() throws Exception {
    Connection conn = getConfiguredConnection();
    PreparedScript script = conn.prepareScript("x=matrix('1 2 3 4',rows=2,cols=2);write(x,'" + getRoot() + "x.csv',format='csv');", new String[] {}, new String[] {}, false);
    script.executeScript();
    String scriptString = "m = matrix('1 2 3 0 0 0 7 8 9 0 0 0', rows=4, cols=3)\n" + "write(m, '" + getRoot() + "m.txt', format='text')\n" + "write(m, '" + getRoot() + "m.mm', format='mm')\n" + "write(m, '" + getRoot() + "m.csv', format='csv')\n" + "write(m, '" + getRoot() + "m.binary', format='binary')\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 20 with Connection

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

the class BuildLiteExecution method jmlcLinReg.

public static void jmlcLinReg() throws Exception {
    Connection conn = getConfiguredConnection();
    String linRegDS = conn.readScript("scripts/algorithms/LinearRegDS.dml");
    PreparedScript linRegDSScript = conn.prepareScript(linRegDS, new String[] { "X", "y" }, new String[] { "beta_out" }, false);
    double[][] trainData = new double[500][3];
    for (int i = 0; i < 500; i++) {
        double one = ThreadLocalRandom.current().nextDouble(0, 100);
        double two = ThreadLocalRandom.current().nextDouble(0, 100);
        double three = ThreadLocalRandom.current().nextDouble(0, 100);
        double[] row = new double[] { one, two, three };
        trainData[i] = row;
    }
    linRegDSScript.setMatrix("X", trainData);
    log.debug(displayMatrix(trainData));
    double[][] trainLabels = new double[500][1];
    for (int i = 0; i < 500; i++) {
        double one = ThreadLocalRandom.current().nextDouble(0, 100);
        double[] row = new double[] { one };
        trainLabels[i] = row;
    }
    linRegDSScript.setMatrix("y", trainLabels);
    log.debug(displayMatrix(trainLabels));
    ResultVariables linRegDSResults = linRegDSScript.executeScript();
    double[][] dsBetas = linRegDSResults.getMatrix("beta_out");
    log.debug("DS BETAS:");
    log.debug(displayMatrix(dsBetas));
    String linRegCG = conn.readScript("scripts/algorithms/LinearRegCG.dml");
    PreparedScript linRegCGScript = conn.prepareScript(linRegCG, new String[] { "X", "y" }, new String[] { "beta_out" }, false);
    linRegCGScript.setMatrix("X", trainData);
    linRegCGScript.setMatrix("y", trainLabels);
    ResultVariables linRegCGResults = linRegCGScript.executeScript();
    double[][] cgBetas = linRegCGResults.getMatrix("beta_out");
    log.debug("CG BETAS:");
    log.debug(displayMatrix(cgBetas));
    String glmPredict = conn.readScript("scripts/algorithms/GLM-predict.dml");
    PreparedScript glmPredictScript = conn.prepareScript(glmPredict, new String[] { "X", "Y", "B_full" }, new String[] { "means" }, false);
    double[][] testData = new double[500][3];
    for (int i = 0; i < 500; i++) {
        double one = ThreadLocalRandom.current().nextDouble(0, 100);
        double two = ThreadLocalRandom.current().nextDouble(0, 100);
        double three = ThreadLocalRandom.current().nextDouble(0, 100);
        double[] row = new double[] { one, two, three };
        testData[i] = row;
    }
    glmPredictScript.setMatrix("X", testData);
    double[][] testLabels = new double[500][1];
    for (int i = 0; i < 500; i++) {
        double one = ThreadLocalRandom.current().nextDouble(0, 100);
        double[] row = new double[] { one };
        testLabels[i] = row;
    }
    glmPredictScript.setMatrix("Y", testLabels);
    glmPredictScript.setMatrix("B_full", cgBetas);
    ResultVariables glmPredictResults = glmPredictScript.executeScript();
    double[][] means = glmPredictResults.getMatrix("means");
    log.debug("GLM PREDICT MEANS:");
    log.debug(displayMatrix(means));
    conn.close();
}
Also used : PreparedScript(org.apache.sysml.api.jmlc.PreparedScript) ResultVariables(org.apache.sysml.api.jmlc.ResultVariables) Connection(org.apache.sysml.api.jmlc.Connection)

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