Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs @ 14875

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

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File size: 4.3 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
2  using System;
3  using System.Collections.Generic;
4
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
7
8  public class InterpreterPoolContainer {
9    private readonly ManagedPoolProvider<PushProgram> pushProgramPoolProvider;
10    private readonly ManagedPoolProvider<LoopState> loopStatePoolProvider;
11    private readonly ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider;
12
13    private readonly IDictionary<Type, ManagedPoolProvider<Expression>> statefulExpressionPoolProviders
14      = new Dictionary<Type, ManagedPoolProvider<Expression>>();
15
16    private readonly IDictionary<Type, IManagedPool<Expression>> statefulExpressionPools
17      = new Dictionary<Type, IManagedPool<Expression>>();
18
19    public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) {
20      pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount);
21      loopStatePoolProvider = new ManagedPoolProvider<LoopState>(partitionSize, () => new LoopState(), maxPartitionCount);
22      expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
23
24      InitStatefulExpressionPools(partitionSize, maxPartitionCount);
25    }
26
27    public InterpreterPoolContainer(
28      ManagedPoolProvider<PushProgram> pushProgramPoolProvider,
29      ManagedPoolProvider<LoopState> loopStatePoolProvider,
30      ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) {
31      this.pushProgramPoolProvider = pushProgramPoolProvider;
32      this.loopStatePoolProvider = loopStatePoolProvider;
33      this.expressionListPoolProvider = expressionListPoolProvider;
34
35      InitStatefulExpressionPools(1024, 2048);
36    }
37
38    private IManagedPool<PushProgram> pushProgramPool;
39    public IManagedPool<PushProgram> PushProgramPool
40    {
41      get
42      {
43        if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool();
44        return pushProgramPool;
45      }
46    }
47
48    private IManagedPool<LoopState> loopStatePool;
49    public IManagedPool<LoopState> LoopStatePool
50    {
51      get
52      {
53        if (loopStatePool == null) loopStatePool = loopStatePoolProvider.CreatePool();
54        return loopStatePool;
55      }
56    }
57
58    private IManagedPool<PooledList<Expression>> expressionListPool;
59    public IManagedPool<PooledList<Expression>> ExpressionListPool
60    {
61      get
62      {
63        if (expressionListPool == null) expressionListPool = expressionListPoolProvider.CreatePool();
64        return expressionListPool;
65      }
66    }
67
68    private void InitStatefulExpressionPools(int partitionSize, int maxPartitionCount) {
69      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
70        var type = ExpressionTable.StatefulExpressionTypes[i];
71        var name = ExpressionTable.TypeToNameTable[type];
72
73        statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
74          partitionSize,
75          ExpressionTable.StatefulExpressionFactory[name],
76          maxPartitionCount));
77
78        statefulExpressionPools.Add(type, null);
79      }
80    }
81
82    public T GetStatefulExpression<T>() where T : Expression {
83      var type = typeof(T);
84      if (statefulExpressionPools[type] == null)
85        statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
86
87      return (T)statefulExpressionPools[type].Get();
88    }
89
90    public void DisposePools() {
91      if (pushProgramPool != null) {
92        pushProgramPool.Dispose();
93        pushProgramPool = null;
94      }
95
96      if (loopStatePool != null) {
97        loopStatePool.Dispose();
98        loopStatePool = null;
99      }
100
101      if (expressionListPool != null) {
102        expressionListPool.Dispose();
103        expressionListPool = null;
104      }
105
106      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
107        var type = ExpressionTable.StatefulExpressionTypes[i];
108        var pool = statefulExpressionPools[type];
109
110        if (pool != null) {
111          pool.Dispose();
112          statefulExpressionPools[type] = null;
113        }
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.