|
| 1 | +package org.apache.sysds.performance.matrix; |
| 2 | + |
| 3 | +import org.apache.sysds.performance.compression.APerfTest; |
| 4 | +import org.apache.sysds.performance.generators.ConstMatrix; |
| 5 | +import org.apache.sysds.performance.generators.IGenerate; |
| 6 | +import org.apache.sysds.runtime.functionobjects.IndexFunction; |
| 7 | +import org.apache.sysds.runtime.functionobjects.RollIndex; |
| 8 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 9 | +import org.apache.sysds.runtime.matrix.operators.ReorgOperator; |
| 10 | +import org.apache.sysds.test.TestUtils; |
| 11 | +import org.apache.sysds.utils.stats.InfrastructureAnalyzer; |
| 12 | + |
| 13 | +import java.util.Random; |
| 14 | + |
| 15 | +public class MatrixRollPerf extends APerfTest<Object, MatrixBlock> { |
| 16 | + |
| 17 | + private final int rows; |
| 18 | + private final int cols; |
| 19 | + private final int shift; |
| 20 | + private final int k; |
| 21 | + |
| 22 | + private final ReorgOperator reorg; |
| 23 | + private MatrixBlock out; |
| 24 | + |
| 25 | + public MatrixRollPerf(int N, int W, IGenerate<MatrixBlock> gen, int rows, int cols, int shift, int k) { |
| 26 | + super(N, W, gen); |
| 27 | + this.rows = rows; |
| 28 | + this.cols = cols; |
| 29 | + this.shift = shift; |
| 30 | + this.k = k; |
| 31 | + |
| 32 | + IndexFunction op = new RollIndex(shift); |
| 33 | + this.reorg = new ReorgOperator(op, k); |
| 34 | + } |
| 35 | + |
| 36 | + public void run() throws Exception { |
| 37 | + MatrixBlock mb = gen.take(); |
| 38 | + logInfos(rows, cols, shift, mb.getSparsity(), k); |
| 39 | + |
| 40 | + |
| 41 | + String info = String.format("rows: %5d cols: %5d sp: %.4f shift: %4d k: %2d", |
| 42 | + rows, cols, mb.getSparsity(), shift, k); |
| 43 | + |
| 44 | + |
| 45 | + warmup(this::rollOnce, W); |
| 46 | + |
| 47 | + execute(this::rollOnce, info); |
| 48 | + } |
| 49 | + |
| 50 | + private void logInfos(int rows, int cols, int shift, double sparsity, int k) { |
| 51 | + String matrixType = sparsity == 1 ? "Dense" : "Sparse"; |
| 52 | + if (k == 1) { |
| 53 | + System.out.println("---------------------------------------------------------------------------------------------------------"); |
| 54 | + System.out.printf("%s Experiment for rows %d columns %d and shift %d \n", matrixType, rows, cols, shift); |
| 55 | + System.out.println("---------------------------------------------------------------------------------------------------------"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void rollOnce() { |
| 60 | + MatrixBlock in = gen.take(); |
| 61 | + |
| 62 | + if (out == null) |
| 63 | + out = new MatrixBlock(rows, cols, in.isInSparseFormat()); |
| 64 | + |
| 65 | + out.reset(rows, cols, in.isInSparseFormat()); |
| 66 | + |
| 67 | + in.reorgOperations(reorg, out, 0, 0, 0); |
| 68 | + |
| 69 | + ret.add(null); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected String makeResString() { |
| 74 | + return ""; |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) throws Exception { |
| 78 | + int kMulti = InfrastructureAnalyzer.getLocalParallelism(); |
| 79 | + int reps = 2000; |
| 80 | + int warmup = 200; |
| 81 | + |
| 82 | + int minRows = 2017; |
| 83 | + int minCols = 1001; |
| 84 | + double spSparse = 0.01; |
| 85 | + int minShift = -50; |
| 86 | + int maxShift = 1022; |
| 87 | + int iterations = 10; |
| 88 | + |
| 89 | + Random rand = new Random(42); |
| 90 | + |
| 91 | + for (int i = 0; i < iterations; i++) { |
| 92 | + int rows = minRows + rand.nextInt(500); |
| 93 | + int cols = minCols + rand.nextInt(500); |
| 94 | + int shift = rand.nextInt((maxShift - minShift) + 1) + minShift; |
| 95 | + |
| 96 | + MatrixBlock denseIn = TestUtils.generateTestMatrixBlock(rows, cols, -100, 100, 1.0, 42); |
| 97 | + MatrixBlock sparseIn = TestUtils.generateTestMatrixBlock(rows, cols, -100, 100, spSparse, 42); |
| 98 | + |
| 99 | + // Run Dense Case (Single vs Multi-threaded) |
| 100 | + new MatrixRollPerf(reps, warmup, new ConstMatrix(denseIn, -1), rows, cols, shift, 1).run(); |
| 101 | + new MatrixRollPerf(reps, warmup, new ConstMatrix(denseIn, -1), rows, cols, shift, kMulti).run(); |
| 102 | + |
| 103 | + // Run Sparse Case (Single vs Multi-threaded) |
| 104 | + new MatrixRollPerf(reps, warmup, new ConstMatrix(sparseIn, -1), rows, cols, shift, 1).run(); |
| 105 | + new MatrixRollPerf(reps, warmup, new ConstMatrix(sparseIn, -1), rows, cols, shift, kMulti).run(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments