source: branches/Benchmarking/sources/HeuristicLab.Algorithms.Benchmarks/3.3/DhrystoneBenchmark.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: 11.2 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("Dhrystone Algorithm", "Dhrystone benchmarking algorithm.")]
34  [StorableClass]
35  public class DhrystoneAlgorithm : 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 const int Ident_1 = 0;
48    private const int Ident_2 = 1;
49    private const int Ident_3 = 2;
50    private const int Ident_4 = 3;
51    private const int Ident_5 = 4;
52
53    private Record_Type Record_Glob;
54    private Record_Type Next_Record_Glob;
55
56    private int Int_Glob;
57    private Boolean Bool_Glob;
58
59    private char Char_Glob_1;
60    private char Char_Glob_2;
61
62    private int[] Array_Glob_1 = new int[128];
63    private int[][] Array_Glob_2 = new int[128][];
64
65    private Record_Type First_Record = new Record_Type();
66    private Record_Type Second_Record = new Record_Type();
67
68    private long Default_Number_Of_Runs = 10000000;
69
70    #endregion
71
72    #region Properties
73
74    public byte[][] ChunkData {
75      get { return chunk; }
76      set { chunk = value; }
77    }
78
79    public TimeSpan TimeLimit {
80      get { return timeLimit; }
81      set { timeLimit = value; }
82    }
83
84    public string ItemName {
85      get { return ItemAttribute.GetName(this.GetType()); }
86    }
87
88    public string ItemDescription {
89      get { return ItemAttribute.GetDescription(this.GetType()); }
90    }
91
92    public Version ItemVersion {
93      get { return ItemAttribute.GetVersion(this.GetType()); }
94    }
95
96    public Image ItemImage {
97      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
98    }
99
100    #endregion
101
102    #region Costructors
103
104    public DhrystoneAlgorithm() {
105    }
106
107    private DhrystoneAlgorithm(DhrystoneAlgorithm original, Cloner cloner) {
108      cloner.RegisterClonedObject(original, this);
109    }
110
111    #endregion
112
113    #region Benchmark Methods
114    // implementation based on Java version: http://www.okayan.jp/DhrystoneApplet/dhry_src.jar
115
116    public void Run(CancellationToken token, ResultCollection results) {
117      cancellationToken = token;
118      stopBenchmark = false;
119
120      int Int_Loc_1;
121      int Int_Loc_2;
122      int Int_Loc_3;
123
124      int[] Int_Loc_1_Ref = new int[1];
125      int[] Int_Loc_3_Ref = new int[1];
126
127      char Char_Index;
128      int[] Enum_Loc = new int[1];
129
130      string String_Loc_1;
131      string String_Loc_2;
132
133      long total_time;
134
135      long Run_Index;
136
137      Next_Record_Glob = Second_Record;
138      Record_Glob = First_Record;
139
140      Record_Glob.Record_Comp = Next_Record_Glob;
141      Record_Glob.Discr = Ident_1;
142      Record_Glob.Enum_Comp = Ident_3;
143      Record_Glob.Int_Comp = 40;
144      Record_Glob.String_Comp = "DHRYSTONE PROGRAM, SOME STRING";
145      String_Loc_1 = "DHRYSTONE PROGRAM, 1'ST STRING";
146
147      for (int i = 0; i < 128; i++) {
148        Array_Glob_2[i] = new int[128];
149      }
150
151      Stopwatch sw = new Stopwatch();
152      sw.Start();
153
154      Run_Index = 1;
155      while (!stopBenchmark) {
156        Proc_5();
157        Proc_4();
158
159        Int_Loc_1 = 2;
160        Int_Loc_2 = 3;
161
162        String_Loc_2 = "DHRYSTONE PROGRAM, 2'ND STRING";
163
164        Enum_Loc[0] = Ident_2;
165        Bool_Glob = !Func_2(String_Loc_1, String_Loc_2);
166
167        while (Int_Loc_1 < Int_Loc_2) {
168          Int_Loc_3_Ref[0] = 5 * Int_Loc_1 - Int_Loc_2;
169          Proc_7(Int_Loc_1, Int_Loc_2, Int_Loc_3_Ref);
170          Int_Loc_1 += 1;
171        }
172
173        Int_Loc_3 = Int_Loc_3_Ref[0];
174        Proc_8(Array_Glob_1, Array_Glob_2, Int_Loc_1, Int_Loc_3);
175        Proc_1(Record_Glob);
176
177        for (Char_Index = 'A'; Char_Index <= Char_Glob_2; ++Char_Index) {
178          if (Enum_Loc[0] == Func_1(Char_Index, 'C'))
179            Proc_6(Ident_1, Enum_Loc);
180        }
181
182        Int_Loc_3 = Int_Loc_2 * Int_Loc_1;
183        Int_Loc_2 = Int_Loc_3 / Int_Loc_1;
184        Int_Loc_2 = 7 * (Int_Loc_3 - Int_Loc_2) - Int_Loc_1;
185
186        Int_Loc_1_Ref[0] = Int_Loc_1;
187        Proc_2(Int_Loc_1_Ref);
188        Int_Loc_1 = Int_Loc_1_Ref[0];
189
190        if (cancellationToken.IsCancellationRequested) {
191          throw new OperationCanceledException(cancellationToken);
192        }
193
194        if ((timeLimit == null) || (timeLimit.TotalMilliseconds == 0)) {
195          if (Run_Index > Default_Number_Of_Runs) {
196            stopBenchmark = true;
197          }
198        } else if (sw.Elapsed > timeLimit) {
199          stopBenchmark = true;
200        }
201
202        Run_Index++;
203      }
204
205      sw.Stop();
206      total_time = sw.ElapsedMilliseconds;
207
208      results.Add(new Result("DIPS", new DoubleValue(Run_Index * 1000 / total_time)));
209
210    }
211
212    private int Func_1(char Char_Par_1_Val, char Char_Par_2_Val) {
213      char Char_Loc_1;
214      char Char_Loc_2;
215
216      Char_Loc_1 = Char_Par_1_Val;
217      Char_Loc_2 = Char_Loc_1;
218      if (Char_Loc_2 != Char_Par_2_Val)
219        return Ident_1;
220      else
221        return Ident_2;
222    }
223
224    private bool Func_2(string String_Par_1_Ref, string String_Par_2_Ref) {
225      int Int_Loc;
226      char Char_Loc = '\0';
227
228      Int_Loc = 2;
229
230      while (Int_Loc <= 2) {
231        if (Func_1(String_Par_1_Ref[Int_Loc], String_Par_2_Ref[Int_Loc + 1]) == Ident_1) {
232          Char_Loc = 'A';
233          Int_Loc += 1;
234        }
235      }
236      if (Char_Loc >= 'W' && Char_Loc < 'z')
237        Int_Loc = 7;
238      if (Char_Loc == 'X')
239        return true;
240      else {
241        if (String_Par_1_Ref.CompareTo(String_Par_2_Ref) > 0) {
242          Int_Loc += 7;
243          return true;
244        } else
245          return false;
246      }
247    }
248
249    private bool Func_3(int Enum_Par_Val) {
250      int Enum_Loc;
251
252      Enum_Loc = Enum_Par_Val;
253      if (Enum_Loc == Ident_3)
254        return true;
255      else
256        return false;
257    }
258
259    private void Proc_1(Record_Type Pointer_Par_Val) {
260      Record_Type Next_Record = Pointer_Par_Val.Record_Comp;
261
262      Pointer_Par_Val.Record_Comp = Record_Glob;
263
264      Pointer_Par_Val.Int_Comp = 5;
265
266      Next_Record.Int_Comp = Pointer_Par_Val.Int_Comp;
267      Next_Record.Record_Comp = Pointer_Par_Val.Record_Comp;
268      Proc_3(Next_Record.Record_Comp);
269
270      int[] Int_Ref = new int[1];
271
272      if (Next_Record.Discr == Ident_1) {
273        Next_Record.Int_Comp = 6;
274        Int_Ref[0] = Next_Record.Enum_Comp;
275        Proc_6(Pointer_Par_Val.Enum_Comp, Int_Ref);
276        Next_Record.Enum_Comp = Int_Ref[0];
277        Next_Record.Record_Comp = Record_Glob.Record_Comp;
278        Int_Ref[0] = Next_Record.Int_Comp;
279        Proc_7(Next_Record.Int_Comp, 10, Int_Ref);
280        Next_Record.Int_Comp = Int_Ref[0];
281      } else
282        Pointer_Par_Val = Pointer_Par_Val.Record_Comp;
283    }
284
285    private void Proc_2(int[] Int_Par_Ref) {
286      int Int_Loc;
287      int Enum_Loc;
288
289      Int_Loc = Int_Par_Ref[0] + 10;
290      Enum_Loc = 0;
291
292      do
293        if (Char_Glob_1 == 'A') {
294          Int_Loc -= 1;
295          Int_Par_Ref[0] = Int_Loc - Int_Glob;
296          Enum_Loc = Ident_1;
297        }
298      while (Enum_Loc != Ident_1);
299    }
300
301    private void Proc_3(Record_Type Pointer_Par_Ref) {
302      if (Record_Glob != null)
303        Pointer_Par_Ref = Record_Glob.Record_Comp;
304      else
305        Int_Glob = 100;
306
307      int[] Int_Comp_Ref = new int[1];
308      Int_Comp_Ref[0] = Record_Glob.Int_Comp;
309      Proc_7(10, Int_Glob, Int_Comp_Ref);
310      Record_Glob.Int_Comp = Int_Comp_Ref[0];
311    }
312
313    private void Proc_4() {
314      bool Bool_Loc;
315
316      Bool_Loc = Char_Glob_1 == 'A';
317      Bool_Loc = Bool_Loc || Bool_Glob;
318      Char_Glob_2 = 'B';
319    }
320
321    private void Proc_5() {
322      Char_Glob_1 = 'A';
323      Bool_Glob = false;
324    }
325
326    private void Proc_6(int Enum_Par_Val, int[] Enum_Par_Ref) {
327
328      Enum_Par_Ref[0] = Enum_Par_Val;
329
330      if (!Func_3(Enum_Par_Val))
331        Enum_Par_Ref[0] = Ident_4;
332
333      switch (Enum_Par_Val) {
334        case Ident_1:
335          Enum_Par_Ref[0] = Ident_1;
336          break;
337
338        case Ident_2:
339          if (Int_Glob > 100)
340            Enum_Par_Ref[0] = Ident_1;
341          else
342            Enum_Par_Ref[0] = Ident_4;
343          break;
344
345        case Ident_3:
346          Enum_Par_Ref[0] = Ident_2;
347          break;
348
349        case Ident_4:
350          break;
351
352        case Ident_5:
353          Enum_Par_Ref[0] = Ident_3;
354          break;
355      }
356    }
357
358    private void Proc_7(int Int_Par_Val1, int Int_Par_Val2, int[] Int_Par_Ref) {
359      int Int_Loc;
360      Int_Loc = Int_Par_Val1 + 2;
361      Int_Par_Ref[0] = Int_Par_Val2 + Int_Loc;
362    }
363
364    private void Proc_8(int[] Array_Par_1_Ref, int[][] Array_Par_2_Ref, int Int_Par_Val_1, int Int_Par_Val_2) {
365      int Int_Index;
366      int Int_Loc;
367
368      Int_Loc = Int_Par_Val_1 + 5;
369      Array_Par_1_Ref[Int_Loc] = Int_Par_Val_2;
370      Array_Par_1_Ref[Int_Loc + 1] = Array_Par_1_Ref[Int_Loc];
371      Array_Par_1_Ref[Int_Loc + 30] = Int_Loc;
372
373      for (Int_Index = Int_Loc; Int_Index <= Int_Loc + 1; ++Int_Index)
374        Array_Par_2_Ref[Int_Loc][Int_Index] = Int_Loc;
375      Array_Par_2_Ref[Int_Loc][Int_Loc - 1] += 1;
376      Array_Par_2_Ref[Int_Loc + 20][Int_Loc] = Array_Par_1_Ref[Int_Loc];
377      Int_Glob = 5;
378    }
379
380    #endregion
381
382    #region Private Class
383
384    private class Record_Type {
385      public Record_Type Record_Comp;
386      public int Discr;
387      public int Enum_Comp;
388      public int Int_Comp;
389      public string String_Comp;
390    }
391
392    #endregion
393
394    #region Clone
395
396    public IDeepCloneable Clone(Cloner cloner) {
397      return new DhrystoneAlgorithm(this, cloner);
398    }
399
400    public object Clone() {
401      return Clone(new Cloner());
402    }
403
404    #endregion
405
406    #region Events
407
408    public event EventHandler ItemImageChanged;
409    protected virtual void OnItemImageChanged() {
410      EventHandler handler = ItemImageChanged;
411      if (handler != null) handler(this, EventArgs.Empty);
412    }
413
414    public event EventHandler ToStringChanged;
415    protected virtual void OnToStringChanged() {
416      EventHandler handler = ToStringChanged;
417      if (handler != null) handler(this, EventArgs.Empty);
418    }
419
420    #endregion
421
422
423  }
424}
Note: See TracBrowser for help on using the repository browser.