Changeset 15373
- Timestamp:
- 09/19/17 15:58:21 (7 years ago)
- Location:
- branches/ParallelExperiment
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ParallelExperiment/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Optimization (added) merged: 15327,15339,15367
- Property svn:mergeinfo changed
-
branches/ParallelExperiment/HeuristicLab.Optimization.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Optimization.Views (added) merged: 15367
- Property svn:mergeinfo changed
-
branches/ParallelExperiment/HeuristicLab.Optimization.Views/3.3/IOptimizerView.cs
r15287 r15373 136 136 137 137 #region Control events 138 protected virtual void startButton_Click(object sender, EventArgs e) {139 Content.StartAsync();138 protected virtual async void startButton_Click(object sender, EventArgs e) { 139 await Content.StartAsync(); 140 140 } 141 141 protected virtual void pauseButton_Click(object sender, EventArgs e) { -
branches/ParallelExperiment/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15337 r15373 82 82 } catch (OperationCanceledException) { 83 83 } catch (AggregateException ae) { 84 OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae);84 ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e)); 85 85 } catch (Exception e) { 86 86 OnExceptionOccurred(e); -
branches/ParallelExperiment/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15337 r15373 122 122 private bool experimentStopped = false; 123 123 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 124 private readonly ManualResetEventSlim allOptimizerFinished = new ManualResetEventSlim(false); // this indicates that all started optimizers have been paused or stopped 126 125 127 126 public Experiment() … … 202 201 Start(CancellationToken.None); 203 202 } 204 205 203 public void Start(CancellationToken cancellationToken) { 206 204 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 207 205 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 208 if ( Optimizers.Count == 0) return;206 if (!Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) return; 209 207 210 208 experimentStarted = true; 211 209 experimentStopped = false; 212 allOptimizerFinished = new ManualResetEventSlim(false); 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) { } 228 } 229 230 allOptimizerFinished.Wait(); 210 211 var startedTasks = new List<Task>(); 212 using (var availableWorkers = new SemaphoreSlim(NumberOfWorkers, NumberOfWorkers)) { 213 var runnableOptimizers = Optimizers.Where(o => o.ExecutionState == ExecutionState.Prepared || o.ExecutionState == ExecutionState.Paused).ToList(); 214 if (!runnableOptimizers.Any()) return; 215 216 while (runnableOptimizers.Any()) { 217 try { 218 availableWorkers.Wait(cancellationToken); 219 if (experimentStopped || !experimentStarted) break; 220 // some optimizers might be started manually somewhere else 221 runnableOptimizers.RemoveAll(o => !(o.ExecutionState == ExecutionState.Prepared || o.ExecutionState == ExecutionState.Paused)); 222 if (runnableOptimizers.Any()) { 223 var optimizer = runnableOptimizers.First(); 224 runnableOptimizers.Remove(optimizer); 225 var startedTask = optimizer.StartAsync(cancellationToken).ContinueWith(x => { 226 availableWorkers.Release(); // is not disposed yet because Task.WaitAll at the end of the using is blocking 227 }); 228 startedTasks.Add(startedTask); 229 } 230 } catch (InvalidOperationException) { } catch (OperationCanceledException) { } 231 } 232 233 allOptimizerFinished.Wait(); 234 Task.WaitAll(startedTasks.ToArray()); // retreive exeptions of the asyncrounously started optimizer 235 } 231 236 } 232 237 public async Task StartAsync() { await StartAsync(CancellationToken.None); } … … 294 299 private void OnPaused() { 295 300 ExecutionState = ExecutionState.Paused; 301 allOptimizerFinished.Set(); 296 302 EventHandler handler = Paused; 297 303 if (handler != null) handler(this, EventArgs.Empty); 298 allOptimizerFinished.Set();299 304 } 300 305 public event EventHandler Stopped; 301 306 private void OnStopped() { 302 307 ExecutionState = ExecutionState.Stopped; 308 allOptimizerFinished.Set(); 303 309 EventHandler handler = Stopped; 304 310 if (handler != null) handler(this, EventArgs.Empty); 305 allOptimizerFinished.Set();306 311 } 307 312 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; … … 398 403 private void optimizer_Paused(object sender, EventArgs e) { 399 404 lock (locker) { 400 if (availableWorkers.CurrentCount < NumberOfWorkers)401 availableWorkers.Release();402 405 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) { 403 406 OnPaused(); … … 415 418 private void optimizer_Stopped(object sender, EventArgs e) { 416 419 lock (locker) { 417 if (availableWorkers.CurrentCount < NumberOfWorkers)418 availableWorkers.Release();419 420 if (experimentStopped) { 420 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) { 421 OnStopped(); 422 } 421 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 423 422 } else { 424 423 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return;
Note: See TracChangeset
for help on using the changeset viewer.