source: branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks/3.3/WhetstoneAlgorithm.cs @ 6948

Last change on this file since 6948 was 6948, checked in by spimming, 11 years ago

#1659:

  • restructuring of the benchmarking algorithms
  • common interface for benchmarks
  • 'benchmark' class discovers benchmarking algorithms
  • added 'TimeLimit' and 'ChunkSize' parameters to benchmark
File size: 8.9 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 Algorithm", "Whetstone benchmarking algorithm.")]
34  [StorableClass]
35  public class WhetstoneBenchmark : IBenchmark {
36    [Storable]
37    private byte[][] chunk;
38
39    private TimeSpan timeLimit;
40
41    private bool stopBenchmark;
42
43    private CancellationToken cancellationToken;
44
45    #region Benchmark Fields
46
47    private long begin_time;
48    private long end_time;
49
50    private int ITERATIONS;
51    private int numberOfCycles;
52    private int cycleNo;
53    private double x1, x2, x3, x4, x, y, t, t1, t2;
54    private double[] z = new double[1];
55    private double[] e1 = new double[4];
56    private int i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
57
58    #endregion
59
60    #region Properties
61
62    public byte[][] ChunkData {
63      get { return chunk; }
64      set { chunk = value; }
65    }
66
67    public TimeSpan TimeLimit {
68      get { return timeLimit; }
69      set { timeLimit = value; }
70    }
71
72    public string ItemName {
73      get { return ItemAttribute.GetName(this.GetType()); }
74    }
75
76    public string ItemDescription {
77      get { return ItemAttribute.GetDescription(this.GetType()); }
78    }
79
80    public Version ItemVersion {
81      get { return ItemAttribute.GetVersion(this.GetType()); }
82    }
83
84    public Image ItemImage {
85      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
86    }
87
88    #endregion
89
90    #region Costructors
91
92    public WhetstoneBenchmark() {
93    }
94
95    private 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.