Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks/3.3/Whetstone.cs @ 6934

Last change on this file since 6934 was 6934, checked in by ascheibe, 11 years ago

on behalf of spimming:
#1659

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