Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 3.8 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<PooledList<Expression>> expressionListPoolProvider;
11
12    private readonly IDictionary<Type, ManagedPoolProvider<Expression>> statefulExpressionPoolProviders
13      = new Dictionary<Type, ManagedPoolProvider<Expression>>();
14
15    private readonly IDictionary<Type, IManagedPool<Expression>> statefulExpressionPools
16      = new Dictionary<Type, IManagedPool<Expression>>();
17
18    public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) {
19      pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount);
20      expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
21
22      InitStatefulExpressionPools(partitionSize, maxPartitionCount);
23    }
24
25    public InterpreterPoolContainer(
26      ManagedPoolProvider<PushProgram> pushProgramPoolProvider,
27      ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) {
28      this.pushProgramPoolProvider = pushProgramPoolProvider;
29      this.expressionListPoolProvider = expressionListPoolProvider;
30
31      InitStatefulExpressionPools(1024, 2048);
32    }
33
34    private IManagedPool<PushProgram> pushProgramPool;
35    public IManagedPool<PushProgram> PushProgramPool
36    {
37      get
38      {
39        if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool();
40        return pushProgramPool;
41      }
42    }
43
44    private IManagedPool<PooledList<Expression>> expressionListPool;
45    public IManagedPool<PooledList<Expression>> ExpressionListPool
46    {
47      get
48      {
49        if (expressionListPool == null) expressionListPool = expressionListPoolProvider.CreatePool();
50        return expressionListPool;
51      }
52    }
53
54    private void InitStatefulExpressionPools(int partitionSize, int maxPartitionCount) {
55      foreach (var type in ExpressionTable.StatefulExpressionTypes) {
56        statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
57          partitionSize,
58          ExpressionTable.StatefulExpressionFactory[type],
59          maxPartitionCount));
60
61        statefulExpressionPools.Add(type, null);
62      }
63    }
64
65    public T GetStatefulExpression<T>() where T : Expression {
66      var type = typeof(T);
67      if (statefulExpressionPools[type] == null)
68        statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
69
70      return (T)statefulExpressionPools[type].Get();
71    }
72
73    public IManagedPool<Expression> GetStatefulExpressionPool<T>() where T : Expression {
74      var type = typeof(T);
75      return statefulExpressionPools[type] ?? (statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool());
76    }
77
78    public void DisposePools() {
79      if (pushProgramPool != null) {
80        pushProgramPool.Dispose();
81        pushProgramPool = null;
82      }
83
84      if (expressionListPool != null) {
85        expressionListPool.Dispose();
86        expressionListPool = null;
87      }
88
89      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
90        var type = ExpressionTable.StatefulExpressionTypes[i];
91        var pool = statefulExpressionPools[type];
92
93        if (pool != null) {
94          pool.Dispose();
95          statefulExpressionPools[type] = null;
96        }
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.