Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.Benchmarks/3.3/WhetstoneBenchmark.cs @ 7246

Last change on this file since 7246 was 7246, checked in by swagner, 12 years ago

Changes due to review of benchmark algorithms (#1659):

  • renamed Benchmark to BenchmarkAlgorithm and BenchmarkView to BenchmarkAlgorithmView
  • removed unnecessary assembly references
  • removed IBenchmarkView as the user should be informed that there is no specific view for benchmarks
  • adapted descriptions of plugins and assemblies
  • adapted assembly Guids
  • sealed BenchmarkAlgorithm
  • adapted item names and item descriptions
  • adapted code formatting
File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Diagnostics;
24using System.Drawing;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.Benchmarks {
33  [Item("Whetstone Benchmark", "Whetstone performance benchmark algorithm.")]
34  [StorableClass]
35  public class WhetstoneBenchmark : IBenchmark {
36    [Storable]
37    private byte[][] chunk;
38    public byte[][] ChunkData {
39      get { return chunk; }
40      set { chunk = value; }
41    }
42
43    [Storable]
44    private TimeSpan timeLimit;
45    public TimeSpan TimeLimit {
46      get { return timeLimit; }
47      set { timeLimit = value; }
48    }
49
50    private bool stopBenchmark;
51
52    private CancellationToken cancellationToken;
53
54    public string ItemName {
55      get { return ItemAttribute.GetName(this.GetType()); }
56    }
57
58    public string ItemDescription {
59      get { return ItemAttribute.GetDescription(this.GetType()); }
60    }
61
62    public Version ItemVersion {
63      get { return ItemAttribute.GetVersion(this.GetType()); }
64    }
65
66    public static Image StaticItemImage {
67      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
68    }
69    public Image ItemImage {
70      get { return ItemAttribute.GetImage(this.GetType()); }
71    }
72
73    #region Benchmark Fields
74
75    private long begin_time;
76    private long end_time;
77
78    private int ITERATIONS;
79    private int numberOfCycles;
80    private int cycleNo;
81    private double x1, x2, x3, x4, x, y, t, t1, t2;
82    private double[] z = new double[1];
83    private double[] e1 = new double[4];
84    private int i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
85
86    #endregion
87
88    #region Costructors
89
90    [StorableConstructor]
91    public WhetstoneBenchmark(bool deserializing) { }
92
93    public WhetstoneBenchmark() { }
94
95    protected WhetstoneBenchmark(WhetstoneBenchmark original, Cloner cloner) {
96      cloner.RegisterClonedObject(original, this);
97    }
98
99    #endregion
100
101    #region Whetstone Benchmark
102    // implementation based on Java version: www.aicas.com/download/Whetstone.java
103
104    public void Run(CancellationToken token, ResultCollection results) {
105      cancellationToken = token;
106      stopBenchmark = false;
107
108      ITERATIONS = 100; // ITERATIONS / 10 = Millions Whetstone instructions
109
110      numberOfCycles = 100;
111      int defaultNumberOfRuns = 10;
112      float elapsedTime = 0;
113      float meanTime = 0;
114      float rating = 0;
115      float meanRating = 0;
116      int intRating = 0;
117
118      long runNumber = 1;
119      Stopwatch sw = new Stopwatch();
120      sw.Start();
121
122      while (!stopBenchmark) {
123        elapsedTime = (float)(MainCalc() / 1000);
124        meanTime = meanTime + (elapsedTime * 1000 / numberOfCycles);
125        rating = (1000 * numberOfCycles) / elapsedTime;
126        meanRating = meanRating + rating;
127        intRating = (int)rating;
128        numberOfCycles += 10;
129
130        if (cancellationToken.IsCancellationRequested) {
131          throw new OperationCanceledException(cancellationToken);
132        }
133
134        if ((timeLimit == null) || (timeLimit.TotalMilliseconds == 0)) {
135          if (runNumber > defaultNumberOfRuns) {
136            stopBenchmark = true;
137          }
138        } else if (sw.Elapsed > timeLimit) {
139          stopBenchmark = true;
140        }
141
142        runNumber++;
143      }
144      sw.Stop();
145      meanTime = meanTime / runNumber;
146      meanRating = meanRating / runNumber;
147      intRating = (int)meanRating;
148
149      results.Add(new Result("MWIPS", new IntValue(intRating / 1000)));
150    }
151
152    private double MainCalc() {
153      // initialize constants
154      t = 0.499975;
155      t1 = 0.50025;
156      t2 = 2.0;
157
158      // set values of module weights
159      n1 = 0 * ITERATIONS;
160      n2 = 12 * ITERATIONS;
161      n3 = 14 * ITERATIONS;
162      n4 = 345 * ITERATIONS;
163      n6 = 210 * ITERATIONS;
164      n7 = 32 * ITERATIONS;
165      n8 = 899 * ITERATIONS;
166      n9 = 616 * ITERATIONS;
167      n10 = 0 * ITERATIONS;
168      n11 = 93 * ITERATIONS;
169
170      begin_time = DateTime.Now.Ticks / 10000; // get ms
171
172      for (cycleNo = 1; cycleNo <= numberOfCycles; cycleNo++) {
173        /* MODULE 1: simple identifiers */
174        x1 = 1.0;
175        x2 = x3 = x4 = -1.0;
176        for (i = 1; i <= n1; i += 1) {
177          x1 = (x1 + x2 + x3 - x4) * t;
178          x2 = (x1 + x2 - x3 + x4) * t; // correction: x2 = ( x1 + x2 - x3 - x4 ) * t;
179          x3 = (x1 - x2 + x3 + x4) * t; // correction: x3 = ( x1 - x2 + x3 + x4 ) * t;
180          x4 = (-x1 + x2 + x3 + x4) * t;
181        }
182
183        /* MODULE 2: array elements */
184        e1[0] = 1.0;
185        e1[1] = e1[2] = e1[3] = -1.0;
186        for (i = 1; i <= n2; i += 1) {
187          e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t;
188          e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t;
189          e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t;
190          e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t;
191        }
192
193        /* MODULE 3: array as parameter */
194        for (i = 1; i <= n3; i += 1)
195          pa(e1);
196
197        /* MODULE 4: conditional jumps */
198        j = 1;
199        for (i = 1; i <= n4; i += 1) {
200          if (j == 1)
201            j = 2;
202          else
203            j = 3;
204          if (j > 2)
205            j = 0;
206          else
207            j = 1;
208          if (j < 1)
209            j = 1;
210          else
211            j = 0;
212        }
213
214        /* MODULE 5: omitted */
215
216        /* MODULE 6: integer arithmetic */
217        j = 1;
218        k = 2;
219        l = 3;
220        for (i = 1; i <= n6; i += 1) {
221          j = j * (k - j) * (l - k);
222          k = l * k - (l - j) * k;
223          l = (l - k) * (k + j);
224          e1[l - 2] = j + k + l; /* C arrays are zero based */
225          e1[k - 2] = j * k * l;
226        }
227
228        /* MODULE 7: trig. functions */
229        x = y = 0.5;
230        for (i = 1; i <= n7; i += 1) {
231          x = t * Math.Atan(t2 * Math.Sin(x) * Math.Cos(x) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
232          y = t * Math.Atan(t2 * Math.Sin(y) * Math.Cos(y) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
233        }
234
235        /* MODULE 8: procedure calls */
236        x = y = z[0] = 1.0;
237        for (i = 1; i <= n8; i += 1)
238          p3(x, y, z);
239
240        /* MODULE9: array references */
241        j = 0;
242        k = 1;
243        l = 2;
244        e1[0] = 1.0;
245        e1[1] = 2.0;
246        e1[2] = 3.0;
247        for (i = 1; i <= n9; i++)
248          p0();
249
250        /* MODULE10: integer arithmetic */
251        j = 2;
252        k = 3;
253        for (i = 1; i <= n10; i += 1) {
254          j = j + k;
255          k = j + k;
256          j = k - j;
257          k = k - j - j;
258        }
259
260        /* MODULE11: standard functions */
261        x = 0.75;
262        for (i = 1; i <= n11; i += 1)
263          x = Math.Sqrt(Math.Exp(Math.Log(x) / t1));
264      } /* for */
265
266      end_time = DateTime.Now.Ticks / 10000; // get ms
267
268      return (end_time - begin_time);
269    }
270
271    public void pa(double[] e) {
272      int j;
273      j = 0;
274      do {
275        e[0] = (e[0] + e[1] + e[2] - e[3]) * t;
276        e[1] = (e[0] + e[1] - e[2] + e[3]) * t;
277        e[2] = (e[0] - e[1] + e[2] + e[3]) * t;
278        e[3] = (-e[0] + e[1] + e[2] + e[3]) / t2;
279        j += 1;
280      }
281      while (j < 6);
282    }
283
284    public void p3(double x, double y, double[] z) {
285      x = t * (x + y);
286      y = t * (x + y);
287      z[0] = (x + y) / t2;
288    }
289
290    public void p0() {
291      e1[j] = e1[k];
292      e1[k] = e1[l];
293      e1[l] = e1[j];
294    }
295
296    #endregion
297
298    #region Clone
299
300    public IDeepCloneable Clone(Cloner cloner) {
301      return new WhetstoneBenchmark(this, cloner);
302    }
303
304    public object Clone() {
305      return Clone(new Cloner());
306    }
307
308    #endregion
309
310    #region Events
311
312    public event EventHandler ItemImageChanged;
313    protected virtual void OnItemImageChanged() {
314      EventHandler handler = ItemImageChanged;
315      if (handler != null) handler(this, EventArgs.Empty);
316    }
317
318    public event EventHandler ToStringChanged;
319    protected virtual void OnToStringChanged() {
320      EventHandler handler = ToStringChanged;
321      if (handler != null) handler(this, EventArgs.Empty);
322    }
323
324    #endregion
325  }
326}
Note: See TracBrowser for help on using the repository browser.