|
| 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.instructions.InstructionUtils; |
| 23 | +import org.apache.sysds.runtime.matrix.data.LibMatrixAgg; |
| 24 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 25 | +import org.apache.sysds.runtime.matrix.operators.AggregateUnaryOperator; |
| 26 | +import org.apache.sysds.test.TestUtils; |
| 27 | +import org.apache.sysds.utils.stats.InfrastructureAnalyzer; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class MatrixAggKernelTest { |
| 31 | + private static final int MIN_PAR = 2*(int)LibMatrixAgg.PAR_NUMCELL_THRESHOLD1; |
| 32 | + private static final int MIN_PAR_SQRT = (int)Math.sqrt(MIN_PAR); |
| 33 | + private static final int K = InfrastructureAnalyzer.getLocalParallelism(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testDenseKahanSum() { |
| 37 | + testMatrixAggregation("uak+", "uark+", "uack+", 0.95); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testSparseKahanSum() { |
| 42 | + testMatrixAggregation("uak+", "uark+", "uack+", 0.1); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testDenseSum() { |
| 47 | + testMatrixAggregation("ua+", "uar+", "uac+", 0.95); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSparseSum() { |
| 52 | + testMatrixAggregation("ua+", "uar+", "uac+", 0.1); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testDenseSqSum() { |
| 57 | + testMatrixAggregation("uasqk+", "uarsqk+", "uacsqk+", 0.95); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testSparseSqSum() { |
| 62 | + testMatrixAggregation("uasqk+", "uarsqk+", "uacsqk+", 0.1); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDenseMean() { |
| 67 | + testMatrixAggregation("uamean", "uarmean", "uacmean", 0.95); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testSparseMean() { |
| 72 | + testMatrixAggregation("uamean", "uarmean", "uacmean", 0.1); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testDenseVar() { |
| 77 | + testMatrixAggregation("uavar", "uarvar", "uacvar", 0.95); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testSparseVar() { |
| 82 | + testMatrixAggregation("uavar", "uarvar", "uacvar", 0.1); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testDenseMin() { |
| 87 | + testMatrixAggregation("uamin", "uarmin", "uacmin", 0.95); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSparseMin() { |
| 92 | + testMatrixAggregation("uamin", "uarmin", "uacmin", 0.1); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testDenseMax() { |
| 97 | + testMatrixAggregation("uamax", "uarmax", "uacmax", 0.95); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testSparseMax() { |
| 102 | + testMatrixAggregation("uamax", "uarmax", "uacmax", 0.1); |
| 103 | + } |
| 104 | + |
| 105 | + private void testMatrixAggregation(String opcode1, String opcode2, String opcode3, double sp) { |
| 106 | + testMatrixAggregation(getOp(opcode1,1), getOp(opcode1,K), MIN_PAR, 1, sp); |
| 107 | + testMatrixAggregation(getOp(opcode2,1), getOp(opcode2,K), 1, MIN_PAR, sp); |
| 108 | + testMatrixAggregation(getOp(opcode3,1), getOp(opcode3,K), MIN_PAR, 1, sp); |
| 109 | + testMatrixAggregation(getOp(opcode1,1), getOp(opcode1,K), MIN_PAR_SQRT, MIN_PAR_SQRT, sp); |
| 110 | + testMatrixAggregation(getOp(opcode2,1), getOp(opcode2,K), MIN_PAR_SQRT, MIN_PAR_SQRT, sp); |
| 111 | + testMatrixAggregation(getOp(opcode3,1), getOp(opcode3,K), MIN_PAR_SQRT, MIN_PAR_SQRT, sp); |
| 112 | + } |
| 113 | + |
| 114 | + private void testMatrixAggregation(AggregateUnaryOperator uaop1, |
| 115 | + AggregateUnaryOperator uaopk, int n, int m, double sp) |
| 116 | + { |
| 117 | + MatrixBlock mb1 = MatrixBlock.randOperations(n, m, sp, 0, 0.1, "uniform", 7); |
| 118 | + //run single- and multi-threaded kernels and compare |
| 119 | + MatrixBlock ret1 = mb1.aggregateUnaryOperations(uaop1); |
| 120 | + MatrixBlock ret2 = mb1.aggregateUnaryOperations(uaopk); |
| 121 | + TestUtils.compareMatrices(ret1, ret2, 1e-8); |
| 122 | + } |
| 123 | + |
| 124 | + private AggregateUnaryOperator getOp(String opcode, int threads) { |
| 125 | + return InstructionUtils.parseBasicAggregateUnaryOperator(opcode, threads); |
| 126 | + } |
| 127 | +} |
0 commit comments