Changeset 14745 for branches/PushGP/HeuristicLab.PushGP/TestPooling
- Timestamp:
- 03/11/17 12:42:31 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/TestPooling
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/TestPooling/Pool/ManagedPool.cs
r14744 r14745 17 17 18 18 private int entryIndex = 0; 19 20 public static int InstanceCount { get { return Pool.Sum(p => p.Count); } } 19 21 20 22 public ManagedPool(int partitionSize, Func<T> factory) { -
branches/PushGP/HeuristicLab.PushGP/TestPooling/Pool/ManagedPool2.cs
r14744 r14745 1 using System.Collections.Generic;1 //using System.Collections.Generic; 2 2 3 namespace TestPooling.Pool {4 using System;5 using System.Collections.Concurrent;6 using System.IO;7 using System.Linq;8 using System.Runtime.Serialization.Formatters.Binary;3 //namespace TestPooling.Pool { 4 // using System; 5 // using System.Collections.Concurrent; 6 // using System.IO; 7 // using System.Linq; 8 // using System.Runtime.Serialization.Formatters.Binary; 9 9 10 public class ManagedPool2<T> : IDisposable where T : class {10 // public class ManagedPool2<T> : IDisposable where T : class { 11 11 12 private static readonly ConcurrentStack<T[]> Pool = new ConcurrentStack<T[]>();12 // private static readonly ConcurrentStack<T[]> Pool = new ConcurrentStack<T[]>(); 13 13 14 private readonly T[] DummyPartition; 15 private readonly Func<T> Factory; 16 public readonly int PartitionSize; 14 // private readonly T[] DummyPartition; 15 // public readonly int PartitionSize; 17 16 18 private readonly IList<T[]> partitions = new List<T[]>();19 private T[] currentPartition;20 private int entryIndex;17 // private readonly IList<T[]> partitions = new List<T[]>(); 18 // private T[] currentPartition; 19 // private int entryIndex; 21 20 22 public ManagedPool2(int partitionSize, T[] dummyPartition, Func<T> factory) { 23 PartitionSize = partitionSize; 24 Factory = factory; 25 entryIndex = partitionSize; 21 // public ManagedPool2(int partitionSize, T[] dummyPartition) { 22 // PartitionSize = partitionSize; 23 // entryIndex = partitionSize; 26 24 27 DummyPartition = dummyPartition;28 }25 // DummyPartition = dummyPartition; 26 // } 29 27 30 public T Get() {31 if (entryIndex == PartitionSize) {32 currentPartition = GetPartition();33 partitions.Add(currentPartition);34 entryIndex = 0;35 }28 // public T Get() { 29 // if (entryIndex == PartitionSize) { 30 // currentPartition = GetPartition(); 31 // partitions.Add(currentPartition); 32 // entryIndex = 0; 33 // } 36 34 37 return currentPartition[entryIndex++];38 }35 // return currentPartition[entryIndex++]; 36 // } 39 37 40 private T[] GetPartition() { 41 //T[] partition; 38 // private T[] GetPartition() { 39 // T[] partition; 40 // return (Pool.TryPop(out partition)) ? partition : DeepArrayCopy(DummyPartition); 41 // } 42 42 43 //partition = Pool.TryPop(out partition) ? partition : new T[PartitionSize]; 43 // private static T[] DeepArrayCopy(object objectToCopy) { 44 // using (var memoryStream = new MemoryStream()) { 45 // var binaryFormatter = new BinaryFormatter(); 44 46 45 //for (var i = 0; i < PartitionSize; i++) { 46 // partition[i] = Factory(); 47 //} 47 // binaryFormatter.Serialize(memoryStream, objectToCopy); 48 // memoryStream.Seek(0, SeekOrigin.Begin); 48 49 49 //return partition; 50 // return (T[])binaryFormatter.Deserialize(memoryStream); 51 // } 52 // } 50 53 51 T[] partition; 52 if (Pool.TryPop(out partition)) return partition; 53 54 //partition = new T[PartitionSize]; 55 //DummyPartition.CopyTo(partition, 0); 56 57 return DeepArrayCopy(DummyPartition); 58 } 59 60 private static T[] DeepArrayCopy(object objectToCopy) { 61 using (var memoryStream = new MemoryStream()) { 62 var binaryFormatter = new BinaryFormatter(); 63 64 binaryFormatter.Serialize(memoryStream, objectToCopy); 65 memoryStream.Seek(0, SeekOrigin.Begin); 66 67 return (T[])binaryFormatter.Deserialize(memoryStream); 68 } 69 } 70 71 public void Dispose() { 72 Pool.PushRange(partitions.ToArray()); 73 } 74 } 75 } 54 // public void Dispose() { 55 // Pool.PushRange(partitions.ToArray()); 56 // partitions.Clear(); 57 // } 58 // } 59 //} -
branches/PushGP/HeuristicLab.PushGP/TestPooling/Pool/ManagedPoolProvider.cs
r14744 r14745 1 //using System;1 using System; 2 2 3 //namespace TestPooling.Pool { 4 // using System.Collections.Concurrent; 5 // using System.Threading.Tasks; 3 namespace TestPooling.Pool { 4 using System.Collections.Concurrent; 5 using System.Collections.Generic; 6 using System.IO; 7 using System.Linq; 8 using System.Runtime.Serialization.Formatters.Binary; 6 9 7 // public class ManagedPoolProvider<T> where T : class { 8 // private static readonly volatile ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>(); 9 // private readonly Func<T> Factory; 10 // public readonly int PartitionSize; 10 public class ManagedPoolProvider<T> where T : class { 11 private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>(); 12 private readonly ObjectPool<IManagedPool<T>> managedPools; 13 private T[] DummyPartition; 14 public readonly int PartitionSize; 11 15 12 // public ManagedPoolProvider(int partitionSize, Func<T> factory) { 13 // PartitionSize = partitionSize; 14 // Factory = factory; 15 // } 16 public int InstanceCount { get { return partitions.Count * PartitionSize; } } 16 17 17 // public T[] GetPartition() { 18 // T[] partition; 18 public ManagedPoolProvider(int partitionSize) { 19 PartitionSize = partitionSize; 20 managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool2<T>(this)); 21 } 22 public void InitDummyPartition(Func<T> factory) { 23 DummyPartition = new T[PartitionSize]; 19 24 20 // return partitions.TryPop(out partition) ? partition : CreatePartition(); 21 // } 25 for (var i = 0; i < PartitionSize; i++) { 26 DummyPartition[i] = factory(); 27 } 28 } 22 29 23 //public void ReleasePartitions(params T[][] partition) {24 //partitions.PushRange(partition);25 //}30 public void ReleasePartitions(params T[][] partition) { 31 if (partition.Length > 0) partitions.PushRange(partition); 32 } 26 33 27 // public T CreateEntry() { 28 // return Factory(); 29 // } 34 private T[] GetPartition() { 35 T[] partition; 36 return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition); 37 } 30 38 31 // private T[] CreatePartition() { 32 // //return new T[PartitionSize]; 39 private static T[] DeepArrayCopy(object objectToCopy) { 40 using (var memoryStream = new MemoryStream()) { 41 var binaryFormatter = new BinaryFormatter(); 33 42 34 // var partition = new T[PartitionSize]; 43 binaryFormatter.Serialize(memoryStream, objectToCopy); 44 memoryStream.Seek(0, SeekOrigin.Begin); 35 45 36 // Parallel.For(0, PartitionSize, i => partition[i] = Factory()); 46 return (T[])binaryFormatter.Deserialize(memoryStream); 47 } 48 } 37 49 38 // return partition; 39 // } 50 public IManagedPool<T> CreatePool() { 51 return managedPools.Allocate(); 52 } 40 53 41 // public ManagedPool<T> CreatePool() { 42 // return new ManagedPool<T>(this); 43 // } 44 // } 45 //} 54 private class ManagedPool2<T> : IManagedPool<T> where T : class { 55 private readonly ManagedPoolProvider<T> provider; 56 private readonly IList<T[]> partitions = new List<T[]>(); 57 private T[] currentPartition; 58 private int entryIndex; 59 60 public ManagedPool2(ManagedPoolProvider<T> provider) { 61 this.provider = provider; 62 entryIndex = provider.PartitionSize; 63 } 64 65 public T Get() { 66 if (entryIndex == provider.PartitionSize) { 67 currentPartition = provider.GetPartition(); 68 partitions.Add(currentPartition); 69 entryIndex = 0; 70 } 71 72 return currentPartition[entryIndex++]; 73 } 74 75 public void Dispose() { 76 provider.ReleasePartitions(partitions.ToArray()); 77 partitions.Clear(); 78 provider.managedPools.Free(this); 79 } 80 } 81 } 82 } -
branches/PushGP/HeuristicLab.PushGP/TestPooling/Program.cs
r14744 r14745 4 4 using System; 5 5 using System.Diagnostics; 6 using System.Threading; 6 7 7 8 using TestPooling.Pool; 8 9 9 10 class Program { 10 private const int Count = 3000; 11 private const int PartitionSize = 512; 12 private const int ExampleCount = 200; 13 private const int InstanceCount = 1024; 14 private const int PopulationCount = 100; 15 16 private static int TotalInstanceCount => PopulationCount * InstanceCount * ExampleCount; 17 18 11 19 static void Main(string[] args) { 12 20 Test1(); … … 19 27 20 28 sw.Start(); 21 //Parallel.For(0, 3, k => { 22 Parallel.For(0, 10000, i => { 23 using (var pool = new ManagedPool<Person>(Count, () => new Person())) { 24 for (var j = 0; j < 3000; j++) { 25 var person = pool.Get(); 26 person.Age = i + j; 27 person.Name = string.Format("{0} - {1}", i, j); 29 Parallel.For(0, PopulationCount, pc => { 30 for (var e = 0; e < ExampleCount; e++) { 31 using (var pool = new ManagedPool<Person>(PartitionSize, () => new Person())) { 32 for (var i = 0; i < InstanceCount; i++) { 33 var person = pool.Get(); 34 person.Age = pc + e + i; 35 person.Name = string.Format("{0} - {1} - {2}", pc, e, i); 36 } 28 37 } 29 38 } 30 39 }); 31 //});32 40 sw.Stop(); 33 41 34 Console.WriteLine("1 - Duration: {0} - {1} ", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);42 Console.WriteLine("1 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, ManagedPool<Person>.InstanceCount, TotalInstanceCount); 35 43 } 36 44 … … 39 47 sw.Start(); 40 48 41 var dummyPartition = new Person[Count]; 49 var provider = new ManagedPoolProvider<Person>(PartitionSize); 50 provider.InitDummyPartition(() => new Person()); 42 51 43 for (var i = 0; i < Count; i++) { 44 dummyPartition[i] = new Person(); 45 } 46 47 //Parallel.For(0, 3, k => { 48 Parallel.For(0, 10000, i => { 49 using (var pool = new ManagedPool2<Person>(Count, dummyPartition, () => new Person())) { 50 for (var j = 0; j < 3000; j++) { 51 var person = pool.Get(); 52 person.Age = i + j; 53 person.Name = string.Format("{0} - {1}", i, j); 52 Parallel.For(0, PopulationCount, pc => { 53 for (var e = 0; e < ExampleCount; e++) { 54 using (var pool = provider.CreatePool()) { 55 for (var i = 0; i < InstanceCount; i++) { 56 var person = pool.Get(); 57 person.Age = pc + e + i; 58 person.Name = string.Format("{0} - {1} - {2}", pc, e, i); 59 } 54 60 } 55 61 } 56 62 }); 57 //});58 63 sw.Stop(); 59 64 60 //var p1 = new ManagedPool2<Person>(Count, dummyPartition, () => new TestPooling.Person()); 61 //var p2 = new ManagedPool2<Person>(Count, dummyPartition, () => new TestPooling.Person()); 62 //var pa1 = p1.Get(); 63 //var pa2 = p2.Get(); 64 65 Console.WriteLine("1_2 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds); 66 } 67 68 private static void Test4() { 69 var sw = new Stopwatch(); 70 71 sw.Start(); ; 72 var pool = new ManagedPool<Person>(512, () => new Person()); 73 74 Parallel.For(0, 10000, i => { 75 for (var j = 0; j < 3000; j++) { 76 var person = pool.Get(); 77 person.Age = i + j; 78 person.Name = string.Format("{0} - {1}", i, j); 79 } 80 pool.Dispose(); 81 }); 82 sw.Stop(); 83 84 Console.WriteLine("1 - Duration: {0}", sw.ElapsedTicks); 65 Console.WriteLine("1_2 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount); 85 66 } 86 67 … … 90 71 91 72 sw.Start(); 92 Parallel.For(0, 10000, i => { 93 var list = new Person[3000]; 73 Parallel.For(0, PopulationCount, pc => { 74 for (var e = 0; e < ExampleCount; e++) { 75 var list = new Person[InstanceCount]; 76 for (var i = 0; i < InstanceCount; i++) { 77 list[i] = new Person { 78 Age = pc + e + i, 79 Name = string.Format("{0} - {1} - {2}", pc, e, i) 80 }; 81 } 94 82 95 for (var j = 0; j < 3000; j++) { 96 list[j] = new Person { Age = i + j, Name = string.Format("{0} - {1}", i, j) }; 83 Interlocked.Add(ref count, list.Length); 97 84 } 98 99 count += list.Length;100 85 }); 101 86 sw.Stop(); 102 87 103 88 Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds); 104 Console.WriteLine(" Count: {0}", count);89 Console.WriteLine("PartitionSize: {0}", count); 105 90 } 106 91 -
branches/PushGP/HeuristicLab.PushGP/TestPooling/TestPooling.csproj
r14744 r14745 44 44 </ItemGroup> 45 45 <ItemGroup> 46 <Compile Include="Pool\IManagedPool.cs" /> 46 47 <Compile Include="Pool\ManagedPool.cs" /> 47 48 <Compile Include="Pool\ManagedPool2.cs" />
Note: See TracChangeset
for help on using the changeset viewer.