Search in sources :

Example 26 with MLContext

use of org.apache.sysml.api.mlcontext.MLContext in project systemml by apache.

the class GPUTests method runOnCPU.

/**
 * Runs a program on the CPU
 *
 * @param spark     a valid {@link SparkSession}
 * @param scriptStr the script to run (as a string)
 * @param inputs    map of input variables names in the scriptStr (of variable_name -> object)
 * @param outStrs   list of variable names needed as output from the scriptStr
 * @return list of output objects in order of outStrs
 */
protected List<Object> runOnCPU(SparkSession spark, String scriptStr, Map<String, Object> inputs, List<String> outStrs) {
    MLContext cpuMLC = new MLContext(spark);
    List<Object> outputs = new ArrayList<>();
    Script script = ScriptFactory.dmlFromString(scriptStr).in(inputs).out(outStrs);
    MLResults res = cpuMLC.execute(script);
    for (String outStr : outStrs) {
        Object output = res.get(outStr);
        outputs.add(output);
    }
    cpuMLC.close();
    return outputs;
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) MLResults(org.apache.sysml.api.mlcontext.MLResults) ArrayList(java.util.ArrayList) MLContext(org.apache.sysml.api.mlcontext.MLContext)

Example 27 with MLContext

use of org.apache.sysml.api.mlcontext.MLContext in project systemml by apache.

the class GPUTests method generateInputMatrix.

/**
 * Generates a random input matrix with a given size and sparsity
 *
 * @param spark    valid instance of {@link SparkSession}
 * @param m        number of rows
 * @param n        number of columns
 * @param sparsity sparsity (1 = completely dense, 0 = completely sparse)
 * @return a random matrix with given size and sparsity
 */
protected Matrix generateInputMatrix(SparkSession spark, int m, int n, double sparsity, int seed) {
    // Generate a random matrix of size m * n
    MLContext genMLC = new MLContext(spark);
    String scriptStr;
    if (sparsity == 0.0) {
        scriptStr = "in1 = matrix(0, rows=" + m + ", cols=" + n + ")";
    } else {
        scriptStr = "in1 = rand(rows=" + m + ", cols=" + n + ", sparsity = " + sparsity + ", seed= " + seed + ", min=-1.0, max=1.0)";
    }
    Script generateScript = ScriptFactory.dmlFromString(scriptStr).out("in1");
    Matrix in1 = genMLC.execute(generateScript).getMatrix("in1");
    genMLC.close();
    return in1;
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) Matrix(org.apache.sysml.api.mlcontext.Matrix) MLContext(org.apache.sysml.api.mlcontext.MLContext)

Example 28 with MLContext

use of org.apache.sysml.api.mlcontext.MLContext in project systemml by apache.

the class GPUTests method generateInputMatrix.

/**
 * Generates a random input matrix with a given size and sparsity
 *
 * @param spark    valid instance of {@link SparkSession}
 * @param m        number of rows
 * @param n        number of columns
 * @param min      min for RNG
 * @param max      max for RNG
 * @param sparsity sparsity (1 = completely dense, 0 = completely sparse)
 * @param performRounding performs rounding after generation of random matrix
 * @return a random matrix with given size and sparsity
 */
protected Matrix generateInputMatrix(SparkSession spark, int m, int n, double min, double max, double sparsity, int seed, boolean performRounding) {
    // Generate a random matrix of size m * n
    MLContext genMLC = new MLContext(spark);
    String scriptStr;
    if (sparsity == 0.0) {
        scriptStr = "in1 = matrix(0, rows=" + m + ", cols=" + n + ")";
    } else {
        scriptStr = "in1 = rand(rows=" + m + ", cols=" + n + ", sparsity = " + sparsity + ", seed= " + seed + ", min=" + min + ", max=" + max + ")";
        if (performRounding)
            scriptStr += "; in1 = round(in1)";
    }
    Script generateScript = ScriptFactory.dmlFromString(scriptStr).out("in1");
    Matrix in1 = genMLC.execute(generateScript).getMatrix("in1");
    genMLC.close();
    return in1;
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) Matrix(org.apache.sysml.api.mlcontext.Matrix) MLContext(org.apache.sysml.api.mlcontext.MLContext)

Example 29 with MLContext

use of org.apache.sysml.api.mlcontext.MLContext in project systemml by apache.

the class MLContextScratchCleanupTest method runMLContextTestMultipleScript.

private static void runMLContextTestMultipleScript(RUNTIME_PLATFORM platform, boolean wRead) {
    RUNTIME_PLATFORM oldplatform = DMLScript.rtplatform;
    DMLScript.rtplatform = platform;
    // create mlcontext
    SparkSession spark = createSystemMLSparkSession("MLContextScratchCleanupTest", "local");
    MLContext ml = new MLContext(spark);
    ml.setExplain(true);
    String dml1 = baseDirectory + File.separator + "ScratchCleanup1.dml";
    String dml2 = baseDirectory + File.separator + (wRead ? "ScratchCleanup2b.dml" : "ScratchCleanup2.dml");
    try {
        Script script1 = dmlFromFile(dml1).in("$rows", rows).in("$cols", cols).out("X");
        Matrix X = ml.execute(script1).getMatrix("X");
        // clear in-memory/cached data to emulate on-disk storage
        X.toMatrixObject().clearData();
        Script script2 = dmlFromFile(dml2).in("X", X).out("z");
        String z = ml.execute(script2).getString("z");
        System.out.println(z);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        DMLScript.rtplatform = oldplatform;
        // stop underlying spark context to allow single jvm tests (otherwise the
        // next test that tries to create a SparkContext would fail)
        spark.stop();
        // clear status mlcontext and spark exec context
        ml.close();
    }
}
Also used : RUNTIME_PLATFORM(org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM) Script(org.apache.sysml.api.mlcontext.Script) DMLScript(org.apache.sysml.api.DMLScript) SparkSession(org.apache.spark.sql.SparkSession) Matrix(org.apache.sysml.api.mlcontext.Matrix) MLContext(org.apache.sysml.api.mlcontext.MLContext)

Example 30 with MLContext

use of org.apache.sysml.api.mlcontext.MLContext in project systemml by apache.

the class MLContextTestBase method setUpClass.

@BeforeClass
public static void setUpClass() {
    spark = createSystemMLSparkSession("SystemML MLContext Test", "local");
    ml = new MLContext(spark);
    sc = MLContextUtil.getJavaSparkContext(ml);
}
Also used : MLContext(org.apache.sysml.api.mlcontext.MLContext) BeforeClass(org.junit.BeforeClass)

Aggregations

MLContext (org.apache.sysml.api.mlcontext.MLContext)30 Script (org.apache.sysml.api.mlcontext.Script)18 Matrix (org.apache.sysml.api.mlcontext.Matrix)10 BeforeClass (org.junit.BeforeClass)8 DMLScript (org.apache.sysml.api.DMLScript)6 ArrayList (java.util.ArrayList)4 SparkConf (org.apache.spark.SparkConf)4 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)4 SparkSession (org.apache.spark.sql.SparkSession)4 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)4 MLResults (org.apache.sysml.api.mlcontext.MLResults)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Formatter (java.util.Formatter)2 CannotCompileException (javassist.CannotCompileException)2 ClassPool (javassist.ClassPool)2 CtClass (javassist.CtClass)2 CtMethod (javassist.CtMethod)2 NotFoundException (javassist.NotFoundException)2