|
| 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.component.compress.colgroup; |
| 21 | + |
| 22 | +import static org.junit.Assert.fail; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.apache.commons.lang3.NotImplementedException; |
| 29 | +import org.apache.commons.logging.Log; |
| 30 | +import org.apache.commons.logging.LogFactory; |
| 31 | +import org.apache.sysds.runtime.compress.CompressedMatrixBlock; |
| 32 | +import org.apache.sysds.runtime.compress.CompressionSettings; |
| 33 | +import org.apache.sysds.runtime.compress.CompressionSettingsBuilder; |
| 34 | +import org.apache.sysds.runtime.compress.colgroup.AColGroup; |
| 35 | +import org.apache.sysds.runtime.compress.colgroup.AColGroup.CompressionType; |
| 36 | +import org.apache.sysds.runtime.compress.colgroup.ColGroupFactory; |
| 37 | +import org.apache.sysds.runtime.compress.colgroup.indexes.ColIndexFactory; |
| 38 | +import org.apache.sysds.runtime.compress.colgroup.indexes.IColIndex; |
| 39 | +import org.apache.sysds.runtime.compress.estim.CompressedSizeInfo; |
| 40 | +import org.apache.sysds.runtime.compress.estim.CompressedSizeInfoColGroup; |
| 41 | +import org.apache.sysds.runtime.compress.estim.EstimationFactors; |
| 42 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 43 | +import org.apache.sysds.test.TestUtils; |
| 44 | +import org.junit.Test; |
| 45 | +import org.junit.runner.RunWith; |
| 46 | +import org.junit.runners.Parameterized; |
| 47 | +import org.junit.runners.Parameterized.Parameters; |
| 48 | + |
| 49 | +@RunWith(value = Parameterized.class) |
| 50 | +public class CombineColGroups { |
| 51 | + protected static final Log LOG = LogFactory.getLog(CombineColGroups.class.getName()); |
| 52 | + |
| 53 | + /** Uncompressed ground truth */ |
| 54 | + final MatrixBlock mb; |
| 55 | + /** ColGroup 1 */ |
| 56 | + final AColGroup a; |
| 57 | + /** ColGroup 2 */ |
| 58 | + final AColGroup b; |
| 59 | + |
| 60 | + @Parameters |
| 61 | + public static Collection<Object[]> data() { |
| 62 | + ArrayList<Object[]> tests = new ArrayList<>(); |
| 63 | + |
| 64 | + try { |
| 65 | + addTwoCols(tests, 100, 3); |
| 66 | + addTwoCols(tests, 1000, 3); |
| 67 | + // addSingleVSMultiCol(tests, 100, 3, 1, 3); |
| 68 | + // addSingleVSMultiCol(tests, 100, 3, 3, 4); |
| 69 | + addSingleVSMultiCol(tests, 1000, 3, 1, 3, 1.0); |
| 70 | + addSingleVSMultiCol(tests, 1000, 3, 3, 4, 1.0); |
| 71 | + addSingleVSMultiCol(tests, 1000, 3, 3, 1, 1.0); |
| 72 | + addSingleVSMultiCol(tests, 1000, 2, 1, 10, 0.05); |
| 73 | + addSingleVSMultiCol(tests, 1000, 2, 10, 10, 0.05); |
| 74 | + addSingleVSMultiCol(tests, 1000, 2, 10, 1, 0.05); |
| 75 | + } |
| 76 | + catch(Exception e) { |
| 77 | + e.printStackTrace(); |
| 78 | + fail("failed constructing tests"); |
| 79 | + } |
| 80 | + |
| 81 | + return tests; |
| 82 | + } |
| 83 | + |
| 84 | + public CombineColGroups(MatrixBlock mb, AColGroup a, AColGroup b) { |
| 85 | + this.mb = mb; |
| 86 | + this.a = a; |
| 87 | + this.b = b; |
| 88 | + |
| 89 | + CompressedMatrixBlock.debug = true; |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void combine() { |
| 94 | + try { |
| 95 | + AColGroup c = a.combine(b, mb.getNumRows()); |
| 96 | + MatrixBlock ref = new MatrixBlock(mb.getNumRows(), mb.getNumColumns(), false); |
| 97 | + ref.allocateDenseBlock(); |
| 98 | + c.decompressToDenseBlock(ref.getDenseBlock(), 0, mb.getNumRows()); |
| 99 | + ref.recomputeNonZeros(); |
| 100 | + String errMessage = a.getClass().getSimpleName() + ": " + a.getColIndices() + " -- " |
| 101 | + + b.getClass().getSimpleName() + ": " + b.getColIndices(); |
| 102 | + |
| 103 | + TestUtils.compareMatricesBitAvgDistance(mb, ref, 0, 0, errMessage); |
| 104 | + } |
| 105 | + catch(NotImplementedException e) { |
| 106 | + // allowed |
| 107 | + } |
| 108 | + catch(Exception e) { |
| 109 | + e.printStackTrace(); |
| 110 | + fail(e.getMessage()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void addTwoCols(ArrayList<Object[]> tests, int nRow, int distinct) { |
| 115 | + MatrixBlock mb = TestUtils.ceil(// |
| 116 | + TestUtils.generateTestMatrixBlock(nRow, 2, 0, distinct, 1.0, 231)); |
| 117 | + |
| 118 | + List<AColGroup> c1s = getGroups(mb, ColIndexFactory.createI(0)); |
| 119 | + List<AColGroup> c2s = getGroups(mb, ColIndexFactory.createI(1)); |
| 120 | + |
| 121 | + for(int i = 0; i < c1s.size(); i++) { |
| 122 | + for(int j = 0; j < c2s.size(); j++) { |
| 123 | + tests.add(new Object[] {mb, c1s.get(i), c2s.get(j)}); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static void addSingleVSMultiCol(ArrayList<Object[]> tests, int nRow, int distinct, int nColL, int nColR, |
| 129 | + double sparsity) { |
| 130 | + MatrixBlock mb = TestUtils.ceil(// |
| 131 | + TestUtils.generateTestMatrixBlock(nRow, nColL + nColR, 0, distinct, sparsity, 231)); |
| 132 | + |
| 133 | + List<AColGroup> c1s = getGroups(mb, ColIndexFactory.create(nColL)); |
| 134 | + List<AColGroup> c2s = getGroups(mb, ColIndexFactory.create(nColL, nColR + nColL)); |
| 135 | + |
| 136 | + for(int i = 0; i < c1s.size(); i++) { |
| 137 | + for(int j = 0; j < c2s.size(); j++) { |
| 138 | + tests.add(new Object[] {mb, c1s.get(0), c2s.get(0)}); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static List<AColGroup> getGroups(MatrixBlock mb, IColIndex cols) { |
| 144 | + final CompressionSettings cs = new CompressionSettingsBuilder().create(); |
| 145 | + |
| 146 | + final int nRow = mb.getNumColumns(); |
| 147 | + final List<CompressedSizeInfoColGroup> es = new ArrayList<>(); |
| 148 | + final EstimationFactors f = new EstimationFactors(nRow, nRow, mb.getSparsity()); |
| 149 | + es.add(new CompressedSizeInfoColGroup(cols, f, 312152, CompressionType.DDC)); |
| 150 | + es.add(new CompressedSizeInfoColGroup(cols, f, 321521, CompressionType.RLE)); |
| 151 | + es.add(new CompressedSizeInfoColGroup(cols, f, 321452, CompressionType.SDC)); |
| 152 | + es.add(new CompressedSizeInfoColGroup(cols, f, 325151, CompressionType.UNCOMPRESSED)); |
| 153 | + final CompressedSizeInfo csi = new CompressedSizeInfo(es); |
| 154 | + return ColGroupFactory.compressColGroups(mb, csi, cs); |
| 155 | + } |
| 156 | +} |
0 commit comments