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