Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/17 12:42:31 (7 years ago)
Author:
pkimmesw
Message:

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

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  
    1717
    1818    private int entryIndex = 0;
     19
     20    public static int InstanceCount { get { return Pool.Sum(p => p.Count); } }
    1921
    2022    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;
    22
    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;
    99
    10   public class ManagedPool2<T> : IDisposable where T : class {
     10//  public class ManagedPool2<T> : IDisposable where T : class {
    1111
    12     private static readonly ConcurrentStack<T[]> Pool = new ConcurrentStack<T[]>();
     12//    private static readonly ConcurrentStack<T[]> Pool = new ConcurrentStack<T[]>();
    1313
    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;
    1716
    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;
    2120
    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;
    2624
    27       DummyPartition = dummyPartition;
    28     }
     25//      DummyPartition = dummyPartition;
     26//    }
    2927
    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//      }
    3634
    37       return currentPartition[entryIndex++];
    38     }
     35//      return currentPartition[entryIndex++];
     36//    }
    3937
    40     private T[] GetPartition() {
    41       //T[] partition;
     38//    private T[] GetPartition() {
     39//      T[] partition;
     40//      return (Pool.TryPop(out partition)) ? partition : DeepArrayCopy(DummyPartition);
     41//    }
    4242
    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();
    4446
    45       //for (var i = 0; i < PartitionSize; i++) {
    46       //  partition[i] = Factory();
    47       //}
     47//        binaryFormatter.Serialize(memoryStream, objectToCopy);
     48//        memoryStream.Seek(0, SeekOrigin.Begin);
    4849
    49       //return partition;
     50//        return (T[])binaryFormatter.Deserialize(memoryStream);
     51//      }
     52//    }
    5053
    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;
     1using System;
    22
    3 //namespace TestPooling.Pool {
    4 //  using System.Collections.Concurrent;
    5 //  using System.Threading.Tasks;
     3namespace 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;
    69
    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;
    1115
    12 //    public ManagedPoolProvider(int partitionSize, Func<T> factory) {
    13 //      PartitionSize = partitionSize;
    14 //      Factory = factory;
    15 //    }
     16    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
    1617
    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];
    1924
    20 //      return partitions.TryPop(out partition) ? partition : CreatePartition();
    21 //    }
     25      for (var i = 0; i < PartitionSize; i++) {
     26        DummyPartition[i] = factory();
     27      }
     28    }
    2229
    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    }
    2633
    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    }
    3038
    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();
    3342
    34 //      var partition = new T[PartitionSize];
     43        binaryFormatter.Serialize(memoryStream, objectToCopy);
     44        memoryStream.Seek(0, SeekOrigin.Begin);
    3545
    36 //      Parallel.For(0, PartitionSize, i => partition[i] = Factory());
     46        return (T[])binaryFormatter.Deserialize(memoryStream);
     47      }
     48    }
    3749
    38 //      return partition;
    39 //    }
     50    public IManagedPool<T> CreatePool() {
     51      return managedPools.Allocate();
     52    }
    4053
    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  
    44  using System;
    55  using System.Diagnostics;
     6  using System.Threading;
    67
    78  using TestPooling.Pool;
    89
    910  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
    1119    static void Main(string[] args) {
    1220      Test1();
     
    1927
    2028      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            }
    2837          }
    2938        }
    3039      });
    31       //});
    3240      sw.Stop();
    3341
    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);
    3543    }
    3644
     
    3947      sw.Start();
    4048
    41       var dummyPartition = new Person[Count];
     49      var provider = new ManagedPoolProvider<Person>(PartitionSize);
     50      provider.InitDummyPartition(() => new Person());
    4251
    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            }
    5460          }
    5561        }
    5662      });
    57       //});
    5863      sw.Stop();
    5964
    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);
    8566    }
    8667
     
    9071
    9172      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          }
    9482
    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);
    9784        }
    98 
    99         count += list.Length;
    10085      });
    10186      sw.Stop();
    10287
    10388      Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);
    104       Console.WriteLine("Count: {0}", count);
     89      Console.WriteLine("PartitionSize: {0}", count);
    10590    }
    10691
  • branches/PushGP/HeuristicLab.PushGP/TestPooling/TestPooling.csproj

    r14744 r14745  
    4444  </ItemGroup>
    4545  <ItemGroup>
     46    <Compile Include="Pool\IManagedPool.cs" />
    4647    <Compile Include="Pool\ManagedPool.cs" />
    4748    <Compile Include="Pool\ManagedPool2.cs" />
Note: See TracChangeset for help on using the changeset viewer.