Changeset 15292 for stable/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 07/28/17 16:47:42 (7 years ago)
- Location:
- stable
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/branches/Async (added) merged: 13329,13349,13354-13355,13401,15065,15190,15204,15212,15215-15216,15232,15280-15281,15284,15286 /trunk/sources merged: 15287
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs
r15150 r15292 25 25 using System.Linq; 26 26 using System.Threading; 27 using System.Threading.Tasks; 27 28 using HeuristicLab.Collections; 28 29 using HeuristicLab.Common; … … 42 43 [Storable] 43 44 private int seed; 45 46 private SemaphoreSlim availableWorkers; // limits the number of concurrent algorithm executions 47 private ManualResetEventSlim allAlgorithmsFinished; // this indicates that all started algorithms have been paused or stopped 44 48 45 49 public CrossValidation() … … 269 273 270 274 public void Prepare() { 275 if (startPending) return; 271 276 if (ExecutionState == ExecutionState.Started) 272 277 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); … … 283 288 } 284 289 290 private bool startPending; 285 291 public void Start() { 286 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 287 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 288 289 seed = new FastRandom().NextInt(); 290 291 if (Algorithm != null) { 292 Start(CancellationToken.None); 293 } 294 public void Start(CancellationToken cancellationToken) { 295 lock (locker) { 296 if (startPending) return; 297 startPending = true; 298 } 299 300 try { 301 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 302 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 303 seed = new FastRandom().NextInt(); 304 305 if (Algorithm == null) return; 292 306 //create cloned algorithms 293 307 if (clonedAlgorithms.Count == 0) { … … 330 344 } 331 345 332 //start prepared or paused cloned algorithms 333 int startedAlgorithms = 0; 334 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { 335 if (startedAlgorithms < NumberOfWorkers.Value) { 336 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || 337 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 338 339 // start and wait until the alg is started 340 using (var signal = new ManualResetEvent(false)) { 341 EventHandler signalSetter = (sender, args) => { signal.Set(); }; 342 clonedAlgorithm.Started += signalSetter; 343 clonedAlgorithm.Start(); 344 signal.WaitOne(); 345 clonedAlgorithm.Started -= signalSetter; 346 347 startedAlgorithms++; 348 } 349 } 346 OnStarted(); 347 } finally { 348 if (startPending) startPending = false; 349 } 350 351 availableWorkers = new SemaphoreSlim(NumberOfWorkers.Value, NumberOfWorkers.Value); 352 allAlgorithmsFinished = new ManualResetEventSlim(false); 353 354 //start prepared or paused cloned algorithms 355 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { 356 if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break; 357 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || 358 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 359 availableWorkers.Wait(); 360 lock (locker) { 361 if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break; 362 clonedAlgorithm.StartAsync(cancellationToken); 350 363 } 351 364 } 352 OnStarted(); 353 } 365 } 366 367 allAlgorithmsFinished.Wait(); 368 } 369 370 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 371 public async Task StartAsync(CancellationToken cancellationToken) { 372 await AsyncHelper.DoAsync(Start, cancellationToken); 354 373 } 355 374 356 375 private bool pausePending; 357 376 public void Pause() { 377 if (startPending) return; 358 378 if (ExecutionState != ExecutionState.Started) 359 379 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 360 380 if (!pausePending) { 361 381 pausePending = true; 362 PauseAllClonedAlgorithms();363 }364 }365 private void PauseAllClonedAlgorithms() {366 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {367 if (clonedAlgorithm.ExecutionState == ExecutionState.Started)368 clonedAlgorithm.Pause();382 lock (locker) { 383 var toPause = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started).ToList(); 384 foreach (var optimizer in toPause) { 385 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 386 try { optimizer.Pause(); } catch (InvalidOperationException) { } 387 } 388 } 369 389 } 370 390 } … … 372 392 private bool stopPending; 373 393 public void Stop() { 394 if (startPending) return; 374 395 if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused)) 375 396 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", … … 377 398 if (!stopPending) { 378 399 stopPending = true; 379 StopAllClonedAlgorithms(); 380 } 381 } 382 private void StopAllClonedAlgorithms() { 383 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { 384 if (clonedAlgorithm.ExecutionState == ExecutionState.Started || 385 clonedAlgorithm.ExecutionState == ExecutionState.Paused) 386 clonedAlgorithm.Stop(); 400 lock (locker) { 401 var toStop = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused).ToList(); 402 foreach (var optimizer in toStop) { 403 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 404 try { optimizer.Stop(); } catch (InvalidOperationException) { } 405 } 406 } 387 407 } 388 408 } … … 685 705 } 686 706 private void ClonedAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 707 Pause(); 687 708 OnExceptionOccurred(e.Value); 688 709 } … … 703 724 private void ClonedAlgorithm_Paused(object sender, EventArgs e) { 704 725 lock (locker) { 705 if (pausePending && clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started)) 726 availableWorkers.Release(); 727 if (clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started)) { 706 728 OnPaused(); 729 allAlgorithmsFinished.Set(); 730 } 707 731 } 708 732 } … … 710 734 private void ClonedAlgorithm_Stopped(object sender, EventArgs e) { 711 735 lock (locker) { 712 if (!stopPending && ExecutionState == ExecutionState.Started) { 713 IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared || 714 alg.ExecutionState == ExecutionState.Paused); 715 if (preparedAlgorithm != null) preparedAlgorithm.Start(); 716 } 717 if (ExecutionState != ExecutionState.Stopped) { 718 if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped)) 719 OnStopped(); 720 else if (stopPending && 721 clonedAlgorithms.All( 722 alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped)) 723 OnStopped(); 736 // if the algorithm was in paused state, its worker has already been released 737 if (availableWorkers.CurrentCount < NumberOfWorkers.Value) 738 availableWorkers.Release(); 739 if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped)) { 740 OnStopped(); 741 allAlgorithmsFinished.Set(); 742 } else if (stopPending && clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped)) { 743 OnStopped(); 744 allAlgorithmsFinished.Set(); 724 745 } 725 746 } … … 748 769 public event EventHandler Started; 749 770 private void OnStarted() { 771 startPending = false; 750 772 ExecutionState = ExecutionState.Started; 751 773 EventHandler handler = Started; -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs
r15061 r15292 21 21 22 22 using System; 23 using System.Linq; 24 using System.Threading; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Optimization;
Note: See TracChangeset
for help on using the changeset viewer.