Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16287 was 16287, checked in by bburlacu, 5 years ago

#2958: Keep the SymbolicDataAnalysisExpressionTreeBatchInterpreter, but remove vectorization.

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12using static HeuristicLab.Problems.DataAnalysis.Symbolic.BatchOperations;
13//using static HeuristicLab.Problems.DataAnalysis.Symbolic.BatchOperationsVector;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
16  [Item("SymbolicDataAnalysisExpressionTreeBatchInterpreter", "An interpreter that uses batching and vectorization techniques to achieve faster performance.")]
17  [StorableClass]
18  public class SymbolicDataAnalysisExpressionTreeBatchInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
19    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
20
21    #region parameters
22    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
23      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
24    }
25    #endregion
26
27    #region properties
28    public int EvaluatedSolutions {
29      get { return EvaluatedSolutionsParameter.Value.Value; }
30      set { EvaluatedSolutionsParameter.Value.Value = value; }
31    }
32    #endregion
33
34    public void ClearState() { }
35
36    public SymbolicDataAnalysisExpressionTreeBatchInterpreter() {
37      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
38    }
39
40
41    [StorableConstructor]
42    protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(bool deserializing) : base(deserializing) { }
43    protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(SymbolicDataAnalysisExpressionTreeBatchInterpreter original, Cloner cloner) : base(original, cloner) {
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new SymbolicDataAnalysisExpressionTreeBatchInterpreter(this, cloner);
47    }
48
49    private void LoadData(BatchInstruction instr, int[] rows, int rowIndex, int batchSize) {
50      for (int i = 0; i < batchSize; ++i) {
51        var row = rows[rowIndex] + i;
52        instr.buf[i] = instr.weight * instr.data[row];
53      }
54    }
55
56    private void Evaluate(BatchInstruction[] code, int[] rows, int rowIndex, int batchSize) {
57      for (int i = code.Length - 1; i >= 0; --i) {
58        var instr = code[i];
59        var c = instr.childIndex;
60        var n = instr.narg;
61
62        switch (instr.opcode) {
63          case OpCodes.Variable: {
64              LoadData(instr, rows, rowIndex, batchSize);
65              break;
66            }
67          case OpCodes.Add: {
68              Load(instr.buf, code[c].buf);
69              for (int j = 1; j < n; ++j) {
70                Add(instr.buf, code[c + j].buf);
71              }
72              break;
73            }
74
75          case OpCodes.Sub: {
76              if (n == 1) {
77                Neg(instr.buf, code[c].buf);
78                break;
79              } else {
80                Load(instr.buf, code[c].buf);
81                for (int j = 1; j < n; ++j) {
82                  Sub(instr.buf, code[c + j].buf);
83                }
84                break;
85              }
86            }
87
88          case OpCodes.Mul: {
89              Load(instr.buf, code[c].buf);
90              for (int j = 1; j < n; ++j) {
91                Mul(instr.buf, code[c + j].buf);
92              }
93              break;
94            }
95
96          case OpCodes.Div: {
97              if (n == 1) {
98                Inv(instr.buf, code[c].buf);
99                break;
100              } else {
101                Load(instr.buf, code[c].buf);
102                for (int j = 1; j < n; ++j) {
103                  Div(instr.buf, code[c + j].buf);
104                }
105                break;
106              }
107            }
108
109          case OpCodes.Exp: {
110              Exp(instr.buf, code[c].buf);
111              break;
112            }
113
114          case OpCodes.Log: {
115              Log(instr.buf, code[c].buf);
116              break;
117            }
118        }
119      }
120    }
121
122    private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
123      var code = Compile(tree, dataset, OpCodes.MapSymbolToOpCode);
124
125      var remainingRows = rows.Length % BATCHSIZE;
126      var roundedTotal = rows.Length - remainingRows;
127
128      var result = new double[rows.Length];
129
130      for (int rowIndex = 0; rowIndex < roundedTotal; rowIndex += BATCHSIZE) {
131        Evaluate(code, rows, rowIndex, BATCHSIZE);
132        Array.Copy(code[0].buf, 0, result, rowIndex, BATCHSIZE);
133      }
134
135      if (remainingRows > 0) {
136        Evaluate(code, rows, roundedTotal, remainingRows);
137        Array.Copy(code[0].buf, 0, result, roundedTotal, remainingRows);
138      }
139
140      return result;
141    }
142
143    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
144      return GetValues(tree, dataset, rows.ToArray());
145    }
146
147    public void InitializeState() {
148    }
149
150    private BatchInstruction[] Compile(ISymbolicExpressionTree tree, IDataset dataset, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
151      var root = tree.Root.GetSubtree(0).GetSubtree(0);
152      var code = new BatchInstruction[root.GetLength()];
153      if (root.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
154      code[0] = new BatchInstruction { narg = (ushort)root.SubtreeCount, opcode = opCodeMapper(root) };
155      int c = 1, i = 0;
156      foreach (var node in root.IterateNodesBreadth()) {
157        for (int j = 0; j < node.SubtreeCount; ++j) {
158          var s = node.GetSubtree(j);
159          if (s.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
160          code[c + j] = new BatchInstruction { narg = (ushort)s.SubtreeCount, opcode = opCodeMapper(s) };
161        }
162
163        code[i].buf = new double[BATCHSIZE];
164
165        if (node is VariableTreeNode variable) {
166          code[i].weight = variable.Weight;
167          code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
168        } else if (node is ConstantTreeNode constant) {
169          code[i].value = constant.Value;
170          for (int j = 0; j < BATCHSIZE; ++j)
171            code[i].buf[j] = code[i].value;
172        }
173
174        code[i].childIndex = c;
175        c += node.SubtreeCount;
176        ++i;
177      }
178      return code;
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.