Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15337


Ignore:
Timestamp:
08/22/17 14:08:52 (7 years ago)
Author:
pfleck
Message:

#2822 Added parallel execution support for Experiment (similar to CrossValidation).

  • Fixed race-condition in BasicAlgorithm (potential NullReference of CancellationTokenSource).
  • Fixed race-condition in BatchRun (calling .Stop potentially fires both OnStopped and OnPaused).
Location:
branches/ParallelExperiment
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/ParallelExperiment/HeuristicLab.Optimization.Views/3.3/ExperimentView.Designer.cs

    r14185 r15337  
    4747      this.tabControl = new HeuristicLab.MainForm.WindowsForms.DragOverTabControl();
    4848      this.optimizersTabPage = new System.Windows.Forms.TabPage();
     49      this.experimentTreeView = new HeuristicLab.Optimization.Views.ExperimentTreeView();
    4950      this.runsTabPage = new System.Windows.Forms.TabPage();
    5051      this.runsViewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
    51       this.experimentTreeView = new HeuristicLab.Optimization.Views.ExperimentTreeView();
     52      this.workersNumericUpDown = new System.Windows.Forms.NumericUpDown();
     53      this.workersLabel = new System.Windows.Forms.Label();
    5254      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    5355      this.tabControl.SuspendLayout();
    5456      this.optimizersTabPage.SuspendLayout();
    5557      this.runsTabPage.SuspendLayout();
     58      ((System.ComponentModel.ISupportInitialize)(this.workersNumericUpDown)).BeginInit();
    5659      this.SuspendLayout();
    5760      //
     
    119122      this.optimizersTabPage.Text = "Optimizers";
    120123      this.optimizersTabPage.UseVisualStyleBackColor = true;
     124      //
     125      // experimentTreeView
     126      //
     127      this.experimentTreeView.Caption = "Experiment View";
     128      this.experimentTreeView.Content = null;
     129      this.experimentTreeView.Dock = System.Windows.Forms.DockStyle.Fill;
     130      this.experimentTreeView.Location = new System.Drawing.Point(3, 3);
     131      this.experimentTreeView.Name = "experimentTreeView";
     132      this.experimentTreeView.ReadOnly = false;
     133      this.experimentTreeView.Size = new System.Drawing.Size(665, 395);
     134      this.experimentTreeView.TabIndex = 0;
    121135      //
    122136      // runsTabPage
     
    147161      this.runsViewHost.ViewType = null;
    148162      //
    149       // experimentTreeView
    150       //
    151       this.experimentTreeView.Caption = "Experiment View";
    152       this.experimentTreeView.Content = null;
    153       this.experimentTreeView.Dock = System.Windows.Forms.DockStyle.Fill;
    154       this.experimentTreeView.Location = new System.Drawing.Point(3, 3);
    155       this.experimentTreeView.Name = "experimentTreeView";
    156       this.experimentTreeView.ReadOnly = false;
    157       this.experimentTreeView.Size = new System.Drawing.Size(665, 395);
    158       this.experimentTreeView.TabIndex = 0;
     163      // workersNumericUpDown
     164      //
     165      this.workersNumericUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
     166      this.workersNumericUpDown.Location = new System.Drawing.Point(193, 461);
     167      this.workersNumericUpDown.Minimum = new decimal(new int[] {
     168            1,
     169            0,
     170            0,
     171            0});
     172      this.workersNumericUpDown.Name = "workersNumericUpDown";
     173      this.workersNumericUpDown.Size = new System.Drawing.Size(59, 20);
     174      this.workersNumericUpDown.TabIndex = 16;
     175      this.workersNumericUpDown.Value = new decimal(new int[] {
     176            1,
     177            0,
     178            0,
     179            0});
     180      this.workersNumericUpDown.ValueChanged += new System.EventHandler(this.workersNumericUpDown_ValueChanged);
     181      //
     182      // workersLabel
     183      //
     184      this.workersLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
     185      this.workersLabel.AutoSize = true;
     186      this.workersLabel.Location = new System.Drawing.Point(140, 464);
     187      this.workersLabel.Name = "workersLabel";
     188      this.workersLabel.Size = new System.Drawing.Size(53, 13);
     189      this.workersLabel.TabIndex = 17;
     190      this.workersLabel.Text = "Workers: ";
    159191      //
    160192      // ExperimentView
    161193      //
    162       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    163194      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
     195      this.Controls.Add(this.workersLabel);
     196      this.Controls.Add(this.workersNumericUpDown);
    164197      this.Controls.Add(this.tabControl);
    165198      this.Name = "ExperimentView";
     
    175208      this.Controls.SetChildIndex(this.nameLabel, 0);
    176209      this.Controls.SetChildIndex(this.nameTextBox, 0);
     210      this.Controls.SetChildIndex(this.workersNumericUpDown, 0);
     211      this.Controls.SetChildIndex(this.workersLabel, 0);
    177212      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit();
    178213      this.tabControl.ResumeLayout(false);
    179214      this.optimizersTabPage.ResumeLayout(false);
    180215      this.runsTabPage.ResumeLayout(false);
     216      ((System.ComponentModel.ISupportInitialize)(this.workersNumericUpDown)).EndInit();
    181217      this.ResumeLayout(false);
    182218      this.PerformLayout();
     
    190226    private HeuristicLab.MainForm.WindowsForms.ViewHost runsViewHost;
    191227    private ExperimentTreeView experimentTreeView;
    192 
     228    private System.Windows.Forms.NumericUpDown workersNumericUpDown;
     229    private System.Windows.Forms.Label workersLabel;
    193230  }
    194231}
  • branches/ParallelExperiment/HeuristicLab.Optimization.Views/3.3/ExperimentView.cs

    r14185 r15337  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using System.Windows.Forms;
     
    4344        experimentTreeView.Content = null;
    4445        runsViewHost.Content = null;
     46        workersNumericUpDown.Value = 1;
    4547      } else {
    4648        experimentTreeView.Content = Content;
    4749        runsViewHost.Content = Content.Runs;
     50        workersNumericUpDown.Value = Content.NumberOfWorkers;
    4851      }
    4952    }
     
    6669      base.OnClosed(e);
    6770    }
     71
     72    protected override void Content_ExecutionStateChanged(object sender, EventArgs e) {
     73      base.Content_ExecutionStateChanged(sender, e);
     74      workersNumericUpDown.Enabled = Content.ExecutionState != ExecutionState.Started;
     75    }
     76
     77    #region Events
     78    private void workersNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
     79      Content.NumberOfWorkers = (int)workersNumericUpDown.Value;
     80    }
     81    #endregion
    6882  }
    6983}
  • branches/ParallelExperiment/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r15302 r15337  
    8080      try {
    8181        Run((object)cancellationTokenSource.Token);
    82       }
    83       catch (OperationCanceledException) {
    84       }
    85       catch (AggregateException ae) {
     82      } catch (OperationCanceledException) {
     83      } catch (AggregateException ae) {
    8684        OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae);
    87       }
    88       catch (Exception e) {
     85      } catch (Exception e) {
    8986        OnExceptionOccurred(e);
    9087      }
     
    104101      base.Pause();
    105102      pausePending = true;
    106       CancellationTokenSource.Cancel();
     103      CancellationTokenSource?.Cancel();
    107104    }
    108105
     
    112109      base.Stop();
    113110      if (ExecutionState == ExecutionState.Paused) OnStopped();
    114       else CancellationTokenSource.Cancel();
     111      else CancellationTokenSource?.Cancel();
    115112    }
    116113
     
    127124        initialized = true;
    128125        Run(cancellationToken);
    129       }
    130       finally {
     126      } finally {
    131127        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    132128        timer.Stop();
  • branches/ParallelExperiment/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs

    r15287 r15337  
    258258        if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
    259259        Optimizer.Prepare();
     260        if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
    260261      }
    261262    }
     
    271272      if (Optimizer.ExecutionState != ExecutionState.Started) return;
    272273      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    273       try { Optimizer.Pause(); } catch (InvalidOperationException) { }
     274      try { Optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { }
    274275    }
    275276    public void Stop() {
     
    385386    }
    386387    private void Optimizer_Prepared(object sender, EventArgs e) {
    387       if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
     388      if (batchRunAction == BatchRunAction.Prepare/* || ExecutionState == ExecutionState.Stopped*/) {
    388389        ExecutionTime = TimeSpan.Zero;
    389390        runsExecutionTime = TimeSpan.Zero;
  • branches/ParallelExperiment/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r15328 r15337  
    9797    }
    9898
     99    [Storable]
     100    private int numberOfWorkers = 1;
     101    public int NumberOfWorkers {
     102      get { return numberOfWorkers; }
     103      set {
     104        if (value < 1) throw new ArgumentException("Number of Workers must not be lower than one.");
     105        numberOfWorkers = value;
     106      }
     107    }
     108
    99109    public IEnumerable<IOptimizer> NestedOptimizers {
    100110      get {
     
    111121    private bool experimentStarted = false;
    112122    private bool experimentStopped = false;
     123
    113124    private ManualResetEventSlim allOptimizerFinished; // this indicates that all started optimizers have been paused or stopped
     125    private SemaphoreSlim availableWorkers; // limits the number of concurrent optimizer executions
    114126
    115127    public Experiment()
     
    155167      experimentStarted = original.experimentStarted;
    156168      experimentStopped = original.experimentStopped;
     169      numberOfWorkers = original.numberOfWorkers;
    157170      Initialize();
    158171    }
     
    189202      Start(CancellationToken.None);
    190203    }
     204
    191205    public void Start(CancellationToken cancellationToken) {
    192206      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     
    197211      experimentStopped = false;
    198212      allOptimizerFinished = new ManualResetEventSlim(false);
    199 
    200       IOptimizer optimizer;
    201       while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) {
    202         // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    203         try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { }
    204         if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
     213      availableWorkers = new SemaphoreSlim(NumberOfWorkers, NumberOfWorkers);
     214
     215      var runnableOptimizers = Optimizers.Where(o => o.ExecutionState == ExecutionState.Prepared || o.ExecutionState == ExecutionState.Paused).ToList();
     216      if (!runnableOptimizers.Any()) return;
     217
     218      while (runnableOptimizers.Any()) {
     219        try {
     220          availableWorkers.Wait(cancellationToken); // an optimizer was pause/stopped previously
     221          if (experimentStopped || !experimentStarted) break;
     222          // some optimizers might be started manually somewhere else
     223          runnableOptimizers.RemoveAll(o => !(o.ExecutionState == ExecutionState.Prepared || o.ExecutionState == ExecutionState.Paused));
     224          var optimizer = runnableOptimizers.FirstOrDefault();
     225          runnableOptimizers.Remove(optimizer);
     226          optimizer?.StartAsync(cancellationToken);
     227        } catch (InvalidOperationException) { } catch (OperationCanceledException) { }
    205228      }
    206229
     
    220243      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
    221244        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    222         try { optimizer.Pause(); } catch (InvalidOperationException) { }
     245        try { optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { }
    223246      }
    224247    }
     
    273296      EventHandler handler = Paused;
    274297      if (handler != null) handler(this, EventArgs.Empty);
     298      allOptimizerFinished.Set();
    275299    }
    276300    public event EventHandler Stopped;
     
    279303      EventHandler handler = Stopped;
    280304      if (handler != null) handler(this, EventArgs.Empty);
     305      allOptimizerFinished.Set();
    281306    }
    282307    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
     
    372397    }
    373398    private void optimizer_Paused(object sender, EventArgs e) {
    374       lock (locker)
     399      lock (locker) {
     400        if (availableWorkers.CurrentCount < NumberOfWorkers)
     401          availableWorkers.Release();
    375402        if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) {
    376403          OnPaused();
    377           allOptimizerFinished.Set();
    378         }
     404        }
     405      }
    379406    }
    380407    private void optimizer_Prepared(object sender, EventArgs e) {
     
    388415    private void optimizer_Stopped(object sender, EventArgs e) {
    389416      lock (locker) {
     417        if (availableWorkers.CurrentCount < NumberOfWorkers)
     418          availableWorkers.Release();
    390419        if (experimentStopped) {
    391420          if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) {
    392421            OnStopped();
    393             allOptimizerFinished.Set();
    394422          }
    395423        } else {
     
    397425          else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) {
    398426            OnStopped();
    399             allOptimizerFinished.Set();
    400427          } else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) {
    401428            OnPaused();
    402             allOptimizerFinished.Set();
    403429          }
    404430        }
Note: See TracChangeset for help on using the changeset viewer.