Search in sources :

Example 6 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MatrixMatrixElementWiseOpTests method runMatrixMatrixElementwiseTest.

/**
	 * Runs a simple matrix-matrix elementwise op test
	 *
	 * @param scriptStr         the script string
	 * @param input1            name of the first input variable in the script string
	 * @param input2            name of the second input variable in the script string
	 * @param output            name of the output variable in the script string
	 * @param heavyHitterOpcode the string printed for the unary op heavy hitter when executed on gpu
	 */
private void runMatrixMatrixElementwiseTest(String scriptStr, String input1, String input2, String output, String heavyHitterOpcode) {
    for (int i = 0; i < rowSizes.length; i++) {
        for (int j = 0; j < columnSizes.length; j++) {
            for (int k = 0; k < sparsities.length; k++) {
                int m = rowSizes[i];
                int n = columnSizes[j];
                double sparsity = sparsities[k];
                Matrix X = generateInputMatrix(spark, m, n, sparsity, seed);
                Matrix Y = generateInputMatrix(spark, m, n, sparsity, seed);
                HashMap<String, Object> inputs = new HashMap<>();
                inputs.put(input1, X);
                inputs.put(input2, Y);
                List<Object> cpuOut = runOnCPU(spark, scriptStr, inputs, Arrays.asList(output));
                List<Object> gpuOut = runOnGPU(spark, scriptStr, inputs, Arrays.asList(output));
                //assertHeavyHitterPresent(heavyHitterOpcode);
                assertEqualObjects(cpuOut.get(0), gpuOut.get(0));
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap)

Example 7 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MatrixMatrixElementWiseOpTests method runAxpyTest.

/**
	 * Run O = aX +/- Y type operations test
	 *
	 * @param scriptStr         the script string
	 * @param input1            name of the first matrix input variable in the script string
	 * @param input2            name of the second matrix input variable in the script string
	 * @param scalarInput       name of the scalar which is multiplied with the first or second matrix
	 * @param output            name of the output variable in the script string
	 * @param heavyHitterOpcode the string printed for the unary op heavy hitter when executed on gpu
	 */
private void runAxpyTest(String scriptStr, String input1, String input2, String scalarInput, String output, String heavyHitterOpcode) {
    for (int i = 0; i < rowSizes.length; i++) {
        for (int j = 0; j < columnSizes.length; j++) {
            for (int k = 0; k < sparsities.length; k++) {
                for (int l = 0; l < scalars.length; l++) {
                    int m = rowSizes[i];
                    int n = columnSizes[j];
                    double scalar = scalars[l];
                    double sparsity = sparsities[k];
                    Matrix X = generateInputMatrix(spark, m, n, sparsity, seed);
                    Matrix Y = generateInputMatrix(spark, m, n, sparsity, seed);
                    HashMap<String, Object> inputs = new HashMap<>();
                    inputs.put(input1, X);
                    inputs.put(input2, Y);
                    inputs.put(scalarInput, scalar);
                    // Test O = aX + Y
                    List<Object> cpuOut = runOnCPU(spark, scriptStr, inputs, Arrays.asList(output));
                    List<Object> gpuOut = runOnGPU(spark, scriptStr, inputs, Arrays.asList(output));
                    //assertHeavyHitterPresent(heavyHitterOpcode);
                    assertEqualObjects(cpuOut.get(0), gpuOut.get(0));
                }
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap)

Example 8 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-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 9 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MatrixMultiplicationOpTest method transposeSelfMatrixMultiply.

@Test
public void transposeSelfMatrixMultiply() {
    String scriptStr = "O = t(X) %*% X";
    int[] sizes = { 1, 128, 512, 1024, 2049 };
    double[] sparsities = { 0.0, 0.03, 0.3, 0.9 };
    for (int i = 0; i < sizes.length; i++) {
        for (int j = 0; j < sparsities.length; j++) {
            int side = sizes[i];
            double sparsity = sparsities[j];
            Matrix X = generateInputMatrix(spark, side, side, sparsity, seed);
            HashMap<String, Object> inputs = new HashMap<>();
            inputs.put("X", X);
            List<Object> cpuOuts = runOnCPU(spark, scriptStr, inputs, Arrays.asList("O"));
            List<Object> gpuOuts = runOnGPU(spark, scriptStr, inputs, Arrays.asList("O"));
            //assertHeavyHitterPresent("gpu_tsmm'");
            assertEqualObjects(cpuOuts.get(0), gpuOuts.get(0));
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MLContextMultipleScriptsTest method runMLContextTestMultipleScript.

/**
	 * 
	 * @param platform
	 */
private void runMLContextTestMultipleScript(RUNTIME_PLATFORM platform, boolean wRead) {
    RUNTIME_PLATFORM oldplatform = DMLScript.rtplatform;
    DMLScript.rtplatform = platform;
    //create mlcontext
    SparkSession spark = createSystemMLSparkSession("MLContextMultipleScriptsTest", "local");
    MLContext ml = new MLContext(spark);
    ml.setExplain(true);
    String dml1 = baseDirectory + File.separator + "MultiScript1.dml";
    String dml2 = baseDirectory + File.separator + (wRead ? "MultiScript2b.dml" : "MultiScript2.dml");
    String dml3 = baseDirectory + File.separator + (wRead ? "MultiScript3b.dml" : "MultiScript3.dml");
    try {
        //run script 1
        Script script1 = dmlFromFile(dml1).in("$rows", rows).in("$cols", cols).out("X");
        Matrix X = ml.execute(script1).getMatrix("X");
        Script script2 = dmlFromFile(dml2).in("X", X).out("Y");
        Matrix Y = ml.execute(script2).getMatrix("Y");
        Script script3 = dmlFromFile(dml3).in("X", X).in("Y", Y).out("z");
        String z = ml.execute(script3).getString("z");
        System.out.println(z);
    } 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)

Aggregations

Matrix (org.apache.sysml.api.mlcontext.Matrix)20 HashMap (java.util.HashMap)15 Test (org.junit.Test)8 Script (org.apache.sysml.api.mlcontext.Script)5 DMLScript (org.apache.sysml.api.DMLScript)3 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)3 MLContext (org.apache.sysml.api.mlcontext.MLContext)3 SparkSession (org.apache.spark.sql.SparkSession)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CoordinateMatrix (org.apache.spark.mllib.linalg.distributed.CoordinateMatrix)1 MatrixEntry (org.apache.spark.mllib.linalg.distributed.MatrixEntry)1 Row (org.apache.spark.sql.Row)1 FrameMetadata (org.apache.sysml.api.mlcontext.FrameMetadata)1 MLResults (org.apache.sysml.api.mlcontext.MLResults)1 MatrixMetadata (org.apache.sysml.api.mlcontext.MatrixMetadata)1 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)1