Changeset 5183
- Timestamp:
- 12/30/10 02:13:02 (14 years ago)
- Location:
- branches/ParallelEngine
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ParallelEngine/HeuristicLab.Common/3.3/HeuristicLab.Common-3.3.csproj
r5163 r5183 12 12 <AssemblyName>HeuristicLab.Common-3.3</AssemblyName> 13 13 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> 14 <TargetFrameworkProfile></TargetFrameworkProfile> 14 <TargetFrameworkProfile> 15 </TargetFrameworkProfile> 15 16 <FileAlignment>512</FileAlignment> 16 17 <SignAssembly>true</SignAssembly> -
branches/ParallelEngine/HeuristicLab.Core/3.3/Interfaces/IOperator.cs
r5178 r5183 24 24 namespace HeuristicLab.Core { 25 25 /// <summary> 26 /// Interface to represent an operator (e.g. GreaterThanComparator,...), 27 /// a basic instruction of an algorithm. 26 /// Interface to represent an operator. 28 27 /// </summary> 29 28 public interface IOperator : IParameterizedNamedItem { 30 /// <summary>31 /// Gets or sets a boolean value whether the engine should stop here during the run.32 /// </summary>33 29 bool Breakpoint { get; set; } 34 30 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>40 31 IOperation Execute(IExecutionContext context); 41 /// <summary>42 /// Aborts the current operator.43 /// </summary>44 32 void Abort(); 45 33 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; 53 35 event EventHandler Executed; 54 36 } -
branches/ParallelEngine/HeuristicLab.Operators/3.3/Operator.cs
r5178 r5183 22 22 using System; 23 23 using System.Drawing; 24 using System.Threading; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 28 29 namespace HeuristicLab.Operators { 29 30 /// <summary> 30 /// The base class for alloperators.31 /// Base class for operators. 31 32 /// </summary> 32 33 [Item("Operator", "Base class for operators.")] … … 43 44 } 44 45 45 [Storable] 46 private IExecutionContext executionContext; 46 private Lazy<ThreadLocal<IExecutionContext>> executionContexts; 47 47 protected IExecutionContext ExecutionContext { 48 get { return executionContext ; }48 get { return executionContexts.Value.Value; } 49 49 private set { 50 if (value != executionContext) { 51 executionContext = value; 52 OnExecutionContextChanged(); 50 if (value != executionContexts.Value.Value) { 51 executionContexts.Value.Value = value; 53 52 } 54 53 } 55 54 } 56 55 57 /// <summary>58 /// Flag whether the current instance has been canceled.59 /// </summary>60 56 private bool canceled; 61 /// <inheritdoc/>62 57 protected bool Canceled { 63 58 get { return canceled; } … … 72 67 [Storable] 73 68 private bool breakpoint; 74 /// <inheritdoc/>75 /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>76 69 public bool Breakpoint { 77 70 get { return breakpoint; } … … 86 79 87 80 [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 } 89 86 protected Operator(Operator original, Cloner cloner) 90 87 : base(original, cloner) { 88 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 91 89 this.canceled = original.canceled; 92 90 this.breakpoint = original.breakpoint; 93 this.executionContext = cloner.Clone<IExecutionContext>(original.executionContext);94 91 } 95 /// <summary>96 /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and97 /// the canceled flag to <c>false</c> and the name of the operator to the type name.98 /// </summary>99 92 protected Operator() 100 93 : base() { 94 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 101 95 canceled = false; 102 96 breakpoint = false; … … 104 98 protected Operator(string name) 105 99 : base(name) { 100 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 106 101 canceled = false; 107 102 breakpoint = false; … … 109 104 protected Operator(string name, ParameterCollection parameters) 110 105 : base(name, parameters) { 106 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 111 107 canceled = false; 112 108 breakpoint = false; … … 114 110 protected Operator(string name, string description) 115 111 : base(name, description) { 112 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 116 113 canceled = false; 117 114 breakpoint = false; … … 119 116 protected Operator(string name, string description, ParameterCollection parameters) 120 117 : base(name, description, parameters) { 118 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 121 119 canceled = false; 122 120 breakpoint = false; 123 121 } 124 122 125 /// <inheritdoc/>126 123 public virtual IOperation Execute(IExecutionContext context) { 127 124 try { … … 140 137 } 141 138 } 142 /// <inheritdoc/>143 /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>144 139 public void Abort() { 145 140 Canceled = true; 146 141 } 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>152 142 public abstract IOperation Apply(); 153 143 154 protected virtual void OnExecutionContextChanged() { }155 144 protected virtual void OnCanceledChanged() { } 156 /// <inheritdoc/>157 145 public event EventHandler BreakpointChanged; 158 /// <summary>159 /// Fires a new <c>BreakpointChanged</c> event.160 /// </summary>161 146 protected virtual void OnBreakpointChanged() { 162 147 if (BreakpointChanged != null) { … … 164 149 } 165 150 } 166 /// <inheritdoc/>167 151 public event EventHandler Executed; 168 /// <summary>169 /// Fires a new <c>Executed</c> event.170 /// </summary>171 152 protected virtual void OnExecuted() { 172 153 if (Executed != null) { -
branches/ParallelEngine/HeuristicLab.Parameters/3.3/Parameter.cs
r5178 r5183 22 22 using System; 23 23 using System.Drawing; 24 using System.Threading; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic;28 using System.Threading;29 28 30 29 namespace HeuristicLab.Parameters { … … 55 54 get { return dataType; } 56 55 } 57 protected Dictionary<int, IItem> cachedActualValues; 56 57 private Lazy<ThreadLocal<IItem>> cachedActualValues; 58 58 public IItem ActualValue { 59 59 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; 73 62 } 74 63 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; 80 65 SetActualValue(value); 81 66 } 82 67 } 83 private Dictionary<int, IExecutionContext> executionContexts;68 private Lazy<ThreadLocal<IExecutionContext>> executionContexts; 84 69 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; } 93 71 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; 108 75 } 109 76 } … … 111 78 112 79 [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); 116 84 } 117 85 protected Parameter(Parameter original, Cloner cloner) 118 86 : base(original, cloner) { 119 87 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); 125 90 } 126 91 protected Parameter() 127 92 : base("Anonymous") { 128 93 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); 131 96 } 132 97 protected Parameter(string name, Type dataType) … … 134 99 if (dataType == null) throw new ArgumentNullException(); 135 100 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); 138 103 } 139 104 protected Parameter(string name, string description, Type dataType) … … 141 106 if (dataType == null) throw new ArgumentNullException(); 142 107 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); 145 110 } 146 111
Note: See TracChangeset
for help on using the changeset viewer.