aboutsummaryrefslogtreecommitdiff
path: root/javamatrix/framework/javamatrix/BenchBase.java
blob: 556d7d3766ca78788a7acc03555be9afbeceb453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2021 Linaro Limited. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package benchmarks.javamatrix;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import jmbench.tools.runtime.FactoryRuntimeEvaluationCase;
import jmbench.tools.runtime.RuntimeBenchmarkConfig;
import jmbench.tools.runtime.RuntimeEvaluationCase;
import jmbench.tools.runtime.RuntimeEvaluationTest;

public abstract class BenchBase {

  public enum CaseType {
    MULT,
    ADD,
    TRANSPOSE_SQUARE,
    TRANSPOSE_TALL,
    TRANSPOSE_WIDE,
    SCALE,
    DET,
    INVERT,
    INVERT_SYMM_POS_DEF,
    SVD,
    CHOL,
    MULT_TRANS_B,
    SOLVE_EXACT,
    SOLVE_OVER,
    QR,
    LU,
    EIG_SYMM,
  }

  private class BenchMapKey {
    public CaseType caseType;
    public int size;

    public BenchMapKey(CaseType caseType, int size) {
      this.caseType = caseType;
      this.size = size;
    }

    @Override
    public boolean equals(Object obj) {
      if (obj.getClass() != BenchMapKey.class) {
        return false;
      }
      BenchMapKey other = (BenchMapKey) obj;
      return (caseType.equals(other.caseType)) &&
             (size == other.size);
    }

    @Override
    public int hashCode() {
      return Objects.hash(caseType, size);
    }
  }

  private FactoryRuntimeEvaluationCase factory;
  private String factoryString;
  private long seed;
  private int[] sizes;

  private Map<BenchMapKey, RuntimeEvaluationTest> testMap;

  public BenchBase(String factoryString, long seed, EnumSet<CaseType> caseTypes, int[] sizes) {
    this.seed = seed;
    this.factoryString = factoryString;
    this.sizes = sizes;
    factory = createEvaluationCasesFactory(factoryString);

    // Create and fill a hashmap of EvaluationTests ready to run
    testMap = new HashMap<BenchMapKey, RuntimeEvaluationTest>();
    for (CaseType caseType: caseTypes) {
      for (int size: sizes) {
        RuntimeEvaluationTest test = createEvaluationTest(caseType, size);
        prepareEvaluationTest(test);
        BenchMapKey key = new BenchMapKey(caseType, size);
        testMap.put(key, test);
      }
    }
  }

  // Helper function to perform a full timing function for a specific CaseType
  // and size of matrix.
  //   iters: number of iterations to perform.
  //   caseType: enum specifying which case of the benchmark to perform.
  //   size: the size of matrix to use for the benchmark case
  //         must be one of the sizes passed to the constructor.
  public void timeRuntimeBenchmark(int iters, CaseType caseType, int size) {
    for (int i = 0; i < iters; i++) {
      try {
        RuntimeEvaluationTest test =
            getEvaluationTest(caseType, size);
        test.evaluate();
      } catch (RuntimeException e) {
        e.printStackTrace();
      }
    }
  }

  // Creates the factory used to build the test cases for the benchmark library.
  //   factoryString: the name of the class to use as a factory.
  //
  //   returns: factory used for generating evaluationCases.
  private FactoryRuntimeEvaluationCase createEvaluationCasesFactory(String factoryString) {
    // Create dummy configuration. This would normally be used to specify which
    // cases to use and the size of the matricies but that functionality has
    // been bypassed.
    RuntimeBenchmarkConfig config = new RuntimeBenchmarkConfig();
    return new FactoryRuntimeEvaluationCase(factoryString,config);
  }

  // Get an EvaluationTest ready to run.
  // Initializes and generates the required matrices.
  private void prepareEvaluationTest(RuntimeEvaluationTest test) {
    test.setRandomSeed(seed);
    test.init();
    test.setupTest();
  }

  // Get a pre-generated EvaluationTest.
  //   caseType: enum specifying the case of the benchmark.
  //   size: the size of matrix to use for the test.
  //
  //   return: the EvaluationTest for that case and size.
  private RuntimeEvaluationTest getEvaluationTest(CaseType caseType,
                                                 int size) {
    BenchMapKey key = new BenchMapKey(caseType, size);
    return testMap.get(key);
  }

  // Create an EvaluationTest for a particular case at a particular size.
  //   caseType: enum specifying the case of the benchmark.
  //   size: the size of matrix to use for the test.
  //
  //   return: an EvaluationTest that contains all the details to run a test.
  private RuntimeEvaluationTest createEvaluationTest(CaseType caseType, int size) {
    RuntimeEvaluationCase eCase = createEvaluationCase(caseType);
    RuntimeEvaluationTest test = new RuntimeEvaluationTest();
    test.setDimen(size);
    test.setNameAlgorithm(eCase.getNameAlgorithm());
    test.setClassFactory(eCase.getClassFactory());
    test.setGenerator(eCase.getGenerator());
    return test;
  }

  // Create a new evaluation case for a particular case of the benchmark.
  //   caseType: enum specifying the case of the benchmark.
  //
  //   returns: an EvaluationCase that is required for a EvaluationTest.
  private RuntimeEvaluationCase createEvaluationCase(CaseType caseType) {
    switch (caseType) {
      case MULT:
        return factory.createMatrixMult(factoryString);
      case ADD:
        return factory.createMatrixAdd(factoryString);
      case TRANSPOSE_SQUARE:
        return factory.createTransposeSquare(factoryString);
      case TRANSPOSE_TALL:
        return factory.createTransposeTall(factoryString);
      case TRANSPOSE_WIDE:
        return factory.createTransposeWide(factoryString);
      case SCALE:
        return factory.createScale(factoryString);
      case DET:
        return factory.createDeterminant(factoryString);
      case INVERT:
        return factory.createInvert(factoryString);
      case INVERT_SYMM_POS_DEF:
        return factory.createInvertSymmPosDef(factoryString);
      case SVD:
        return factory.createSVD(factoryString);
      case CHOL:
        return factory.createCholesky(factoryString);
      case MULT_TRANS_B:
        return factory.createMultTranB(factoryString);
      case SOLVE_EXACT:
        return factory.createSolveEq(factoryString);
      case SOLVE_OVER:
        return factory.createSolveOver(factoryString);
      case QR:
        return factory.createQR(factoryString);
      case LU:
        return factory.createLU(factoryString);
      case EIG_SYMM:
        return factory.createEigSymm(factoryString);
      default:
        return null;
    }
  }

}