001// --- BEGIN LICENSE BLOCK ---
002/*
003 * Copyright (c) 2009, Mikio L. Braun
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are
008 * met:
009 *
010 *     * Redistributions of source code must retain the above copyright
011 *       notice, this list of conditions and the following disclaimer.
012 *
013 *     * Redistributions in binary form must reproduce the above
014 *       copyright notice, this list of conditions and the following
015 *       disclaimer in the documentation and/or other materials provided
016 *       with the distribution.
017 *
018 *     * Neither the name of the Technische Universität Berlin nor the
019 *       names of its contributors may be used to endorse or promote
020 *       products derived from this software without specific prior
021 *       written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035// --- END LICENSE BLOCK ---
036package org.jblas.benchmark;
037
038import java.io.PrintStream;
039import org.jblas.util.Logger;
040
041/**
042 * A simple command-line style benchmarking program.
043 * 
044 * <p>Benchmarks matrix-matrix multiplication, and compares to a 
045 * pure Java implementation</p>
046 *
047 * @author Mikio L. Braun
048 */
049public class Main {
050
051    static Benchmark[] multiplicationBenchmarks = {
052        new JavaDoubleMultiplicationBenchmark(),
053        new JavaFloatMultiplicationBenchmark(),
054        new NativeDoubleMultiplicationBenchmark(),
055        new NativeFloatMultiplicationBenchmark(),};
056
057    public static void printHelp() {
058        System.out.printf("Usage: benchmark [opts]%n"
059                + "%n"
060                + "with options:%n"
061                + "%n"
062                + "  --arch-flavor=value     overriding arch flavor (e.g. --arch-flavor=sse2)%n"
063                + "  --skip-java             don't run java benchmarks%n"
064                + "  --help                  show this help%n"
065                + "  --debug                 set config levels to debug%n"
066                + "%njblas version " + org.jblas.Info.VERSION + "%n");
067    }
068
069    public static void main(String[] args) {
070        int[] multiplicationSizes = {10, 100, 1000};
071        PrintStream out = System.out;
072
073        boolean skipJava = false;
074        boolean unrecognizedOptions = false;
075
076        Logger log = Logger.getLogger();
077
078        log.info("jblas version is " + org.jblas.Info.VERSION);
079
080        for (String arg : args) {
081            if (arg.startsWith("--")) {
082                int i = arg.indexOf('=');
083                String value = null;
084                if (i != -1) {
085                    value = arg.substring(i + 1);
086                    arg = arg.substring(0, i);
087                }
088
089                if (arg.equals("--arch-flavor")) {
090                    Logger.getLogger().info("Setting arch flavor to " + value);
091                    org.jblas.util.ArchFlavor.overrideArchFlavor(value);
092                } else if (arg.equals("--skip-java")) {
093                    skipJava = true;
094                } else if (arg.equals("--help")) {
095                    printHelp();
096                    return;
097                } else if (arg.equals("--debug")) {
098                    Logger.getLogger().setLevel(Logger.DEBUG);
099                } else {
100                    Logger.getLogger().warning("Unrecognized option \"" + arg + "\"");
101                    unrecognizedOptions = true;
102                }
103            }
104        }
105        if (unrecognizedOptions) {
106            return;
107        }
108
109        out.println("Simple benchmark for jblas");
110        out.println();
111
112        out.println("Running sanity benchmarks.");
113        out.println();
114        org.jblas.util.SanityChecks.main(args);
115        out.println();
116
117        out.println("Each benchmark will take about 5 seconds...");
118
119        for (Benchmark b : multiplicationBenchmarks) {
120            if (skipJava) {
121                if (b.getName().contains("Java")) {
122                    continue;
123                }
124            }
125
126            out.println();
127            out.println("Running benchmark \"" + b.getName() + "\".");
128            for (int n : multiplicationSizes) {
129                out.printf("n = %-5d: ", n);
130                out.flush();
131
132                BenchmarkResult result = b.run(n, 5.0);
133
134                result.printResult();
135            }
136        }
137    }
138}