Changeset 15408
- Timestamp:
- 10/06/17 14:13:21 (7 years ago)
- Location:
- trunk/sources
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/branches/ParallelExperiment/HeuristicLab.Optimization (added) merged: 15337,15373,15375,15389,15394,15407
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Optimization.Views
- Property svn:mergeinfo changed
/branches/ParallelExperiment/HeuristicLab.Optimization.Views (added) merged: 15337,15373,15407
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Optimization.Views/3.3/ExperimentView.Designer.cs
r14185 r15408 47 47 this.tabControl = new HeuristicLab.MainForm.WindowsForms.DragOverTabControl(); 48 48 this.optimizersTabPage = new System.Windows.Forms.TabPage(); 49 this.experimentTreeView = new HeuristicLab.Optimization.Views.ExperimentTreeView(); 49 50 this.runsTabPage = new System.Windows.Forms.TabPage(); 50 51 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(); 52 54 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); 53 55 this.tabControl.SuspendLayout(); 54 56 this.optimizersTabPage.SuspendLayout(); 55 57 this.runsTabPage.SuspendLayout(); 58 ((System.ComponentModel.ISupportInitialize)(this.workersNumericUpDown)).BeginInit(); 56 59 this.SuspendLayout(); 57 60 // … … 119 122 this.optimizersTabPage.Text = "Optimizers"; 120 123 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; 121 135 // 122 136 // runsTabPage … … 147 161 this.runsViewHost.ViewType = null; 148 162 // 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: "; 159 191 // 160 192 // ExperimentView 161 193 // 162 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);163 194 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 195 this.Controls.Add(this.workersLabel); 196 this.Controls.Add(this.workersNumericUpDown); 164 197 this.Controls.Add(this.tabControl); 165 198 this.Name = "ExperimentView"; … … 175 208 this.Controls.SetChildIndex(this.nameLabel, 0); 176 209 this.Controls.SetChildIndex(this.nameTextBox, 0); 210 this.Controls.SetChildIndex(this.workersNumericUpDown, 0); 211 this.Controls.SetChildIndex(this.workersLabel, 0); 177 212 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit(); 178 213 this.tabControl.ResumeLayout(false); 179 214 this.optimizersTabPage.ResumeLayout(false); 180 215 this.runsTabPage.ResumeLayout(false); 216 ((System.ComponentModel.ISupportInitialize)(this.workersNumericUpDown)).EndInit(); 181 217 this.ResumeLayout(false); 182 218 this.PerformLayout(); … … 190 226 private HeuristicLab.MainForm.WindowsForms.ViewHost runsViewHost; 191 227 private ExperimentTreeView experimentTreeView; 192 228 private System.Windows.Forms.NumericUpDown workersNumericUpDown; 229 private System.Windows.Forms.Label workersLabel; 193 230 } 194 231 } -
trunk/sources/HeuristicLab.Optimization.Views/3.3/ExperimentView.cs
r14185 r15408 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using System.Windows.Forms; … … 43 44 experimentTreeView.Content = null; 44 45 runsViewHost.Content = null; 46 workersNumericUpDown.Value = 1; 45 47 } else { 46 48 experimentTreeView.Content = Content; 47 49 runsViewHost.Content = Content.Runs; 50 workersNumericUpDown.Value = Content.NumberOfWorkers; 48 51 } 49 52 } … … 66 69 base.OnClosed(e); 67 70 } 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 68 82 } 69 83 } -
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15367 r15408 80 80 try { 81 81 Run((object)cancellationTokenSource.Token); 82 } 83 catch (OperationCanceledException) { 84 } 85 catch (AggregateException ae) { 82 } catch (OperationCanceledException) { 83 } catch (AggregateException ae) { 86 84 ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e)); 87 } 88 catch (Exception e) { 85 } catch (Exception e) { 89 86 OnExceptionOccurred(e); 90 87 } … … 104 101 base.Pause(); 105 102 pausePending = true; 106 CancellationTokenSource .Cancel();103 CancellationTokenSource?.Cancel(); 107 104 } 108 105 … … 112 109 base.Stop(); 113 110 if (ExecutionState == ExecutionState.Paused) OnStopped(); 114 else CancellationTokenSource .Cancel();111 else CancellationTokenSource?.Cancel(); 115 112 } 116 113 … … 127 124 initialized = true; 128 125 Run(cancellationToken); 129 } 130 finally { 126 } finally { 131 127 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 132 128 timer.Stop(); -
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r15287 r15408 258 258 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 259 259 Optimizer.Prepare(); 260 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 260 261 } 261 262 } … … 271 272 if (Optimizer.ExecutionState != ExecutionState.Started) return; 272 273 // 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) { } 274 275 } 275 276 public void Stop() { -
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15339 r15408 97 97 } 98 98 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 99 109 public IEnumerable<IOptimizer> NestedOptimizers { 100 110 get { … … 111 121 private bool experimentStarted = false; 112 122 private bool experimentStopped = false; 123 113 124 private readonly ManualResetEventSlim allOptimizerFinished = new ManualResetEventSlim(false); // this indicates that all started optimizers have been paused or stopped 114 125 … … 155 166 experimentStarted = original.experimentStarted; 156 167 experimentStopped = original.experimentStopped; 168 numberOfWorkers = original.numberOfWorkers; 157 169 Initialize(); 158 170 } … … 192 204 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 193 205 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 194 if (!Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) return; 206 207 // Multiple enumerations of runnableOptimizers is done on purpose to get the latest list of runnnable optimizers lazily 208 var runnableOptimizers = Optimizers.Where(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused); 209 if (!runnableOptimizers.Any()) return; 195 210 196 211 experimentStarted = true; 197 212 experimentStopped = false; 198 allOptimizerFinished.Reset(); 199 IOptimizer optimizer; 200 while ((optimizer = Optimizers.FirstOrDefault(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) != null) { 201 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 202 try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { } 203 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 204 } 205 allOptimizerFinished.Wait(); 213 214 var startedOptimizers = new Dictionary<IOptimizer, Task>(); // track already started optimizers (.StartAsync does not set the executionstate immediately) 215 using (var availableWorkers = new SemaphoreSlim(NumberOfWorkers, NumberOfWorkers)) { 216 while (runnableOptimizers.Any(o => !startedOptimizers.ContainsKey(o))) { 217 try { 218 availableWorkers.Wait(cancellationToken); 219 if (experimentStopped || !experimentStarted) break; 220 var optimizer = runnableOptimizers.FirstOrDefault(o => !startedOptimizers.ContainsKey(o)); 221 if (optimizer != null) { 222 var startedTask = optimizer.StartAsync(cancellationToken).ContinueWith(async t => { 223 availableWorkers.Release(); // is guaranteed to be not disposed yet because Task.WaitAll blocks before the end of the using 224 await t; // trigger a potential exception on the optimizerTask 225 }); 226 startedOptimizers.Add(optimizer, startedTask.Unwrap()); // unwrap task because lambda of .ContinueWith is async 227 } 228 } catch (InvalidOperationException) { } catch (OperationCanceledException) { } 229 } 230 231 Task.WaitAll(startedOptimizers.Values.ToArray()); // retreive exeptions of the asyncrounously started optimizer 232 allOptimizerFinished.Wait(); 233 } 206 234 } 207 235 public async Task StartAsync() { await StartAsync(CancellationToken.None); } … … 218 246 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 219 247 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 220 try { optimizer.Pause(); } catch (InvalidOperationException) { } 248 try { optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { } 221 249 } 222 250 } … … 372 400 } 373 401 private void optimizer_Paused(object sender, EventArgs e) { 374 lock (locker) 375 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 402 lock (locker) { 403 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) { 404 OnPaused(); 405 } 406 experimentStarted = false; 407 } 376 408 } 377 409 private void optimizer_Prepared(object sender, EventArgs e) { … … 389 421 } else { 390 422 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return; 391 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 392 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 423 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) { 424 OnStopped(); 425 } else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) { 426 OnPaused(); 427 } 393 428 } 394 429 }
Note: See TracChangeset
for help on using the changeset viewer.