jdk/src/sample/share/forkjoin/mergesort/MergeDemo.java
author emc
Wed, 05 Nov 2014 08:37:04 -0500
changeset 27386 784414cffd9a
parent 25859 3317bb8137f4
permissions -rw-r--r--
8035259: javac, incorrect shadowing of classes vs type parameters Summary: Cause javac to look at type variables first when resolving names in an extends/implements list Reviewed-by: mcimadamore, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9255
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     1
/*
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     3
 *
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     6
 * are met:
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     7
 *
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    10
 *
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    14
 *
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    18
 *
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    30
 */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9255
diff changeset
    40
9255
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    41
import java.util.Arrays;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    42
import java.util.Random;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    43
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    44
import static java.lang.Integer.parseInt;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    45
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    46
/**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    47
 * MergeExample is a class that runs a demo benchmark of the {@code ForkJoin} framework
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    48
 * by benchmarking a {@link MergeSort} algorithm that is implemented using
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    49
 * {@link java.util.concurrent.RecursiveAction}.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    50
 * The {@code ForkJoin} framework is setup with different parallelism levels
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    51
 * and the sort is executed with arrays of different sizes to see the
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    52
 * trade offs by using multiple threads for different sizes of the array.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    53
 */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    54
public class MergeDemo {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    55
    // Use a fixed seed to always get the same random values back
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    56
    private final Random random = new Random(759123751834L);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    57
    private static final int ITERATIONS = 10;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    58
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    59
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    60
     * Represents the formula {@code f(n) = start + (step * n)} for n = 0 & n < iterations
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    61
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    62
    private static class Range {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    63
        private final int start;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    64
        private final int step;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    65
        private final int iterations;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    66
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    67
        private Range(int start, int step, int iterations) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    68
            this.start = start;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    69
            this.step = step;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    70
            this.iterations = iterations;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    71
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    72
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    73
        /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    74
         * Parses start, step and iterations from args
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    75
         * @param args the string array containing the arguments
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    76
         * @param start which element to start the start argument from
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    77
         * @return the constructed range
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    78
         */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    79
        public static Range parse(String[] args, int start) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    80
            if (args.length < start + 3) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    81
                throw new IllegalArgumentException("Too few elements in array");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    82
            }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    83
            return new Range(parseInt(args[start]), parseInt(args[start + 1]), parseInt(args[start + 2]));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    84
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    85
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    86
        public int get(int iteration) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    87
            return start + (step * iteration);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    88
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    89
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    90
        public int getIterations() {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    91
            return iterations;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    92
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    93
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    94
        @Override
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    95
        public String toString() {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    96
            StringBuilder builder = new StringBuilder();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    97
            builder.append(start).append(" ").append(step).append(" ").append(iterations);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    98
            return builder.toString();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
    99
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   100
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   101
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   102
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   103
     * Wraps the different parameters that is used when running the MergeExample.
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   104
     * {@code sizes} represents the different array sizes
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   105
     * {@code parallelism} represents the different parallelism levels
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   106
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   107
    private static class Configuration {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   108
        private final Range sizes;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   109
        private final Range parallelism;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   110
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   111
        private final static Configuration defaultConfig = new Configuration(new Range(20000, 20000, 10),
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   112
                new Range(2, 2, 10));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   113
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   114
        private Configuration(Range sizes, Range parallelism) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   115
            this.sizes = sizes;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   116
            this.parallelism = parallelism;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   117
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   118
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   119
        /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   120
         * Parses the arguments and attempts to create a configuration containing the
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   121
         * parameters for creating the array sizes and parallelism sizes
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   122
         * @param args the input arguments
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   123
         * @return the configuration
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   124
         */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   125
        public static Configuration parse(String[] args) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   126
            if (args.length == 0) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   127
                return defaultConfig;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   128
            } else {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   129
                try {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   130
                    if (args.length == 6) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   131
                        return new Configuration(Range.parse(args, 0), Range.parse(args, 3));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   132
                    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   133
                } catch (NumberFormatException e) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   134
                    System.err.println("MergeExample: error: Argument was not a number.");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   135
                }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   136
                System.err.println("MergeExample <size start> <size step> <size steps> <parallel start> <parallel step>" +
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   137
                        " <parallel steps>");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   138
                System.err.println("example: MergeExample 20000 10000 3 1 1 4");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   139
                System.err.println("example: will run with arrays of sizes 20000, 30000, 40000" +
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   140
                        " and parallelism: 1, 2, 3, 4");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   141
                return null;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   142
            }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   143
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   144
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   145
        /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   146
         * Creates an array for reporting the test result time in
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   147
         * @return an array containing {@code sizes.iterations * parallelism.iterations} elements
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   148
         */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   149
        private long[][] createTimesArray() {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   150
            return new long[sizes.getIterations()][parallelism.getIterations()];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   151
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   152
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   153
        @Override
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   154
        public String toString() {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   155
            StringBuilder builder = new StringBuilder("");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   156
            if (this == defaultConfig) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   157
                builder.append("Default configuration. ");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   158
            }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   159
            builder.append("Running with parameters: ");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   160
            builder.append(sizes);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   161
            builder.append(" ");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   162
            builder.append(parallelism);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   163
            return builder.toString();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   164
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   165
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   166
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   167
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   168
     * Generates an array of {@code elements} random elements
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   169
     * @param elements the number of elements requested in the array
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   170
     * @return an array of {@code elements} random elements
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   171
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   172
    private int[] generateArray(int elements) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   173
        int[] array = new int[elements];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   174
        for (int i = 0; i < elements; ++i) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   175
            array[i] = random.nextInt();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   176
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   177
        return array;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   178
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   179
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   180
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   181
     * Runs the test
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   182
     * @param config contains the settings for the test
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   183
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   184
    private void run(Configuration config) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   185
        Range sizes = config.sizes;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   186
        Range parallelism = config.parallelism;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   187
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   188
        // Run a couple of sorts to make the JIT compile / optimize the code
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   189
        // which should produce somewhat more fair times
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   190
        warmup();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   191
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   192
        long[][] times = config.createTimesArray();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   193
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   194
        for (int size = 0; size < sizes.getIterations(); size++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   195
            runForSize(parallelism, sizes.get(size), times, size);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   196
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   197
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   198
        printResults(sizes, parallelism, times);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   199
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   200
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   201
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   202
     * Prints the results as a table
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   203
     * @param sizes the different sizes of the arrays
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   204
     * @param parallelism the different parallelism levels used
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   205
     * @param times the median times for the different sizes / parallelism
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   206
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   207
    private void printResults(Range sizes, Range parallelism, long[][] times) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   208
        System.out.println("Time in milliseconds. Y-axis: number of elements. X-axis parallelism used.");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   209
        long[] sums = new long[times[0].length];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   210
        System.out.format("%8s  ", "");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   211
        for (int i = 0; i < times[0].length; i++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   212
            System.out.format("%4d ", parallelism.get(i));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   213
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   214
        System.out.println("");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   215
        for (int size = 0; size < sizes.getIterations(); size++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   216
            System.out.format("%8d: ", sizes.get(size));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   217
            for (int i = 0; i < times[size].length; i++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   218
                sums[i] += times[size][i];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   219
                System.out.format("%4d ", times[size][i]);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   220
            }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   221
            System.out.println("");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   222
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   223
        System.out.format("%8s: ", "Total");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   224
        for (long sum : sums) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   225
            System.out.format("%4d ", sum);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   226
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   227
        System.out.println("");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   228
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   229
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   230
    private void runForSize(Range parallelism, int elements, long[][] times, int size) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   231
        for (int step = 0; step < parallelism.getIterations(); step++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   232
            long time = runForParallelism(ITERATIONS, elements, parallelism.get(step));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   233
            times[size][step] = time;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   234
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   235
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   236
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   237
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   238
     * Runs <i>iterations</i> number of test sorts of a random array of <i>element</i> length
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   239
     * @param iterations number of iterations
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   240
     * @param elements number of elements in the random array
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   241
     * @param parallelism parallelism for the ForkJoin framework
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   242
     * @return the median time of runs
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   243
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   244
    private long runForParallelism(int iterations, int elements, int parallelism) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   245
        MergeSort mergeSort = new MergeSort(parallelism);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   246
        long[] times = new long[iterations];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   247
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   248
        for (int i = 0; i < iterations; i++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   249
            // Suggest the VM to run a garbage collection to reduce the risk of getting one
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   250
            // while running the test run
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   251
            System.gc();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   252
            long start = System.currentTimeMillis();
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   253
            mergeSort.sort(generateArray(elements));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   254
            times[i] = System.currentTimeMillis() - start;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   255
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   256
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   257
        return medianValue(times);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   258
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   259
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   260
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   261
     * Calculates the median value of the array
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   262
     * @param times array of times
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   263
     * @return the median value
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   264
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   265
    private long medianValue(long[] times) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   266
        if (times.length == 0) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   267
            throw new IllegalArgumentException("Empty array");
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   268
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   269
        // Make a copy of times to avoid having side effects on the parameter value
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   270
        Arrays.sort(times.clone());
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   271
        long median = times[times.length / 2];
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   272
        if (times.length > 1 && times.length % 2 != 0) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   273
            median = (median + times[times.length / 2 + 1]) / 2;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   274
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   275
        return median;
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   276
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   277
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   278
    /**
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   279
     * Generates 1000 arrays of 1000 elements and sorts them as a warmup
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   280
     */
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   281
    private void warmup() {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   282
        MergeSort mergeSort = new MergeSort(Runtime.getRuntime().availableProcessors());
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   283
        for (int i = 0; i < 1000; i++) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   284
            mergeSort.sort(generateArray(1000));
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   285
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   286
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   287
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   288
    public static void main(String[] args) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   289
        Configuration configuration = Configuration.parse(args);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   290
        if (configuration == null) {
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   291
            System.exit(1);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   292
        }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   293
        System.out.println(configuration);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   294
        new MergeDemo().run(configuration);
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   295
    }
c7df2225fecc 7026304: Fork-Join sample
rbackman
parents:
diff changeset
   296
}