|
| 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.Types; |
| 23 | +import org.apache.sysds.runtime.controlprogram.LocalVariableMap; |
| 24 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 25 | +import org.apache.sysds.runtime.instructions.Instruction; |
| 26 | +import org.apache.sysds.runtime.instructions.cp.ReorgCPInstruction; |
| 27 | +import org.apache.sysds.runtime.instructions.cp.VariableCPInstruction; |
| 28 | +import org.apache.sysds.runtime.instructions.ooc.ReblockOOCInstruction; |
| 29 | +import org.apache.sysds.runtime.instructions.ooc.TransposeOOCInstruction; |
| 30 | +import org.apache.sysds.runtime.io.MatrixWriter; |
| 31 | +import org.apache.sysds.runtime.io.MatrixWriterFactory; |
| 32 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 33 | +import org.apache.sysds.runtime.matrix.data.MatrixValue; |
| 34 | +import org.apache.sysds.runtime.meta.MatrixCharacteristics; |
| 35 | +import org.apache.sysds.runtime.util.DataConverter; |
| 36 | +import org.apache.sysds.runtime.util.HDFSTool; |
| 37 | +import org.apache.sysds.test.AutomatedTestBase; |
| 38 | +import org.apache.sysds.test.TestConfiguration; |
| 39 | +import org.apache.sysds.test.TestUtils; |
| 40 | +import org.apache.sysds.utils.Statistics; |
| 41 | +import org.junit.Assert; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.HashMap; |
| 47 | +import java.util.List; |
| 48 | + |
| 49 | +public class StreamCollectTest extends AutomatedTestBase { |
| 50 | + private final static String TEST_NAME1 = "StreamCollect"; |
| 51 | + private final static String TEST_DIR = "functions/ooc/"; |
| 52 | + private final static String TEST_CLASS_DIR = TEST_DIR + StreamCollectTest.class.getSimpleName() + "/"; |
| 53 | + private final static int rows = 2000; |
| 54 | + private final static int cols = 1000; |
| 55 | + private final static String INPUT_NAME = "input"; |
| 56 | + private final static String OUTPUT_NAME = "res"; |
| 57 | + private final static double eps = 1e-10; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void setUp() { |
| 61 | + TestUtils.clearAssertionInformation(); |
| 62 | + TestConfiguration config = new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1); |
| 63 | + addTestConfiguration(TEST_NAME1, config); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void runRawInstructionSequenceTest() { |
| 68 | + try { |
| 69 | + getAndLoadTestConfiguration(TEST_NAME1); |
| 70 | + MatrixBlock mb = MatrixBlock.randOperations(rows, cols, 1.0, -1, 1, "uniform", 7); |
| 71 | + MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(Types.FileFormat.BINARY); |
| 72 | + writer.writeMatrixToHDFS(mb, input(INPUT_NAME), rows, cols, 1000, rows * cols); |
| 73 | + HDFSTool.writeMetaDataFile(input(INPUT_NAME + ".mtd"), Types.ValueType.FP64, |
| 74 | + new MatrixCharacteristics(rows, cols, 1000, rows * cols), Types.FileFormat.BINARY); |
| 75 | + |
| 76 | + ExecutionContext ec = new ExecutionContext(new LocalVariableMap()); |
| 77 | + |
| 78 | + VariableCPInstruction createIn = VariableCPInstruction.parseInstruction( |
| 79 | + "CP°createvar°pREADX°" + input(INPUT_NAME) + "°false°MATRIX°binary°" + rows + "°" + cols + |
| 80 | + "°1000°700147°copy"); |
| 81 | + VariableCPInstruction createInRblk = VariableCPInstruction.parseInstruction( |
| 82 | + "CP°createvar°_mVar0°" + input("tmp0") + "°true°MATRIX°binary°" + rows + "°" + cols + |
| 83 | + "°1000°700147°copy"); |
| 84 | + ReblockOOCInstruction rblkIn = ReblockOOCInstruction.parseInstruction( |
| 85 | + "OOC°rblk°pREADX·MATRIX·FP64°_mVar0·MATRIX·FP64°1000°true"); |
| 86 | + VariableCPInstruction createOut = VariableCPInstruction.parseInstruction( |
| 87 | + "CP°createvar°_mVar1°" + input("tmp1") + "°true°MATRIX°binary°" + rows + "°" + cols + |
| 88 | + "°1000°700147°copy"); |
| 89 | + TransposeOOCInstruction oocTranspose = TransposeOOCInstruction.parseInstruction( |
| 90 | + "OOC°r'°_mVar0·MATRIX·FP64°_mVar1·MATRIX·FP64"); |
| 91 | + VariableCPInstruction createOut2 = VariableCPInstruction.parseInstruction( |
| 92 | + "CP°createvar°_mVar2°" + input("tmp2") + "°true°MATRIX°binary°" + rows + "°" + cols + |
| 93 | + "°1000°700147°copy"); |
| 94 | + ReorgCPInstruction cpTranspose = ReorgCPInstruction.parseInstruction( |
| 95 | + "CP°r'°_mVar1·MATRIX·FP64°_mVar2·MATRIX·FP64°1"); |
| 96 | + |
| 97 | + createIn.processInstruction(ec); |
| 98 | + createInRblk.processInstruction(ec); |
| 99 | + rblkIn.processInstruction(ec); |
| 100 | + createOut.processInstruction(ec); |
| 101 | + oocTranspose.processInstruction(ec); |
| 102 | + createOut2.processInstruction(ec); |
| 103 | + cpTranspose.processInstruction(ec); |
| 104 | + } |
| 105 | + catch(Exception ex) { |
| 106 | + Assert.fail(ex.getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void runAppTest1() { |
| 112 | + runAppTest("_1", List.of(List.of(1000D, 1D), List.of(1000D, 1000D))); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void runAppTest2() { |
| 117 | + runAppTest("_2", List.of(List.of(500D, 1D), List.of(500D, 500D))); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void runAppTest3() { |
| 122 | + runAppTest("_3", List.of(List.of(1500D, 1D), List.of(1500D, 1500D))); |
| 123 | + } |
| 124 | + |
| 125 | + private void runAppTest(String scriptSuffix, List<List<Double>> matrixDims) { |
| 126 | + Types.ExecMode platformOld = setExecMode(Types.ExecMode.SINGLE_NODE); |
| 127 | + |
| 128 | + try { |
| 129 | + getAndLoadTestConfiguration(TEST_NAME1); |
| 130 | + String HOME = SCRIPT_DIR + TEST_DIR; |
| 131 | + fullDMLScriptName = HOME + TEST_NAME1 + scriptSuffix + ".dml"; |
| 132 | + List<String> proArgs = new ArrayList<>(); |
| 133 | + proArgs.add("-explain"); |
| 134 | + proArgs.add("-stats"); |
| 135 | + proArgs.add("-ooc"); |
| 136 | + proArgs.add("-args"); |
| 137 | + //programArgs = new String[]{"-explain", "-stats", "-ooc", |
| 138 | + // "-args", input(INPUT_NAME), output(OUTPUT_NAME)}; |
| 139 | + |
| 140 | + for(int i = 0; i < matrixDims.size(); i++) { |
| 141 | + List<Double> dims = matrixDims.get(i); |
| 142 | + int mrows = dims.get(0).intValue(); |
| 143 | + int mcols = dims.get(1).intValue(); |
| 144 | + |
| 145 | + // 1. Generate the data as MatrixBlock object |
| 146 | + double[][] A_data = getRandomMatrix(mrows, mcols, 0, 10, 1, 10); |
| 147 | + |
| 148 | + // 2. Convert the double arrays to MatrixBlock object |
| 149 | + MatrixBlock A_mb = DataConverter.convertToMatrixBlock(A_data); |
| 150 | + |
| 151 | + // 3. Create a binary matrix writer |
| 152 | + MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(Types.FileFormat.BINARY); |
| 153 | + |
| 154 | + // 4. Write matrix A to a binary SequenceFile |
| 155 | + writer.writeMatrixToHDFS(A_mb, input(INPUT_NAME + "_" + i), mrows, mcols, 1000, A_mb.getNonZeros()); |
| 156 | + HDFSTool.writeMetaDataFile(input(INPUT_NAME + "_" + i + ".mtd"), Types.ValueType.FP64, |
| 157 | + new MatrixCharacteristics(mrows, mcols, 1000, A_mb.getNonZeros()), Types.FileFormat.BINARY); |
| 158 | + proArgs.add(input(INPUT_NAME + "_" + i)); |
| 159 | + } |
| 160 | + |
| 161 | + proArgs.add(output(OUTPUT_NAME)); |
| 162 | + |
| 163 | + programArgs = proArgs.toArray(String[]::new); |
| 164 | + |
| 165 | + boolean exceptionExpected = false; |
| 166 | + runTest(true, exceptionExpected, null, -1); |
| 167 | + |
| 168 | + // Validate that at least one OOC instruction was used |
| 169 | + Assert.assertTrue("OOC wasn't used", heavyHittersContainOOC()); |
| 170 | + |
| 171 | + proArgs.remove(2); // Remove ooc flag |
| 172 | + proArgs.set(proArgs.size() - 1, output(OUTPUT_NAME + "_target")); |
| 173 | + programArgs = proArgs.toArray(String[]::new); |
| 174 | + runTest(true, exceptionExpected, null, -1); |
| 175 | + |
| 176 | + HashMap<MatrixValue.CellIndex, Double> result = readDMLMatrixFromOutputDir(OUTPUT_NAME); |
| 177 | + HashMap<MatrixValue.CellIndex, Double> target = readDMLMatrixFromOutputDir(OUTPUT_NAME + "_target"); |
| 178 | + TestUtils.compareMatrices(result, target, eps, "Result", "Target"); |
| 179 | + } |
| 180 | + catch(IOException e) { |
| 181 | + throw new RuntimeException(e); |
| 182 | + } |
| 183 | + finally { |
| 184 | + resetExecMode(platformOld); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private boolean heavyHittersContainOOC() { |
| 189 | + for(String opcode : Statistics.getCPHeavyHitterOpCodes()) |
| 190 | + if(opcode.startsWith(Instruction.OOC_INST_PREFIX)) |
| 191 | + return true; |
| 192 | + return false; |
| 193 | + } |
| 194 | +} |
0 commit comments