|
| 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.io.MatrixWriter; |
| 24 | +import org.apache.sysds.runtime.io.MatrixWriterFactory; |
| 25 | +import org.apache.sysds.runtime.matrix.data.LibCommonsMath; |
| 26 | +import org.apache.sysds.runtime.matrix.data.LibMatrixMult; |
| 27 | +import org.apache.sysds.runtime.matrix.data.LibMatrixReorg; |
| 28 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 29 | +import org.apache.sysds.runtime.meta.MatrixCharacteristics; |
| 30 | +import org.apache.sysds.runtime.util.DataConverter; |
| 31 | +import org.apache.sysds.runtime.util.HDFSTool; |
| 32 | +import org.apache.sysds.test.AutomatedTestBase; |
| 33 | +import org.apache.sysds.test.TestConfiguration; |
| 34 | +import org.apache.sysds.test.TestUtils; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Ignore; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | + |
| 41 | +public class lmDSTest extends AutomatedTestBase { |
| 42 | + private final static String TEST_NAME1 = "lmDS"; |
| 43 | + private final static String TEST_DIR = "functions/ooc/"; |
| 44 | + private final static String TEST_CLASS_DIR = TEST_DIR + lmDSTest.class.getSimpleName() + "/"; |
| 45 | + private final static double eps = 1e-10; |
| 46 | + private static final String INPUT_NAME = "X"; |
| 47 | + private static final String INPUT_NAME2 = "y"; |
| 48 | + private static final String OUTPUT_NAME = "R"; |
| 49 | + |
| 50 | + private final static int rows = 100000; |
| 51 | + private final static int cols_wide = 500; //TODO larger than 1000 |
| 52 | + private final static int cols_skinny = 10; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void setUp() { |
| 56 | + TestUtils.clearAssertionInformation(); |
| 57 | + TestConfiguration config = new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1); |
| 58 | + addTestConfiguration(TEST_NAME1, config); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @Ignore //FIXME |
| 63 | + public void testlmDS1() { |
| 64 | + runMatrixVectorMultiplicationTest(cols_wide); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @Ignore //FIXME |
| 69 | + public void testlmDS2() { |
| 70 | + runMatrixVectorMultiplicationTest(cols_skinny); |
| 71 | + } |
| 72 | + |
| 73 | + private void runMatrixVectorMultiplicationTest(int cols) |
| 74 | + { |
| 75 | + Types.ExecMode platformOld = setExecMode(Types.ExecMode.SINGLE_NODE); |
| 76 | + |
| 77 | + try |
| 78 | + { |
| 79 | + getAndLoadTestConfiguration(TEST_NAME1); |
| 80 | + String HOME = SCRIPT_DIR + TEST_DIR; |
| 81 | + fullDMLScriptName = HOME + TEST_NAME1 + ".dml"; |
| 82 | + programArgs = new String[]{"-explain", "-stats", "-ooc", |
| 83 | + "-args", input(INPUT_NAME), input(INPUT_NAME2), output(OUTPUT_NAME)}; |
| 84 | + |
| 85 | + // 1. Generate the data in-memory as MatrixBlock objects |
| 86 | + double[][] X_data = getRandomMatrix(rows, cols, 0, 1, 1.0, 7); |
| 87 | + double[][] y_data = getRandomMatrix(rows, 1, 0, 1, 1.0, 3); |
| 88 | + |
| 89 | + // 2. Convert the double arrays to MatrixBlock objects |
| 90 | + MatrixBlock X_mb = DataConverter.convertToMatrixBlock(X_data); |
| 91 | + MatrixBlock y_mb = DataConverter.convertToMatrixBlock(y_data); |
| 92 | + |
| 93 | + // 3. Create a binary matrix writer |
| 94 | + MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(Types.FileFormat.BINARY); |
| 95 | + |
| 96 | + // 4. Write matrix A to a binary SequenceFile |
| 97 | + writer.writeMatrixToHDFS(X_mb, input(INPUT_NAME), rows, cols, 1000, X_mb.getNonZeros()); |
| 98 | + HDFSTool.writeMetaDataFile(input(INPUT_NAME + ".mtd"), Types.ValueType.FP64, |
| 99 | + new MatrixCharacteristics(rows, cols, 1000, X_mb.getNonZeros()), Types.FileFormat.BINARY); |
| 100 | + |
| 101 | + // 5. Write vector x to a binary SequenceFile |
| 102 | + writer.writeMatrixToHDFS(y_mb, input(INPUT_NAME2), rows, 1, 1000, y_mb.getNonZeros()); |
| 103 | + HDFSTool.writeMetaDataFile(input(INPUT_NAME2 + ".mtd"), Types.ValueType.FP64, |
| 104 | + new MatrixCharacteristics(rows, 1, 1000, y_mb.getNonZeros()), Types.FileFormat.BINARY); |
| 105 | + |
| 106 | + runTest(true, false, null, -1); |
| 107 | + MatrixBlock C = DataConverter.readMatrixFromHDFS( |
| 108 | + output(OUTPUT_NAME), Types.FileFormat.BINARY, rows, cols, 1000, 1000); |
| 109 | + |
| 110 | + //expected results |
| 111 | + MatrixBlock xtx = LibMatrixMult.matrixMultTransposeSelf(X_mb, new MatrixBlock(cols,cols,false), true); |
| 112 | + MatrixBlock xt = LibMatrixReorg.transpose(X_mb); |
| 113 | + MatrixBlock xty = LibMatrixMult.matrixMult(xt, y_mb); |
| 114 | + MatrixBlock ret = LibCommonsMath.computeSolve(xtx, xty); |
| 115 | + for(int i = 0; i < cols; i++) |
| 116 | + Assert.assertEquals(ret.get(i, 0), C.get(i,0), eps); |
| 117 | + } |
| 118 | + catch (IOException e) { |
| 119 | + throw new RuntimeException(e); |
| 120 | + } |
| 121 | + finally { |
| 122 | + resetExecMode(platformOld); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments