|
| 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.matrix; |
| 21 | + |
| 22 | +import org.apache.sysds.runtime.matrix.data.LibMatrixMult; |
| 23 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 24 | +import org.apache.sysds.test.TestUtils; |
| 25 | +import org.apache.sysds.utils.stats.InfrastructureAnalyzer; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class MatrixMultiplyKernelTest { |
| 29 | + private static final int MIN_PAR = (int)LibMatrixMult.PAR_MINFLOP_THRESHOLD1+1; |
| 30 | + private static final int MIN_PAR_SQRT = (int)Math.sqrt(MIN_PAR); |
| 31 | + |
| 32 | + // dense-dense kernels |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testDenseDenseDotProduct() { |
| 36 | + testMatrixMultiply(1, MIN_PAR, 1, 1, 1); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testDenseDenseOuterProduct() { |
| 41 | + testMatrixMultiply(MIN_PAR_SQRT, 1, MIN_PAR_SQRT, 1, 1); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testDenseDenseVectorScalar() { |
| 46 | + testMatrixMultiply(MIN_PAR, 1, 1, 1, 1); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testDenseDenseMatrixSmallVector() { |
| 51 | + testMatrixMultiply(MIN_PAR, 16, 1, 1, 1); |
| 52 | + } |
| 53 | + |
| 54 | +// @Test //FIXME |
| 55 | +// public void testDenseDenseMatrixLargeVector() { |
| 56 | +// testMatrixMultiply(16, MIN_PAR, 1, 1, 1); |
| 57 | +// } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testDenseDenseVectorMatrix() { |
| 61 | + testMatrixMultiply(1, MIN_PAR, 16, 1, 1); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testDenseDenseSmallMatrixMatrix() { |
| 66 | + testMatrixMultiply(16, MIN_PAR, 16, 1, 1); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDenseDenseMatrixSmallMatrix() { |
| 71 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, 4, 1, 1); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testDenseDenseMatrixMatrix() { |
| 76 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, MIN_PAR_SQRT, 1, 1); |
| 77 | + } |
| 78 | + |
| 79 | + // dense-sparse kernels |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testDenseSparseVectorMatrix() { |
| 83 | + testMatrixMultiply(1, MIN_PAR, 12, 1, 0.1); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testDenseSparseMatrixMatrix() { |
| 88 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, MIN_PAR_SQRT, 1, 0.1); |
| 89 | + } |
| 90 | + |
| 91 | + // sparse-dense kernels |
| 92 | + |
| 93 | +// @Test FIXME |
| 94 | +// public void testSparseDenseDotProduct() { |
| 95 | +// testMatrixMultiply(1, MIN_PAR, 1, 0.1, 1); |
| 96 | +// } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testSparseDenseMatrixSmallVector() { |
| 100 | + testMatrixMultiply(MIN_PAR_SQRT, 1024, 1, 0.1, 1); |
| 101 | + } |
| 102 | + |
| 103 | + @Test // see SYSTEMDS-3769 |
| 104 | + public void testSparseDenseMatrixLargeVector() { |
| 105 | + testMatrixMultiply(13, 8000, 1, 0.1, 1); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testSparseDenseVectorMatrix() { |
| 110 | + testMatrixMultiply(1, MIN_PAR_SQRT, MIN_PAR_SQRT, 0.1, 1); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testSparseDenseSmallMatrixMatrix() { |
| 115 | + testMatrixMultiply(9, MIN_PAR_SQRT, MIN_PAR_SQRT, 0.1, 1); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testSparseDenseMatrixSmallMatrix() { |
| 120 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, 9, 0.1, 1); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testSparseDenseMatrixMatrix() { |
| 125 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, MIN_PAR_SQRT, 0.1, 1); |
| 126 | + } |
| 127 | + |
| 128 | + // sparse-sparse kernels |
| 129 | + @Test |
| 130 | + public void testSparseSparseVectorMatrix() { |
| 131 | + testMatrixMultiply(1, MIN_PAR_SQRT, MIN_PAR_SQRT, 0.1, 0.1); |
| 132 | + } |
| 133 | + |
| 134 | + @Test //w/ sparse output |
| 135 | + public void testSparseSparseSparseMatrixMatrix() { |
| 136 | + testMatrixMultiply(MIN_PAR_SQRT, 2, MIN_PAR_SQRT, 0.1, 0.1); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testSparseSparseMatrixSmallMatrix() { |
| 141 | + testMatrixMultiply(MIN_PAR_SQRT, 15, 1000, 0.1, 0.1); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testSparseSparseMatrixMatrix() { |
| 146 | + testMatrixMultiply(MIN_PAR_SQRT, MIN_PAR_SQRT, MIN_PAR_SQRT, 0.1, 0.1); |
| 147 | + } |
| 148 | + |
| 149 | + private void testMatrixMultiply(int n, int m, int l, double sp1, double sp2) { |
| 150 | + MatrixBlock mb1 = MatrixBlock.randOperations(n, m, sp1, 0, 0.1, "uniform", 3); |
| 151 | + MatrixBlock mb2 = MatrixBlock.randOperations(m, l, sp2, 0, 0.1, "uniform", 7); |
| 152 | + //run single- and multi-threaded kernels and compare |
| 153 | + MatrixBlock ret1 = LibMatrixMult.matrixMult(mb1, mb2); |
| 154 | + MatrixBlock ret2 = LibMatrixMult.matrixMult(mb1, mb2, |
| 155 | + InfrastructureAnalyzer.getLocalParallelism()); |
| 156 | + TestUtils.compareMatrices(ret1, ret2, 1e-8); |
| 157 | + } |
| 158 | +} |
0 commit comments