001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.benchmark; 025 026 import org.ujmp.core.Coordinates; 027 import org.ujmp.core.Matrix; 028 import org.ujmp.core.doublematrix.DoubleMatrix2D; 029 import org.ujmp.core.exceptions.MatrixException; 030 import org.ujmp.core.util.StringUtil; 031 import org.ujmp.core.util.UJMPSettings; 032 033 public abstract class AbstractMatrix2DBenchmark implements MatrixBenchmark { 034 035 private static final BenchmarkConfig config = new BenchmarkConfig(); 036 037 public abstract DoubleMatrix2D createMatrix(long... size) throws MatrixException; 038 039 public abstract DoubleMatrix2D createMatrix(Matrix source) throws MatrixException; 040 041 private static long benchmarkSeed = 3345454363676l; 042 043 public AbstractMatrix2DBenchmark() { 044 benchmarkSeed = System.currentTimeMillis(); 045 } 046 047 public final String getMatrixLabel() { 048 return createMatrix(1, 1).getClass().getSimpleName(); 049 } 050 051 public final Class<? extends DoubleMatrix2D> getMatrixClass() { 052 return createMatrix(1, 1).getClass(); 053 } 054 055 public final BenchmarkConfig getConfig() { 056 return config; 057 } 058 059 public void setName(String name) { 060 config.setName(name); 061 } 062 063 public final void run() { 064 if (Runtime.getRuntime().maxMemory() < 900 * 1024 * 1024) { 065 throw new MatrixException("You must start Java with more memory: -Xmx1024M"); 066 } 067 068 // adjust maximal size to fit into memory: 069 // max memory for A*B=C (three matrices, 8 Byte for double) 070 int newMaxSize = (int) Math.sqrt(Runtime.getRuntime().maxMemory() / 24); 071 if (config.getMaxSize() > newMaxSize) { 072 config.setMaxSize(newMaxSize); 073 } 074 075 System.out.println("==============================================================="); 076 System.out.println(" UJMP matrix benchmark"); 077 System.out.println("==============================================================="); 078 System.out.println(" Settings:"); 079 System.out.println(" numberOfThreads: " + config.getNumberOfThreads()); 080 System.out.println(" gcMemory: " + config.isGCMemory()); 081 System.out.println(" purgeMemory: " + config.isPurgeMemory()); 082 System.out.println(" burnInRuns: " + config.getBurnInRuns()); 083 System.out.println(" runs: " + config.getRuns()); 084 System.out.println(" maxTime: " + config.getMaxTime()); 085 System.out.println(" maxStd: " + config.getMaxStd()); 086 System.out.println(" minSize: " 087 + Coordinates.toString('x', config.getSquareSizes().get(0))); 088 System.out.println(" maxSize: " 089 + Coordinates.toString('x', config.getSquareSizes().get( 090 config.getSquareSizes().size() - 1))); 091 092 System.out.println(); 093 094 try { 095 System.out.println("==============================================================="); 096 System.out.println(createMatrix(1, 1).getClass().getSimpleName()); 097 System.out.println("==============================================================="); 098 099 long t0 = System.currentTimeMillis(); 100 101 UJMPSettings.setUseCommonsMath(config.isUseCommonsMath()); 102 UJMPSettings.setUseMTJ(config.isUseMTJ()); 103 UJMPSettings.setUseEJML(config.isUseEJML()); 104 UJMPSettings.setUseJBlas(config.isUseJBlas()); 105 UJMPSettings.setUseOjalgo(config.isUseOjalgo()); 106 UJMPSettings.setUseParallelColt(config.isUseParallelColt()); 107 UJMPSettings.setUseBlockMatrixMultiply(config.isUseBlockMatrixMultiply()); 108 109 UJMPSettings.setDefaultBlockSize(config.getDefaultBlockSize()); 110 111 if (config.isRunTimesScalar()) { 112 new TimesScalarBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 113 } 114 115 if (config.isRunPlusMatrix()) { 116 new PlusMatrixBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 117 } 118 119 if (config.isRunTranspose()) { 120 new TransposeBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 121 } 122 123 if (config.isRunMtimes()) { 124 new MtimesBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 125 } 126 127 if (config.isRunInv()) { 128 new InvBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 129 } 130 131 if (config.isRunInvSPD()) { 132 new InvSPDBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 133 } 134 135 if (config.isRunSolveSquare()) { 136 new SolveSquareBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 137 } 138 139 if (config.isRunSolveTall()) { 140 new SolveTallBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 141 } 142 143 if (config.isRunSVD()) { 144 new SVDBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 145 } 146 147 if (config.isRunEig()) { 148 new EigBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 149 } 150 151 if (config.isRunChol()) { 152 new CholBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 153 } 154 155 if (config.isRunLU()) { 156 new LUBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 157 } 158 159 if (config.isRunQR()) { 160 new QRBenchmarkTask(benchmarkSeed, getMatrixClass(), getConfig()).run(); 161 } 162 163 long t1 = System.currentTimeMillis(); 164 165 System.out.println(); 166 System.out.println("Benchmark runtime: " + StringUtil.duration(t1 - t0)); 167 System.out.println(); 168 169 } catch (UnsupportedClassVersionError e) { 170 System.out.println("this library is not compatible with the current Java version"); 171 System.out.println("it cannot be included in the benchmark"); 172 System.out.println(); 173 } catch (Exception e) { 174 System.out.println("there was some error with this library"); 175 System.out.println("it cannot be included in the benchmark"); 176 e.printStackTrace(); 177 } 178 } 179 180 }