Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16296


Ignore:
Timestamp:
11/14/18 12:08:42 (5 years ago)
Author:
bburlacu
Message:

#2958: SymbolicDataAnalysisExpressionTreeBatchInterpreter: simplify Compile, add cache for variable values (helps a lot with performance).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs

    r16293 r16296  
    154154    }
    155155
     156    [ThreadStatic]
     157    private Dictionary<string, double[]> cachedData;
     158
     159    private void InitCache(IDataset dataset) {
     160      cachedData = new Dictionary<string, double[]>();
     161      foreach (var v in dataset.DoubleVariables) {
     162        var values = dataset.GetDoubleValues(v).ToArray();
     163      }
     164    }
     165
     166    public void InitializeState() {
     167      cachedData = null;
     168      EvaluatedSolutions = 0;
     169    }
     170
    156171    private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
    157172      var code = Compile(tree, dataset, OpCodes.MapSymbolToOpCode);
    158 
    159173      var remainingRows = rows.Length % BATCHSIZE;
    160174      var roundedTotal = rows.Length - remainingRows;
     
    176190
    177191    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
     192      if (cachedData == null) {
     193        InitCache(dataset);
     194      }
    178195      return GetValues(tree, dataset, rows);
    179196    }
    180197
    181198    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    182       return GetValues(tree, dataset, rows.ToArray());
    183     }
    184 
    185     public void InitializeState() {
     199      return GetSymbolicExpressionTreeValues(tree, dataset, rows.ToArray());
    186200    }
    187201
     
    193207      int c = 1, i = 0;
    194208      foreach (var node in root.IterateNodesBreadth()) {
    195         for (int j = 0; j < node.SubtreeCount; ++j) {
    196           var s = node.GetSubtree(j);
    197           if (s.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
    198           code[c + j] = new BatchInstruction { narg = (ushort)s.SubtreeCount, opcode = opCodeMapper(s) };
    199         }
    200 
    201         code[i].buf = new double[BATCHSIZE];
    202 
     209        if (node.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
     210        code[i] = new BatchInstruction {
     211          opcode = opCodeMapper(node),
     212          narg = (ushort)node.SubtreeCount,
     213          buf = new double[BATCHSIZE],
     214          childIndex = c
     215        };
    203216        if (node is VariableTreeNode variable) {
    204217          code[i].weight = variable.Weight;
    205           code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
     218          if (cachedData.ContainsKey(variable.VariableName)) {
     219            code[i].data = cachedData[variable.VariableName];
     220          } else {
     221            code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
     222            cachedData[variable.VariableName] = code[i].data;
     223          }
    206224        } else if (node is ConstantTreeNode constant) {
    207225          code[i].value = constant.Value;
     
    209227            code[i].buf[j] = code[i].value;
    210228        }
    211 
    212         code[i].childIndex = c;
    213229        c += node.SubtreeCount;
    214230        ++i;
Note: See TracChangeset for help on using the changeset viewer.