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 ATLASDoubleMultiplicationBenchmark(),
055        new ATLASFloatMultiplicationBenchmark(),};
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    }
067
068    public static void main(String[] args) {
069        int[] multiplicationSizes = {10, 100, 1000};
070        PrintStream out = System.out;
071
072        boolean skipJava = false;
073        boolean unrecognizedOptions = false;
074
075        for (String arg : args) {
076            if (arg.startsWith("--")) {
077                int i = arg.indexOf('=');
078                String value = null;
079                if (i != -1) {
080                    value = arg.substring(i + 1);
081                    arg = arg.substring(0, i);
082                }
083
084                if (arg.equals("--arch-flavor")) {
085                    Logger.getLogger().info("Setting arch flavor to " + value);
086                    org.jblas.util.ArchFlavor.overrideArchFlavor(value);
087                } else if (arg.equals("--skip-java")) {
088                    skipJava = true;
089                } else if (arg.equals("--help")) {
090                    printHelp();
091                    return;
092                } else if (arg.equals("--debug")) {
093                    Logger.getLogger().setLevel(Logger.DEBUG);
094                } else {
095                    Logger.getLogger().warning("Unrecognized option \"" + arg + "\"");
096                    unrecognizedOptions = true;
097                }
098            }
099        }
100        if (unrecognizedOptions) {
101            return;
102        }
103
104        out.println("Simple benchmark for jblas");
105        out.println();
106
107        out.println("Running sanity benchmarks.");
108        out.println();
109        org.jblas.util.SanityChecks.main(args);
110        out.println();
111
112        out.println("Each benchmark will take about 5 seconds...");
113
114        for (Benchmark b : multiplicationBenchmarks) {
115            if (skipJava) {
116                if (b.getName().contains("Java")) {
117                    continue;
118                }
119            }
120
121            out.println();
122            out.println("Running benchmark \"" + b.getName() + "\".");
123            for (int n : multiplicationSizes) {
124                out.printf("n = %-5d: ", n);
125                out.flush();
126
127                BenchmarkResult result = b.run(n, 5.0);
128
129                result.printResult();
130            }
131        }
132    }
133}