|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.test.functions.ooc; |
| 21 | + |
| 22 | +import org.apache.sysds.common.Opcodes; |
| 23 | +import org.apache.sysds.common.Types; |
| 24 | +import org.apache.sysds.common.Types.ExecMode; |
| 25 | +import org.apache.sysds.common.Types.FileFormat; |
| 26 | +import org.apache.sysds.common.Types.ValueType; |
| 27 | +import org.apache.sysds.hops.OptimizerUtils; |
| 28 | +import org.apache.sysds.runtime.instructions.Instruction; |
| 29 | +import org.apache.sysds.runtime.io.MatrixWriter; |
| 30 | +import org.apache.sysds.runtime.io.MatrixWriterFactory; |
| 31 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 32 | +import org.apache.sysds.runtime.matrix.data.MatrixValue; |
| 33 | +import org.apache.sysds.runtime.meta.MatrixCharacteristics; |
| 34 | +import org.apache.sysds.runtime.util.HDFSTool; |
| 35 | +import org.apache.sysds.test.AutomatedTestBase; |
| 36 | +import org.apache.sysds.test.TestConfiguration; |
| 37 | +import org.apache.sysds.test.TestUtils; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +import java.util.HashMap; |
| 42 | + |
| 43 | +public class UnaryTest extends AutomatedTestBase { |
| 44 | + |
| 45 | + private static final String TEST_NAME = "Unary"; |
| 46 | + private static final String TEST_DIR = "functions/ooc/"; |
| 47 | + private static final String TEST_CLASS_DIR = TEST_DIR + UnaryTest.class.getSimpleName() + "/"; |
| 48 | + private static final String INPUT_NAME = "X"; |
| 49 | + private static final String OUTPUT_NAME = "res"; |
| 50 | + |
| 51 | + @Override |
| 52 | + public void setUp() { |
| 53 | + TestUtils.clearAssertionInformation(); |
| 54 | + TestConfiguration config = new TestConfiguration(TEST_CLASS_DIR, TEST_NAME); |
| 55 | + addTestConfiguration(TEST_NAME, config); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test the sum of scalar multiplication, "sum(X*7)", with OOC backend. |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void testUnary() { |
| 63 | + testUnaryOperation(false); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public void testUnaryOperation(boolean rewrite) |
| 68 | + { |
| 69 | + Types.ExecMode platformOld = setExecMode(ExecMode.SINGLE_NODE); |
| 70 | + boolean oldRewrite = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION; |
| 71 | + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = rewrite; |
| 72 | + |
| 73 | + try { |
| 74 | + getAndLoadTestConfiguration(TEST_NAME); |
| 75 | + String HOME = SCRIPT_DIR + TEST_DIR; |
| 76 | + fullDMLScriptName = HOME + TEST_NAME + ".dml"; |
| 77 | + programArgs = new String[] {"-explain", "-stats", "-ooc", |
| 78 | + "-args", input(INPUT_NAME), output(OUTPUT_NAME)}; |
| 79 | + |
| 80 | + int rows = 1000, cols = 1000; |
| 81 | + MatrixBlock mb = MatrixBlock.randOperations(rows, cols, 1.0, -1, 1, "uniform", 7); |
| 82 | + MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(FileFormat.BINARY); |
| 83 | + writer.writeMatrixToHDFS(mb, input(INPUT_NAME), rows, cols, 1000, rows*cols); |
| 84 | + HDFSTool.writeMetaDataFile(input(INPUT_NAME+".mtd"), ValueType.FP64, |
| 85 | + new MatrixCharacteristics(rows,cols,1000,rows*cols), FileFormat.BINARY); |
| 86 | + |
| 87 | + runTest(true, false, null, -1); |
| 88 | + |
| 89 | + HashMap<MatrixValue.CellIndex, Double> dmlfile = readDMLMatrixFromOutputDir(OUTPUT_NAME); |
| 90 | + Double result = dmlfile.get(new MatrixValue.CellIndex(1, 1)); |
| 91 | + double expected = 0.0; |
| 92 | + for(int i = 0; i < rows; i++) { |
| 93 | + for(int j = 0; j < cols; j++) { |
| 94 | + expected += Math.ceil(mb.get(i, j)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Assert.assertEquals(expected, result, 1e-10); |
| 99 | + |
| 100 | + String prefix = Instruction.OOC_INST_PREFIX; |
| 101 | + Assert.assertTrue("OOC wasn't used for RBLK", |
| 102 | + heavyHittersContainsString(prefix + Opcodes.RBLK)); |
| 103 | + Assert.assertTrue("OOC wasn't used for CEIL", |
| 104 | + heavyHittersContainsString(prefix + Opcodes.CEIL)); |
| 105 | + } |
| 106 | + catch(Exception ex) { |
| 107 | + Assert.fail(ex.getMessage()); |
| 108 | + } |
| 109 | + finally { |
| 110 | + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = oldRewrite; |
| 111 | + resetExecMode(platformOld); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments