Changeset 15204 for branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4
- Timestamp:
- 07/11/17 21:58:59 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs
r15190 r15204 40 40 [StorableClass] 41 41 public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent { 42 private SemaphoreSlim ticket;43 private ManualResetEventSlim signal;42 private SemaphoreSlim availableWorkers; // limits the number of concurrent algorithm executions 43 private ManualResetEventSlim allAlgorithmsFinished; // this indicates that all started algorithms have been paused or stopped 44 44 45 45 public CrossValidation() … … 311 311 312 312 OnStarted(); 313 ticket = new SemaphoreSlim(NumberOfWorkers.Value);314 signal= new ManualResetEventSlim(false);313 availableWorkers = new SemaphoreSlim(NumberOfWorkers.Value, NumberOfWorkers.Value); 314 allAlgorithmsFinished = new ManualResetEventSlim(false); 315 315 316 316 //start prepared or paused cloned algorithms 317 317 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { 318 if (pausePending || stopPending ) break;318 if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break; 319 319 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || 320 320 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 321 ticket.Wait();321 availableWorkers.Wait(); 322 322 lock (locker) { 323 if (pausePending || stopPending ) break;323 if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break; 324 324 clonedAlgorithm.StartAsync(cancellationToken); 325 325 } … … 327 327 } 328 328 329 signal.Wait(); 330 if (pausePending) OnPaused(); 331 else OnStopped(); 329 allAlgorithmsFinished.Wait(); 332 330 } 333 331 … … 343 341 if (!pausePending) { 344 342 pausePending = true; 345 var toPause = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started);346 if (toPause.Any()) {343 lock (locker) { 344 var toPause = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started); 347 345 foreach (var optimizer in toPause) { 348 346 // a race-condition may occur when the optimizer has changed the state by itself in the meantime … … 350 348 } 351 349 } 352 if (ExecutionState != ExecutionState.Paused) OnPaused();353 350 } 354 351 } … … 361 358 if (!stopPending) { 362 359 stopPending = true; 363 var toStop = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused);364 if (toStop.Any()) {360 lock (locker) { 361 var toStop = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused); 365 362 foreach (var optimizer in toStop) { 366 363 // a race-condition may occur when the optimizer has changed the state by itself in the meantime … … 368 365 } 369 366 } 370 if (ExecutionState != ExecutionState.Stopped) OnStopped();371 367 } 372 368 } … … 656 652 } 657 653 private void ClonedAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 654 Pause(); 658 655 OnExceptionOccurred(e.Value); 659 656 } … … 674 671 private void ClonedAlgorithm_Paused(object sender, EventArgs e) { 675 672 lock (locker) { 676 if (ExecutionState != ExecutionState.Paused) { 677 if (clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started)) { 678 pausePending = true; 679 signal.Set(); 680 ticket.Release(); 681 } 673 availableWorkers.Release(); 674 if (clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started)) { 675 OnPaused(); 676 allAlgorithmsFinished.Set(); 682 677 } 683 678 } … … 686 681 private void ClonedAlgorithm_Stopped(object sender, EventArgs e) { 687 682 lock (locker) { 688 if (ExecutionState != ExecutionState.Stopped) { 689 if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped || stopPending && alg.ExecutionState == ExecutionState.Prepared)) { 690 stopPending = true; 691 signal.Set(); 692 } 693 ticket.Release(); 683 // if the algorithm was in paused state, its worker has already been released 684 if (availableWorkers.CurrentCount < NumberOfWorkers.Value) 685 availableWorkers.Release(); 686 if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped)) { 687 OnStopped(); 688 allAlgorithmsFinished.Set(); 689 } else if (stopPending && clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped)) { 690 OnStopped(); 691 allAlgorithmsFinished.Set(); 694 692 } 695 693 }
Note: See TracChangeset
for help on using the changeset viewer.