Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs @ 18160

Last change on this file since 18160 was 18132, checked in by gkronber, 3 years ago

#3140: merged r18091:18131 from branch to trunk

File size: 10.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
24using System.Linq;
25
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HEAL.Attic;
32
33using static HeuristicLab.Problems.DataAnalysis.Symbolic.BatchOperations;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [Item("SymbolicDataAnalysisExpressionTreeBatchInterpreter", "An interpreter that uses batching and vectorization techniques to achieve faster performance.")]
37  [StorableType("BEB15146-BB95-4838-83AC-6838543F017B")]
38  public class SymbolicDataAnalysisExpressionTreeBatchInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
39    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
40
41    #region parameters
42    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
44    }
45    #endregion
46
47    #region properties
48    public int EvaluatedSolutions {
49      get { return EvaluatedSolutionsParameter.Value.Value; }
50      set { EvaluatedSolutionsParameter.Value.Value = value; }
51    }
52    #endregion
53
54    public void ClearState() { }
55
56    public SymbolicDataAnalysisExpressionTreeBatchInterpreter() {
57      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
58    }
59
60    [StorableConstructor]
61    protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(StorableConstructorFlag _) : base(_) { }
62    protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(SymbolicDataAnalysisExpressionTreeBatchInterpreter original, Cloner cloner) : base(original, cloner) {
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new SymbolicDataAnalysisExpressionTreeBatchInterpreter(this, cloner);
66    }
67
68    private void LoadData(BatchInstruction instr, int[] rows, int rowIndex, int batchSize) {
69      for (int i = 0; i < batchSize; ++i) {
70        var row = rows[rowIndex] + i;
71        instr.buf[i] = instr.weight * instr.data[row];
72      }
73    }
74
75    private void Evaluate(BatchInstruction[] code, int[] rows, int rowIndex, int batchSize) {
76      for (int i = code.Length - 1; i >= 0; --i) {
77        var instr = code[i];
78        var c = instr.childIndex;
79        var n = instr.narg;
80
81        switch (instr.opcode) {
82          case OpCodes.Variable: {
83              LoadData(instr, rows, rowIndex, batchSize);
84              break;
85            }
86          case OpCodes.Constant: // fall through
87          case OpCodes.Number:
88            break; // nothing to do here, don't remove because we want to prevent falling into the default case here.
89          case OpCodes.Add: {
90              Load(instr.buf, code[c].buf);
91              for (int j = 1; j < n; ++j) {
92                Add(instr.buf, code[c + j].buf);
93              }
94              break;
95            }
96
97          case OpCodes.Sub: {
98              if (n == 1) {
99                Neg(instr.buf, code[c].buf);
100              } else {
101                Load(instr.buf, code[c].buf);
102                for (int j = 1; j < n; ++j) {
103                  Sub(instr.buf, code[c + j].buf);
104                }
105              }
106              break;
107            }
108
109          case OpCodes.Mul: {
110              Load(instr.buf, code[c].buf);
111              for (int j = 1; j < n; ++j) {
112                Mul(instr.buf, code[c + j].buf);
113              }
114              break;
115            }
116
117          case OpCodes.Div: {
118              if (n == 1) {
119                Inv(instr.buf, code[c].buf);
120              } else {
121                Load(instr.buf, code[c].buf);
122                for (int j = 1; j < n; ++j) {
123                  Div(instr.buf, code[c + j].buf);
124                }
125              }
126              break;
127            }
128
129          case OpCodes.Square: {
130              Square(instr.buf, code[c].buf);
131              break;
132            }
133
134          case OpCodes.Root: {
135              Load(instr.buf, code[c].buf);
136              Root(instr.buf, code[c + 1].buf);
137              break;
138            }
139
140          case OpCodes.SquareRoot: {
141              Sqrt(instr.buf, code[c].buf);
142              break;
143            }
144
145          case OpCodes.Cube: {
146              Cube(instr.buf, code[c].buf);
147              break;
148            }
149          case OpCodes.CubeRoot: {
150              CubeRoot(instr.buf, code[c].buf);
151              break;
152            }
153
154          case OpCodes.Power: {
155              Load(instr.buf, code[c].buf);
156              Pow(instr.buf, code[c + 1].buf);
157              break;
158            }
159
160          case OpCodes.Exp: {
161              Exp(instr.buf, code[c].buf);
162              break;
163            }
164
165          case OpCodes.Log: {
166              Log(instr.buf, code[c].buf);
167              break;
168            }
169
170          case OpCodes.Sin: {
171              Sin(instr.buf, code[c].buf);
172              break;
173            }
174
175          case OpCodes.Cos: {
176              Cos(instr.buf, code[c].buf);
177              break;
178            }
179
180          case OpCodes.Tan: {
181              Tan(instr.buf, code[c].buf);
182              break;
183            }
184          case OpCodes.Tanh: {
185              Tanh(instr.buf, code[c].buf);
186              break;
187            }
188          case OpCodes.Absolute: {
189              Absolute(instr.buf, code[c].buf);
190              break;
191            }
192
193          case OpCodes.AnalyticQuotient: {
194              Load(instr.buf, code[c].buf);
195              AnalyticQuotient(instr.buf, code[c + 1].buf);
196              break;
197            }
198          default: throw new NotSupportedException($"This interpreter does not support {(OpCode)instr.opcode}");
199        }
200      }
201    }
202
203    private readonly object syncRoot = new object();
204
205    [ThreadStatic]
206    private static Dictionary<string, double[]> cachedData;
207
208    [ThreadStatic]
209    private static IDataset cachedDataset;
210
211    private void InitCache(IDataset dataset) {
212      cachedDataset = dataset;
213      cachedData = new Dictionary<string, double[]>();
214      foreach (var v in dataset.DoubleVariables) {
215        cachedData[v] = dataset.GetDoubleValues(v).ToArray();
216      }
217    }
218
219    public void InitializeState() {
220      cachedData = null;
221      cachedDataset = null;
222      EvaluatedSolutions = 0;
223    }
224
225    private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
226      if (cachedData == null || cachedDataset != dataset || cachedDataset is ModifiableDataset) {
227        InitCache(dataset);
228      }
229
230      var code = Compile(tree, dataset, OpCodes.MapSymbolToOpCode);
231      var remainingRows = rows.Length % BATCHSIZE;
232      var roundedTotal = rows.Length - remainingRows;
233
234      var result = new double[rows.Length];
235
236      for (int rowIndex = 0; rowIndex < roundedTotal; rowIndex += BATCHSIZE) {
237        Evaluate(code, rows, rowIndex, BATCHSIZE);
238        Array.Copy(code[0].buf, 0, result, rowIndex, BATCHSIZE);
239      }
240
241      if (remainingRows > 0) {
242        Evaluate(code, rows, roundedTotal, remainingRows);
243        Array.Copy(code[0].buf, 0, result, roundedTotal, remainingRows);
244      }
245
246      // when evaluation took place without any error, we can increment the counter
247      lock (syncRoot) {
248        EvaluatedSolutions++;
249      }
250
251      return result;
252    }
253
254    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
255      return GetValues(tree, dataset, rows);
256    }
257
258    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
259      return GetSymbolicExpressionTreeValues(tree, dataset, rows.ToArray());
260    }
261
262    private BatchInstruction[] Compile(ISymbolicExpressionTree tree, IDataset dataset, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
263      var root = tree.Root.GetSubtree(0).GetSubtree(0);
264      var code = new BatchInstruction[root.GetLength()];
265      if (root.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
266      int c = 1, i = 0;
267      foreach (var node in root.IterateNodesBreadth()) {
268        if (node.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
269        code[i] = new BatchInstruction {
270          opcode = opCodeMapper(node),
271          narg = (ushort)node.SubtreeCount,
272          buf = new double[BATCHSIZE],
273          childIndex = c
274        };
275        if (node is VariableTreeNode variable) {
276          code[i].weight = variable.Weight;
277          if (cachedData.ContainsKey(variable.VariableName)) {
278            code[i].data = cachedData[variable.VariableName];
279          } else {
280            code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
281            cachedData[variable.VariableName] = code[i].data;
282          }
283        } else if (node is INumericTreeNode numeric) {
284          code[i].value = numeric.Value;
285          for (int j = 0; j < BATCHSIZE; ++j)
286            code[i].buf[j] = code[i].value;
287        }
288        c += node.SubtreeCount;
289        ++i;
290      }
291      return code;
292    }
293  }
294}
Note: See TracBrowser for help on using the repository browser.