Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.Benchmarks/3.3/Whetstone.cs @ 14722

Last change on this file since 14722 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.Benchmarks {
32  [Item("Whetstone", "Whetstone performance benchmark.")]
33  [StorableClass]
34  public sealed class Whetstone : Benchmark {
35    private long begin_time;
36    private long end_time;
37
38    private int ITERATIONS;
39    private int numberOfCycles;
40    private int cycleNo;
41    private double x1, x2, x3, x4, x, y, t, t1, t2;
42    private double[] z = new double[1];
43    private double[] e1 = new double[4];
44    private int i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
45
46    [StorableConstructor]
47    private Whetstone(bool deserializing) : base(deserializing) { }
48    private Whetstone(Whetstone original, Cloner cloner) : base(original, cloner) { }
49    public Whetstone() { }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new Whetstone(this, cloner);
53    }
54
55    // implementation based on Java version: www.aicas.com/download/Whetstone.java
56    public override void Run(CancellationToken cancellationToken, ResultCollection results) {
57      bool stopBenchmark = false;
58
59      ITERATIONS = 100; // ITERATIONS / 10 = Millions Whetstone instructions
60
61      numberOfCycles = 100;
62      int defaultNumberOfRuns = 10;
63      float elapsedTime = 0;
64      float meanTime = 0;
65      float rating = 0;
66      float meanRating = 0;
67      int intRating = 0;
68
69      long runNumber = 1;
70      Stopwatch sw = new Stopwatch();
71      sw.Start();
72
73      while (!stopBenchmark) {
74        elapsedTime = (float)(MainCalc() / 1000);
75        meanTime = meanTime + (elapsedTime * 1000 / numberOfCycles);
76        rating = (1000 * numberOfCycles) / elapsedTime;
77        meanRating = meanRating + rating;
78        intRating = (int)rating;
79        numberOfCycles += 10;
80
81        if (cancellationToken.IsCancellationRequested) {
82          throw new OperationCanceledException(cancellationToken);
83        }
84
85        if ((TimeLimit == null) || (TimeLimit.TotalMilliseconds == 0)) {
86          if (runNumber > defaultNumberOfRuns) {
87            stopBenchmark = true;
88          }
89        } else if (sw.Elapsed > TimeLimit) {
90          stopBenchmark = true;
91        }
92
93        runNumber++;
94      }
95      sw.Stop();
96      meanTime = meanTime / runNumber;
97      meanRating = meanRating / runNumber;
98      intRating = (int)meanRating;
99
100      results.Add(new Result("MWIPS", new IntValue(intRating / 1000)));
101    }
102
103    private double MainCalc() {
104      // initialize constants
105      t = 0.499975;
106      t1 = 0.50025;
107      t2 = 2.0;
108
109      // set values of module weights
110      n1 = 0 * ITERATIONS;
111      n2 = 12 * ITERATIONS;
112      n3 = 14 * ITERATIONS;
113      n4 = 345 * ITERATIONS;
114      n6 = 210 * ITERATIONS;
115      n7 = 32 * ITERATIONS;
116      n8 = 899 * ITERATIONS;
117      n9 = 616 * ITERATIONS;
118      n10 = 0 * ITERATIONS;
119      n11 = 93 * ITERATIONS;
120
121      begin_time = DateTime.Now.Ticks / 10000; // get ms
122
123      for (cycleNo = 1; cycleNo <= numberOfCycles; cycleNo++) {
124        /* MODULE 1: simple identifiers */
125        x1 = 1.0;
126        x2 = x3 = x4 = -1.0;
127        for (i = 1; i <= n1; i += 1) {
128          x1 = (x1 + x2 + x3 - x4) * t;
129          x2 = (x1 + x2 - x3 + x4) * t; // correction: x2 = ( x1 + x2 - x3 - x4 ) * t;
130          x3 = (x1 - x2 + x3 + x4) * t; // correction: x3 = ( x1 - x2 + x3 + x4 ) * t;
131          x4 = (-x1 + x2 + x3 + x4) * t;
132        }
133
134        /* MODULE 2: array elements */
135        e1[0] = 1.0;
136        e1[1] = e1[2] = e1[3] = -1.0;
137        for (i = 1; i <= n2; i += 1) {
138          e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t;
139          e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t;
140          e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t;
141          e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t;
142        }
143
144        /* MODULE 3: array as parameter */
145        for (i = 1; i <= n3; i += 1)
146          pa(e1);
147
148        /* MODULE 4: conditional jumps */
149        j = 1;
150        for (i = 1; i <= n4; i += 1) {
151          if (j == 1)
152            j = 2;
153          else
154            j = 3;
155          if (j > 2)
156            j = 0;
157          else
158            j = 1;
159          if (j < 1)
160            j = 1;
161          else
162            j = 0;
163        }
164
165        /* MODULE 5: omitted */
166
167        /* MODULE 6: integer arithmetic */
168        j = 1;
169        k = 2;
170        l = 3;
171        for (i = 1; i <= n6; i += 1) {
172          j = j * (k - j) * (l - k);
173          k = l * k - (l - j) * k;
174          l = (l - k) * (k + j);
175          e1[l - 2] = j + k + l; /* C arrays are zero based */
176          e1[k - 2] = j * k * l;
177        }
178
179        /* MODULE 7: trig. functions */
180        x = y = 0.5;
181        for (i = 1; i <= n7; i += 1) {
182          x = t * Math.Atan(t2 * Math.Sin(x) * Math.Cos(x) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
183          y = t * Math.Atan(t2 * Math.Sin(y) * Math.Cos(y) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
184        }
185
186        /* MODULE 8: procedure calls */
187        x = y = z[0] = 1.0;
188        for (i = 1; i <= n8; i += 1)
189          p3(x, y, z);
190
191        /* MODULE9: array references */
192        j = 0;
193        k = 1;
194        l = 2;
195        e1[0] = 1.0;
196        e1[1] = 2.0;
197        e1[2] = 3.0;
198        for (i = 1; i <= n9; i++)
199          p0();
200
201        /* MODULE10: integer arithmetic */
202        j = 2;
203        k = 3;
204        for (i = 1; i <= n10; i += 1) {
205          j = j + k;
206          k = j + k;
207          j = k - j;
208          k = k - j - j;
209        }
210
211        /* MODULE11: standard functions */
212        x = 0.75;
213        for (i = 1; i <= n11; i += 1)
214          x = Math.Sqrt(Math.Exp(Math.Log(x) / t1));
215      } /* for */
216
217      end_time = DateTime.Now.Ticks / 10000; // get ms
218
219      return (end_time - begin_time);
220    }
221
222    public void pa(double[] e) {
223      int j;
224      j = 0;
225      do {
226        e[0] = (e[0] + e[1] + e[2] - e[3]) * t;
227        e[1] = (e[0] + e[1] - e[2] + e[3]) * t;
228        e[2] = (e[0] - e[1] + e[2] + e[3]) * t;
229        e[3] = (-e[0] + e[1] + e[2] + e[3]) / t2;
230        j += 1;
231      }
232      while (j < 6);
233    }
234
235    public void p3(double x, double y, double[] z) {
236      x = t * (x + y);
237      y = t * (x + y);
238      z[0] = (x + y) / t2;
239    }
240
241    public void p0() {
242      e1[j] = e1[k];
243      e1[k] = e1[l];
244      e1[l] = e1[j];
245    }
246  }
247}
Note: See TracBrowser for help on using the repository browser.