001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is JTransforms.
015 *
016 * The Initial Developer of the Original Code is
017 * Piotr Wendykier, Emory University.
018 * Portions created by the Initial Developer are Copyright (C) 2007-2009
019 * the Initial Developer. All Rights Reserved.
020 *
021 * Alternatively, the contents of this file may be used under the terms of
022 * either the GNU General Public License Version 2 or later (the "GPL"), or
023 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
024 * in which case the provisions of the GPL or the LGPL are applicable instead
025 * of those above. If you wish to allow use of your version of this file only
026 * under the terms of either the GPL or the LGPL, and not to allow others to
027 * use your version of this file under the terms of the MPL, indicate your
028 * decision by deleting the provisions above and replace them with the notice
029 * and other provisions required by the GPL or the LGPL. If you do not delete
030 * the provisions above, a recipient may use your version of this file under
031 * the terms of any one of the MPL, the GPL or the LGPL.
032 *
033 * ***** END LICENSE BLOCK ***** */
034
035package edu.emory.mathcs.jtransforms.dst;
036
037import java.util.Arrays;
038
039import edu.emory.mathcs.utils.ConcurrencyUtils;
040import edu.emory.mathcs.utils.IOUtils;
041
042/**
043 * Benchmark of single precision DST's
044 * 
045 * @author Piotr Wendykier (piotr.wendykier@gmail.com)
046 * 
047 */
048public class BenchmarkFloatDST {
049
050    private static int nthread = 8;
051
052    private static int niter = 200;
053
054    private static int nsize = 16;
055
056    private static int threadsBegin2D = 65636;
057
058    private static int threadsBegin3D = 65636;
059
060    private static boolean doWarmup = true;
061
062    private static int[] sizes1D = new int[] { 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 10368, 27000, 75600, 165375, 362880, 1562500, 3211264, 6250000 };
063
064    private static int[] sizes2D = new int[] { 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 260, 520, 1050, 1458, 1960, 2916, 4116, 5832 };
065
066    private static int[] sizes3D = new int[] { 8, 16, 32, 64, 128, 256, 512, 1024, 5, 17, 30, 95, 180, 270, 324, 420 };
067
068    private static boolean doScaling = false;
069
070    private BenchmarkFloatDST() {
071
072    }
073
074    public static void parseArguments(String[] args) {
075        if (args.length > 0) {
076            nthread = Integer.parseInt(args[0]);
077            threadsBegin2D = Integer.parseInt(args[1]);
078            threadsBegin3D = Integer.parseInt(args[2]);
079            niter = Integer.parseInt(args[3]);
080            doWarmup = Boolean.parseBoolean(args[4]);
081            doScaling = Boolean.parseBoolean(args[5]);
082            nsize = Integer.parseInt(args[6]);
083            sizes1D = new int[nsize];
084            sizes2D = new int[nsize];
085            sizes3D = new int[nsize];
086            for (int i = 0; i < nsize; i++) {
087                sizes1D[i] = Integer.parseInt(args[7 + i]);
088            }
089            for (int i = 0; i < nsize; i++) {
090                sizes2D[i] = Integer.parseInt(args[7 + nsize + i]);
091            }
092            for (int i = 0; i < nsize; i++) {
093                sizes3D[i] = Integer.parseInt(args[7 + nsize + nsize + i]);
094            }
095        } else {
096            System.out.println("Default settings are used.");
097        }
098        ConcurrencyUtils.setNumberOfThreads(nthread);
099        ConcurrencyUtils.setThreadsBeginN_2D(threadsBegin2D);
100        ConcurrencyUtils.setThreadsBeginN_3D(threadsBegin3D);
101        System.out.println("nthred = " + nthread);
102        System.out.println("threadsBegin2D = " + threadsBegin2D);
103        System.out.println("threadsBegin3D = " + threadsBegin3D);
104        System.out.println("niter = " + niter);
105        System.out.println("doWarmup = " + doWarmup);
106        System.out.println("doScaling = " + doScaling);
107        System.out.println("nsize = " + nsize);
108        System.out.println("sizes1D[] = " + Arrays.toString(sizes1D));
109        System.out.println("sizes2D[] = " + Arrays.toString(sizes2D));
110        System.out.println("sizes3D[] = " + Arrays.toString(sizes3D));
111    }
112
113    public static void benchmarkForward_1D() {
114        double[] times = new double[nsize];
115        float[] x;
116        for (int i = 0; i < nsize; i++) {
117            System.out.println("Forward DST 1D of size " + sizes1D[i]);
118            FloatDST_1D dst = new FloatDST_1D(sizes1D[i]);
119            x = new float[sizes1D[i]];
120            if (doWarmup) { // call the transform twice to warm up
121                IOUtils.fillMatrix_1D(sizes1D[i], x);
122                dst.forward(x, doScaling);
123                IOUtils.fillMatrix_1D(sizes1D[i], x);
124                dst.forward(x, doScaling);
125            }
126            float av_time = 0;
127            long elapsedTime = 0;
128            for (int j = 0; j < niter; j++) {
129                IOUtils.fillMatrix_1D(sizes1D[i], x);
130                elapsedTime = System.nanoTime();
131                dst.forward(x, doScaling);
132                elapsedTime = System.nanoTime() - elapsedTime;
133                av_time = av_time + elapsedTime;
134            }
135            times[i] = (double) av_time / 1000000.0 / (float) niter;
136            System.out.println("Average execution time: " + String.format("%.2f", av_time / 1000000.0 / (float) niter) + " msec");
137            x = null;
138            dst = null;
139            System.gc();
140            ConcurrencyUtils.sleep(5000);
141        }
142        IOUtils.writeFFTBenchmarkResultsToFile("benchmarkFloatForwardDST_1D.txt", nthread, niter, doWarmup, doScaling, sizes1D, times);
143    }
144
145    public static void benchmarkForward_2D_input_1D() {
146        double[] times = new double[nsize];
147        float[] x;
148        for (int i = 0; i < nsize; i++) {
149            System.out.println("Forward DST 2D (input 1D) of size " + sizes2D[i] + " x " + sizes2D[i]);
150            FloatDST_2D dst2 = new FloatDST_2D(sizes2D[i], sizes2D[i]);
151            x = new float[sizes2D[i] * sizes2D[i]];
152            if (doWarmup) { // call the transform twice to warm up
153                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
154                dst2.forward(x, doScaling);
155                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
156                dst2.forward(x, doScaling);
157            }
158            float av_time = 0;
159            long elapsedTime = 0;
160            for (int j = 0; j < niter; j++) {
161                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
162                elapsedTime = System.nanoTime();
163                dst2.forward(x, doScaling);
164                elapsedTime = System.nanoTime() - elapsedTime;
165                av_time = av_time + elapsedTime;
166            }
167            times[i] = (double) av_time / 1000000.0 / (float) niter;
168            System.out.println("Average execution time: " + String.format("%.2f", av_time / 1000000.0 / (float) niter) + " msec");
169            x = null;
170            dst2 = null;
171            System.gc();
172            ConcurrencyUtils.sleep(5000);
173        }
174        IOUtils.writeFFTBenchmarkResultsToFile("benchmarkFloatForwardDST_2D_input_1D.txt", nthread, niter, doWarmup, doScaling, sizes2D, times);
175
176    }
177
178    public static void benchmarkForward_2D_input_2D() {
179        double[] times = new double[nsize];
180        float[][] x;
181        for (int i = 0; i < nsize; i++) {
182            System.out.println("Forward DST 2D (input 2D) of size " + sizes2D[i] + " x " + sizes2D[i]);
183            FloatDST_2D dst2 = new FloatDST_2D(sizes2D[i], sizes2D[i]);
184            x = new float[sizes2D[i]][sizes2D[i]];
185            if (doWarmup) { // call the transform twice to warm up
186                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
187                dst2.forward(x, doScaling);
188                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
189                dst2.forward(x, doScaling);
190            }
191            float av_time = 0;
192            long elapsedTime = 0;
193            for (int j = 0; j < niter; j++) {
194                IOUtils.fillMatrix_2D(sizes2D[i], sizes2D[i], x);
195                elapsedTime = System.nanoTime();
196                dst2.forward(x, doScaling);
197                elapsedTime = System.nanoTime() - elapsedTime;
198                av_time = av_time + elapsedTime;
199            }
200            times[i] = (double) av_time / 1000000.0 / (float) niter;
201            System.out.println("Average execution time: " + String.format("%.2f", av_time / 1000000.0 / (float) niter) + " msec");
202            x = null;
203            dst2 = null;
204            System.gc();
205            ConcurrencyUtils.sleep(5000);
206        }
207        IOUtils.writeFFTBenchmarkResultsToFile("benchmarkFloatForwardDST_2D_input_2D.txt", nthread, niter, doWarmup, doScaling, sizes2D, times);
208
209    }
210
211    public static void benchmarkForward_3D_input_1D() {
212        double[] times = new double[nsize];
213        float[] x;
214        for (int i = 0; i < nsize; i++) {
215            System.out.println("Forward DST 3D (input 1D) of size " + sizes3D[i] + " x " + sizes3D[i] + " x " + sizes3D[i]);
216            FloatDST_3D dst3 = new FloatDST_3D(sizes3D[i], sizes3D[i], sizes3D[i]);
217            x = new float[sizes3D[i] * sizes3D[i] * sizes3D[i]];
218            if (doWarmup) { // call the transform twice to warm up
219                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
220                dst3.forward(x, doScaling);
221                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
222                dst3.forward(x, doScaling);
223            }
224            float av_time = 0;
225            long elapsedTime = 0;
226            for (int j = 0; j < niter; j++) {
227                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
228                elapsedTime = System.nanoTime();
229                dst3.forward(x, doScaling);
230                elapsedTime = System.nanoTime() - elapsedTime;
231                av_time = av_time + elapsedTime;
232            }
233            times[i] = (double) av_time / 1000000.0 / (float) niter;
234            System.out.println("Average execution time: " + String.format("%.2f", av_time / 1000000.0 / (float) niter) + " msec");
235            x = null;
236            dst3 = null;
237            System.gc();
238            ConcurrencyUtils.sleep(5000);
239        }
240        IOUtils.writeFFTBenchmarkResultsToFile("benchmarkFloatForwardDST_3D_input_1D.txt", nthread, niter, doWarmup, doScaling, sizes3D, times);
241
242    }
243
244    public static void benchmarkForward_3D_input_3D() {
245        double[] times = new double[nsize];
246        float[][][] x;
247        for (int i = 0; i < nsize; i++) {
248            System.out.println("Forward DST 3D (input 3D) of size " + sizes3D[i] + " x " + sizes3D[i] + " x " + sizes3D[i]);
249            FloatDST_3D dst3 = new FloatDST_3D(sizes3D[i], sizes3D[i], sizes3D[i]);
250            x = new float[sizes3D[i]][sizes3D[i]][sizes3D[i]];
251            if (doWarmup) { // call the transform twice to warm up
252                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
253                dst3.forward(x, doScaling);
254                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
255                dst3.forward(x, doScaling);
256            }
257            float av_time = 0;
258            long elapsedTime = 0;
259            for (int j = 0; j < niter; j++) {
260                IOUtils.fillMatrix_3D(sizes3D[i], sizes3D[i], sizes3D[i], x);
261                elapsedTime = System.nanoTime();
262                dst3.forward(x, doScaling);
263                elapsedTime = System.nanoTime() - elapsedTime;
264                av_time = av_time + elapsedTime;
265            }
266            times[i] = (double) av_time / 1000000.0 / (double) (niter);
267            System.out.println("Average execution time: " + String.format("%.2f", av_time / 1000000.0 / (double) (niter)) + " msec");
268            x = null;
269            dst3 = null;
270            System.gc();
271            ConcurrencyUtils.sleep(5000);
272        }
273        IOUtils.writeFFTBenchmarkResultsToFile("benchmarkFloatForwardDST_3D_input_3D.txt", nthread, niter, doWarmup, doScaling, sizes3D, times);
274    }
275
276    public static void main(String[] args) {
277        parseArguments(args);
278        benchmarkForward_1D();
279        benchmarkForward_2D_input_1D();
280        benchmarkForward_2D_input_2D();
281        benchmarkForward_3D_input_1D();
282        benchmarkForward_3D_input_3D();
283        System.exit(0);
284
285    }
286}