Changeset 15337 for branches/ParallelExperiment/HeuristicLab.Optimization
- Timestamp:
- 08/22/17 14:08:52 (7 years ago)
- Location:
- branches/ParallelExperiment/HeuristicLab.Optimization/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ParallelExperiment/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15302 r15337 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 OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae); 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(); -
branches/ParallelExperiment/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r15287 r15337 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() { … … 385 386 } 386 387 private void Optimizer_Prepared(object sender, EventArgs e) { 387 if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {388 if (batchRunAction == BatchRunAction.Prepare/* || ExecutionState == ExecutionState.Stopped*/) { 388 389 ExecutionTime = TimeSpan.Zero; 389 390 runsExecutionTime = TimeSpan.Zero; -
branches/ParallelExperiment/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15328 r15337 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 ManualResetEventSlim allOptimizerFinished; // this indicates that all started optimizers have been paused or stopped 125 private SemaphoreSlim availableWorkers; // limits the number of concurrent optimizer executions 114 126 115 127 public Experiment() … … 155 167 experimentStarted = original.experimentStarted; 156 168 experimentStopped = original.experimentStopped; 169 numberOfWorkers = original.numberOfWorkers; 157 170 Initialize(); 158 171 } … … 189 202 Start(CancellationToken.None); 190 203 } 204 191 205 public void Start(CancellationToken cancellationToken) { 192 206 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) … … 197 211 experimentStopped = false; 198 212 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) { } 205 228 } 206 229 … … 220 243 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 221 244 // 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) { } 223 246 } 224 247 } … … 273 296 EventHandler handler = Paused; 274 297 if (handler != null) handler(this, EventArgs.Empty); 298 allOptimizerFinished.Set(); 275 299 } 276 300 public event EventHandler Stopped; … … 279 303 EventHandler handler = Stopped; 280 304 if (handler != null) handler(this, EventArgs.Empty); 305 allOptimizerFinished.Set(); 281 306 } 282 307 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; … … 372 397 } 373 398 private void optimizer_Paused(object sender, EventArgs e) { 374 lock (locker) 399 lock (locker) { 400 if (availableWorkers.CurrentCount < NumberOfWorkers) 401 availableWorkers.Release(); 375 402 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) { 376 403 OnPaused(); 377 allOptimizerFinished.Set();378 404 } 405 } 379 406 } 380 407 private void optimizer_Prepared(object sender, EventArgs e) { … … 388 415 private void optimizer_Stopped(object sender, EventArgs e) { 389 416 lock (locker) { 417 if (availableWorkers.CurrentCount < NumberOfWorkers) 418 availableWorkers.Release(); 390 419 if (experimentStopped) { 391 420 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) { 392 421 OnStopped(); 393 allOptimizerFinished.Set();394 422 } 395 423 } else { … … 397 425 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) { 398 426 OnStopped(); 399 allOptimizerFinished.Set();400 427 } else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) { 401 428 OnPaused(); 402 allOptimizerFinished.Set();403 429 } 404 430 }
Note: See TracChangeset
for help on using the changeset viewer.