Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5183


Ignore:
Timestamp:
12/30/10 02:13:02 (14 years ago)
Author:
swagner
Message:

Stored execution contexts locally for each thread (#1333)

Location:
branches/ParallelEngine
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/ParallelEngine/HeuristicLab.Common/3.3/HeuristicLab.Common-3.3.csproj

    r5163 r5183  
    1212    <AssemblyName>HeuristicLab.Common-3.3</AssemblyName>
    1313    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    14     <TargetFrameworkProfile></TargetFrameworkProfile>
     14    <TargetFrameworkProfile>
     15    </TargetFrameworkProfile>
    1516    <FileAlignment>512</FileAlignment>
    1617    <SignAssembly>true</SignAssembly>
  • branches/ParallelEngine/HeuristicLab.Core/3.3/Interfaces/IOperator.cs

    r5178 r5183  
    2424namespace HeuristicLab.Core {
    2525  /// <summary>
    26   /// Interface to represent an operator (e.g. GreaterThanComparator,...),
    27   /// a basic instruction of an algorithm.
     26  /// Interface to represent an operator.
    2827  /// </summary>
    2928  public interface IOperator : IParameterizedNamedItem {
    30     /// <summary>
    31     /// Gets or sets a boolean value whether the engine should stop here during the run.
    32     /// </summary>
    3329    bool Breakpoint { get; set; }
    3430
    35     /// <summary>
    36     /// Executes the current instance on the specified <paramref name="scope"/>.
    37     /// </summary>
    38     /// <param name="scope">The scope where to execute the current instance.</param>
    39     /// <returns>The next operation.</returns>
    4031    IOperation Execute(IExecutionContext context);
    41     /// <summary>
    42     /// Aborts the current operator.
    43     /// </summary>
    4432    void Abort();
    4533
    46     /// <summary>
    47     /// Occurs when the breakpoint flag of the current instance was changed.
    48     /// </summary>
    49     event EventHandler BreakpointChanged;
    50     /// <summary>
    51     /// Occurs when the current instance is executed.
    52     /// </summary>
     34    event EventHandler BreakpointChanged;
    5335    event EventHandler Executed;
    5436  }
  • branches/ParallelEngine/HeuristicLab.Operators/3.3/Operator.cs

    r5178 r5183  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2829namespace HeuristicLab.Operators {
    2930  /// <summary>
    30   /// The base class for all operators.
     31  /// Base class for operators.
    3132  /// </summary>
    3233  [Item("Operator", "Base class for operators.")]
     
    4344    }
    4445
    45     [Storable]
    46     private IExecutionContext executionContext;
     46    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    4747    protected IExecutionContext ExecutionContext {
    48       get { return executionContext; }
     48      get { return executionContexts.Value.Value; }
    4949      private set {
    50         if (value != executionContext) {
    51           executionContext = value;
    52           OnExecutionContextChanged();
     50        if (value != executionContexts.Value.Value) {
     51          executionContexts.Value.Value = value;
    5352        }
    5453      }
    5554    }
    5655
    57     /// <summary>
    58     /// Flag whether the current instance has been canceled.
    59     /// </summary>
    6056    private bool canceled;
    61     /// <inheritdoc/>
    6257    protected bool Canceled {
    6358      get { return canceled; }
     
    7267    [Storable]
    7368    private bool breakpoint;
    74     /// <inheritdoc/>
    75     /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
    7669    public bool Breakpoint {
    7770      get { return breakpoint; }
     
    8679
    8780    [StorableConstructor]
    88     protected Operator(bool deserializing) : base(deserializing) { }
     81    protected Operator(bool deserializing)
     82      : base(deserializing) {
     83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     84      canceled = false;
     85    }
    8986    protected Operator(Operator original, Cloner cloner)
    9087      : base(original, cloner) {
     88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9189      this.canceled = original.canceled;
    9290      this.breakpoint = original.breakpoint;
    93       this.executionContext = cloner.Clone<IExecutionContext>(original.executionContext);
    9491    }
    95     /// <summary>
    96     /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
    97     /// the canceled flag to <c>false</c> and the name of the operator to the type name.
    98     /// </summary>
    9992    protected Operator()
    10093      : base() {
     94      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10195      canceled = false;
    10296      breakpoint = false;
     
    10498    protected Operator(string name)
    10599      : base(name) {
     100      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    106101      canceled = false;
    107102      breakpoint = false;
     
    109104    protected Operator(string name, ParameterCollection parameters)
    110105      : base(name, parameters) {
     106      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    111107      canceled = false;
    112108      breakpoint = false;
     
    114110    protected Operator(string name, string description)
    115111      : base(name, description) {
     112      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    116113      canceled = false;
    117114      breakpoint = false;
     
    119116    protected Operator(string name, string description, ParameterCollection parameters)
    120117      : base(name, description, parameters) {
     118      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    121119      canceled = false;
    122120      breakpoint = false;
    123121    }
    124122
    125     /// <inheritdoc/>
    126123    public virtual IOperation Execute(IExecutionContext context) {
    127124      try {
     
    140137      }
    141138    }
    142     /// <inheritdoc/>
    143     /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
    144139    public void Abort() {
    145140      Canceled = true;
    146141    }
    147     /// <summary>
    148     /// Performs the current operator on the specified <paramref name="scope"/>.
    149     /// </summary>
    150     /// <param name="scope">The scope where to execute the operator</param>
    151     /// <returns><c>null</c>.</returns>
    152142    public abstract IOperation Apply();
    153143
    154     protected virtual void OnExecutionContextChanged() { }
    155144    protected virtual void OnCanceledChanged() { }
    156     /// <inheritdoc/>
    157145    public event EventHandler BreakpointChanged;
    158     /// <summary>
    159     /// Fires a new <c>BreakpointChanged</c> event.
    160     /// </summary>
    161146    protected virtual void OnBreakpointChanged() {
    162147      if (BreakpointChanged != null) {
     
    164149      }
    165150    }
    166     /// <inheritdoc/>
    167151    public event EventHandler Executed;
    168     /// <summary>
    169     /// Fires a new <c>Executed</c> event.
    170     /// </summary>
    171152    protected virtual void OnExecuted() {
    172153      if (Executed != null) {
  • branches/ParallelEngine/HeuristicLab.Parameters/3.3/Parameter.cs

    r5178 r5183  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System.Collections.Generic;
    28 using System.Threading;
    2928
    3029namespace HeuristicLab.Parameters {
     
    5554      get { return dataType; }
    5655    }
    57     protected Dictionary<int, IItem> cachedActualValues;
     56
     57    private Lazy<ThreadLocal<IItem>> cachedActualValues;
    5858    public IItem ActualValue {
    5959      get {
    60         int id = Thread.CurrentThread.ManagedThreadId;
    61         IItem value = null;
    62         lock (cachedActualValues) {
    63           cachedActualValues.TryGetValue(id, out value);
    64         }
    65         if (value == null) {
    66           value = GetActualValue();
    67           lock (cachedActualValues) {
    68             if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value;
    69             else cachedActualValues.Add(id, value);
    70           }
    71         }
    72         return value;
     60        if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
     61        return cachedActualValues.Value.Value;
    7362      }
    7463      set {
    75         int id = Thread.CurrentThread.ManagedThreadId;
    76         lock (cachedActualValues) {
    77           if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value;
    78           else cachedActualValues.Add(id, value);
    79         }
     64        cachedActualValues.Value.Value = value;
    8065        SetActualValue(value);
    8166      }
    8267    }
    83     private Dictionary<int, IExecutionContext> executionContexts;
     68    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    8469    public IExecutionContext ExecutionContext {
    85       get {
    86         int id = Thread.CurrentThread.ManagedThreadId;
    87         IExecutionContext context = null;
    88         lock (executionContexts) {
    89           executionContexts.TryGetValue(id, out context);
    90         }
    91         return context;
    92       }
     70      get { return executionContexts.Value.Value; }
    9371      set {
    94         IExecutionContext context = null;
    95         int id = Thread.CurrentThread.ManagedThreadId;
    96         lock (executionContexts) {
    97           executionContexts.TryGetValue(id, out context);
    98           if (value != context) {
    99             if (context == null) executionContexts.Add(id, value);
    100             else if (value == null) {
    101               executionContexts.Remove(id);
    102               lock (cachedActualValues) cachedActualValues.Remove(id);
    103             } else {
    104               executionContexts[id] = value;
    105               lock (cachedActualValues) cachedActualValues.Remove(id);
    106             }
    107           }
     72        if (value != executionContexts.Value.Value) {
     73          executionContexts.Value.Value = value;
     74          cachedActualValues.Value.Value = null;
    10875        }
    10976      }
     
    11178
    11279    [StorableConstructor]
    113     protected Parameter(bool deserializing) : base(deserializing) {
    114       cachedActualValues = new Dictionary<int, IItem>();
    115       executionContexts = new Dictionary<int, IExecutionContext>();
     80    protected Parameter(bool deserializing)
     81      : base(deserializing) {
     82      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    11684    }
    11785    protected Parameter(Parameter original, Cloner cloner)
    11886      : base(original, cloner) {
    11987      dataType = original.dataType;
    120       cachedActualValues = new Dictionary<int, IItem>();
    121       executionContexts = new Dictionary<int, IExecutionContext>();
    122       foreach (var item in original.executionContexts) {
    123         executionContexts.Add(item.Key, cloner.Clone(item.Value));
    124       }
     88      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     89      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    12590    }
    12691    protected Parameter()
    12792      : base("Anonymous") {
    12893      dataType = typeof(IItem);
    129       cachedActualValues = new Dictionary<int, IItem>();
    130       executionContexts = new Dictionary<int, IExecutionContext>();
     94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    13196    }
    13297    protected Parameter(string name, Type dataType)
     
    13499      if (dataType == null) throw new ArgumentNullException();
    135100      this.dataType = dataType;
    136       cachedActualValues = new Dictionary<int, IItem>();
    137       executionContexts = new Dictionary<int, IExecutionContext>();
     101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    138103    }
    139104    protected Parameter(string name, string description, Type dataType)
     
    141106      if (dataType == null) throw new ArgumentNullException();
    142107      this.dataType = dataType;
    143       cachedActualValues = new Dictionary<int, IItem>();
    144       executionContexts = new Dictionary<int, IExecutionContext>();
     108      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     109      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    145110    }
    146111
Note: See TracChangeset for help on using the changeset viewer.