Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs @ 17603

Last change on this file since 17603 was 15341, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Improved Performance

File size: 2.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
2  using System;
3  using HeuristicLab.Core;
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
7
8  public class PushInterpreterPool {
9    private readonly ObjectPool<PooledPushInterpreter> pool;
10
11    public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider;
12    public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
13
14    public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
15      : this(Environment.ProcessorCount * 4, 1024, null, config) {
16    }
17
18    public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
19      PushConfiguration = config ?? new PushConfiguration();
20
21      PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(poolPartitionSize, () => new PushProgram(), maxPartitionCount);
22      ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize, () => new PooledList<Expression>(), maxPartitionCount);
23
24      pool = new ObjectPool<PooledPushInterpreter>(CreateInterpreter, size);
25    }
26
27    private PooledPushInterpreter CreateInterpreter() {
28      var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider);
29      return new PooledPushInterpreter(this, PushConfiguration, poolContainer);
30    }
31
32    public IReadOnlyPushConfiguration PushConfiguration { get; private set; }
33
34    public PooledPushInterpreter Create(IRandom random = null) {
35      var interpreter = pool.Allocate();
36      interpreter.Reset(random);
37
38      return interpreter;
39    }
40
41    public void Free(PooledPushInterpreter interpreter) {
42      interpreter.ClearStacks();
43      pool.Free(interpreter);
44    }
45
46    public void Clear() {
47      PushProgramPoolProvider.Clear();
48      ExpressionListPoolProvider.Clear();
49      pool.Clear();
50    }
51  }
52}
Note: See TracBrowser for help on using the repository browser.