Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/03/11 00:46:55 (14 years ago)
Author:
swagner
Message:

Merged ParallelEngine branch back into trunk (#1333)

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Core/3.3/Engine.cs

    r4722 r5193  
    2323using System.Collections.Generic;
    2424using System.Threading;
     25using System.Threading.Tasks;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4243    }
    4344
    44     private bool pausePending, stopPending;
     45    #region Variables for communication between threads
     46    private CancellationTokenSource cancellationTokenSource;
     47    private bool stopPending;
    4548    private DateTime lastUpdateTime;
    46     private System.Timers.Timer timer;
     49    #endregion
    4750
    4851    [StorableConstructor]
    49     protected Engine(bool deserializing)
    50       : base(deserializing) {
    51       pausePending = stopPending = false;
    52       timer = new System.Timers.Timer(100);
    53       timer.AutoReset = true;
    54       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    55     }
     52    protected Engine(bool deserializing) : base(deserializing) { }
    5653    protected Engine(Engine original, Cloner cloner)
    5754      : base(original, cloner) {
     
    6259      for (int i = contexts.Length - 1; i >= 0; i--)
    6360        executionStack.Push(cloner.Clone(contexts[i]));
    64       pausePending = original.pausePending;
    65       stopPending = original.stopPending;
    66       timer = new System.Timers.Timer(100);
    67       timer.AutoReset = true;
    68       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    6961    }
    7062    protected Engine()
     
    7264      log = new Log();
    7365      executionStack = new Stack<IOperation>();
    74       pausePending = stopPending = false;
    75       timer = new System.Timers.Timer(100);
    76       timer.AutoReset = true;
    77       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    7866    }
    7967
     
    9785    public override void Start() {
    9886      base.Start();
    99       ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
     87      cancellationTokenSource = new CancellationTokenSource();
     88      stopPending = false;
     89      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     90      task.ContinueWith(t => {
     91        try {
     92          t.Wait();
     93        }
     94        catch (AggregateException ex) {
     95          try {
     96            ex.Flatten().Handle(x => x is OperationCanceledException);
     97          }
     98          catch (AggregateException remaining) {
     99            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
     100            else OnExceptionOccurred(remaining);
     101          }
     102        }
     103        cancellationTokenSource.Dispose();
     104        cancellationTokenSource = null;
     105        if (stopPending) executionStack.Clear();
     106        if (executionStack.Count == 0) OnStopped();
     107        else OnPaused();
     108      });
    100109    }
    101110    protected override void OnStarted() {
     
    106115    public override void Pause() {
    107116      base.Pause();
    108       pausePending = true;
     117      cancellationTokenSource.Cancel();
    109118    }
    110119    protected override void OnPaused() {
     
    115124    public override void Stop() {
    116125      base.Stop();
    117       stopPending = true;
    118       if (ExecutionState == ExecutionState.Paused) OnStopped();
     126      if (ExecutionState == ExecutionState.Paused) {
     127        executionStack.Clear();
     128        OnStopped();
     129      } else {
     130        stopPending = true;
     131        cancellationTokenSource.Cancel();
     132      }
    119133    }
    120134    protected override void OnStopped() {
     
    129143
    130144    private void Run(object state) {
     145      CancellationToken cancellationToken = (CancellationToken)state;
     146
    131147      OnStarted();
    132       pausePending = stopPending = false;
     148      lastUpdateTime = DateTime.Now;
     149      System.Timers.Timer timer = new System.Timers.Timer(100);
     150      timer.AutoReset = true;
     151      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
     152      timer.Start();
     153      try {
     154        Run(cancellationToken);
     155      }
     156      finally {
     157        timer.Stop();
     158        timer.Dispose();
     159        ExecutionTime += DateTime.Now - lastUpdateTime;
     160      }
    133161
    134       lastUpdateTime = DateTime.Now;
    135       timer.Start();
    136       while (!pausePending && !stopPending && (executionStack.Count > 0)) {
    137         ProcessNextOperation();
    138       }
    139       timer.Stop();
    140       ExecutionTime += DateTime.Now - lastUpdateTime;
    141 
    142       if (pausePending) OnPaused();
    143       else OnStopped();
     162      cancellationToken.ThrowIfCancellationRequested();
    144163    }
    145 
    146     protected abstract void ProcessNextOperation();
     164    protected abstract void Run(CancellationToken cancellationToken);
    147165
    148166    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperator.cs

    r2845 r5193  
    2121
    2222using System;
     23using System.Threading;
    2324
    2425namespace HeuristicLab.Core {
    2526  /// <summary>
    26   /// Interface to represent an operator (e.g. GreaterThanComparator,...),
    27   /// a basic instruction of an algorithm.
     27  /// Interface to represent an operator.
    2828  /// </summary>
    2929  public interface IOperator : IParameterizedNamedItem {
    30     /// <summary>
    31     /// Gets or sets a boolean value whether the engine should stop here during the run.
    32     /// </summary>
    3330    bool Breakpoint { get; set; }
    3431
    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     IOperation Execute(IExecutionContext context);
    41     /// <summary>
    42     /// Aborts the current operator.
    43     /// </summary>
    44     void Abort();
     32    IOperation Execute(IExecutionContext context, CancellationToken cancellationToken);
    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  }
Note: See TracChangeset for help on using the changeset viewer.