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