1 | namespace 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 | using HeuristicLab.Random;
|
---|
8 |
|
---|
9 | public class PushInterpreterPool {
|
---|
10 | private readonly ObjectPool<PooledPushInterpreter> pool;
|
---|
11 |
|
---|
12 | public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider;
|
---|
13 | public readonly ManagedPoolProvider<LoopState> LoopStatePoolProvider;
|
---|
14 | public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
|
---|
15 |
|
---|
16 |
|
---|
17 | public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
|
---|
18 | : this(Environment.ProcessorCount * 2, 1024, null, config) {
|
---|
19 | }
|
---|
20 |
|
---|
21 | public PushInterpreterPool(int size, int pushProgramPoolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
|
---|
22 | PushGpConfiguration = config ?? new PushConfiguration();
|
---|
23 |
|
---|
24 | PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(pushProgramPoolPartitionSize, () => new PushProgram(), maxPartitionCount);
|
---|
25 | LoopStatePoolProvider = new ManagedPoolProvider<LoopState>(pushProgramPoolPartitionSize, () => new LoopState(), maxPartitionCount);
|
---|
26 | ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(pushProgramPoolPartitionSize * 2, () => new PooledList<Expression>(), maxPartitionCount);
|
---|
27 |
|
---|
28 | pool = new ObjectPool<PooledPushInterpreter>(() => {
|
---|
29 | var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, LoopStatePoolProvider, ExpressionListPoolProvider);
|
---|
30 | return new PooledPushInterpreter(this, PushGpConfiguration, poolContainer);
|
---|
31 | }, size);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public IReadOnlyPushConfiguration PushGpConfiguration { get; private set; }
|
---|
35 |
|
---|
36 | public PooledPushInterpreter Create(IRandom random = null) {
|
---|
37 | var interpreter = this.pool.Allocate();
|
---|
38 | interpreter.Random = random ?? new FastRandom();
|
---|
39 |
|
---|
40 | return interpreter;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void Free(PooledPushInterpreter interpreter) {
|
---|
44 | this.pool.Free(interpreter);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|