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();
}
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();
}
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;
}
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);
}
}
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();
}
}
Aggregations