public GeneticAlgorithm(IGeneticAlgorithmStateFactory geneticAlgorithmStateFactory, IStateLogger <TCandidate> logger, IGod <TCandidate> god, IExitStrategy exitStrategy, IRandomService randomService) { this.geneticAlgorithmStateFactory = geneticAlgorithmStateFactory; this.logger = logger; this.god = god; this.exitStrategy = exitStrategy; this.randomService = randomService; }
public void CountDown() { MockRepository mr = Mocks; int latchSize = 5; CountDownLatch startLatch = new CountDownLatch(0); CountDownLatch latch = new CountDownLatch(latchSize); IShard shard = mr.DynamicMock <IShard>(); IExitStrategy <object> strat = mr.CreateMock <IExitStrategy <object> >(); ICallable <object> anotherCallable = mr.DynamicMock <ICallable <object> >(); IShardOperation <object> operation = mr.DynamicMock <IShardOperation <object> >(); //Expectations and results Expect.Call(strat.AddResult(null, shard)).Return(false); Expect.Call(strat.AddResult(null, shard)).Return(false); Expect.Call(strat.AddResult(null, shard)).Return(true); Expect.Call(strat.AddResult(null, shard)).Return(true); SetupResult.For(shard.ShardIds).Return(new HashedSet <ShardId>(new ShardId[] { (new ShardId(0)) })); mr.ReplayAll(); IList <StartAwareFutureTask <object> > futureTasks = new List <StartAwareFutureTask <object> >(); ParallelShardOperationCallable <object> callable = new ParallelShardOperationCallable <object>( startLatch, latch, strat, operation, shard, futureTasks); callable.Call(); // addResult returns false so latch is only decremented by 1 Assert.AreEqual(latchSize - 1, latch.Count); // addResult returns false so latch is only decremented by 1 callable.Call(); Assert.AreEqual(latchSize - 2, latch.Count); StartAwareFutureTask <object> ft = new StartAwareFutureTask_CancelReturnFalse(anotherCallable, 0); futureTasks.Add(ft); callable.Call(); // cancelling the 1 task returns false, so latch is only decremented by 1 Assert.AreEqual(latchSize - 3, latch.Count); ft = new StartAwareFutureTask_CancelReturnTrue(anotherCallable, 0); futureTasks.Add(ft); callable.Call(); // 1 decrement for myself and 1 for the task that returned true when cancelled Assert.AreEqual(latchSize - 5, latch.Count); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="shards"></param> /// <param name="operation"></param> /// <param name="exitStrategy"></param> /// <param name="exitOperationsCollector"></param> /// <returns></returns> public T Apply <T>(IList <IShard> shards, IShardOperation <T> operation, IExitStrategy <T> exitStrategy, IExitOperationsCollector exitOperationsCollector) { IList <StartAwareFutureTask <T> > tasks = new List <StartAwareFutureTask <T> >(shards.Count); int taskId = 0; CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(shards.Count); foreach (IShard shard in shards) { ParallelShardOperationCallable <T> callable = new ParallelShardOperationCallable <T>( startSignal, doneSignal, exitStrategy, operation, shard, tasks); StartAwareFutureTask <T> ft = new StartAwareFutureTask <T>(callable, taskId++); tasks.Add(ft); ThreadPool.QueueUserWorkItem(delegate { ft.Run(); }); } // the tasks List is populated, release the threads! startSignal.CountDown(); try { log.Debug("Waiting for threads to complete processing before proceeding."); //TODO(maxr) let users customize timeout behavior /* * if(!doneSignal.await(10, TimeUnit.SECONDS)) { * final String msg = "Parallel operations timed out."; * log.error(msg); * throw new HibernateException(msg); * } */ // now we wait until all threads finish doneSignal.Await(); } catch (Exception e) { // not sure why this would happen or what we should do if it does log.Error("Received unexpected exception while waiting for done signal.", e); } log.Debug("Compiling results."); return(exitStrategy.CompileResults(exitOperationsCollector)); }
public ParallelOperation(IEnumerable <IShard> shards, IShardOperation <T> operation, IExitStrategy <T> exitStrategy) { _operation = operation; _exitStrategy = exitStrategy; lock (this) { foreach (var shard in shards) { ThreadPool.QueueUserWorkItem(ExecuteForShard, shard); _activeCount++; } } }
public T Apply <T>(IList <IShard> shards, IShardOperation <T> operation, IExitStrategy <T> exitStrategy, IExitOperationsCollector exitOperationsCollector) { foreach (IShard shard in GetNextOrderingOfShards(shards)) { if (exitStrategy.AddResult(operation.Execute(shard), shard)) { log.DebugFormat("Short-circuiting operation {0} after execution against shard {1}", operation.OperationName, shard); break; } } return(exitStrategy.CompileResults(exitOperationsCollector)); }
public ParallelShardOperationCallable( CountDownLatch startSignal, CountDownLatch doneSignal, IExitStrategy <T> exitStrategy, IShardOperation <T> operation, IShard shard, IList <StartAwareFutureTask <T> > futureTasks) { this.startSignal = startSignal; this.doneSignal = doneSignal; this.exitStrategy = exitStrategy; this.operation = operation; this.shard = shard; this.futureTasks = futureTasks; }
public virtual T Execute <T>(IShardOperation <T> operation, IExitStrategy <T> exitStrategy) { throw new NotSupportedException(); }
/// <inheritdoc /> public Task <T> ApplyAsync <T>(IEnumerable <IShard> shards, IAsyncShardOperation <T> operation, IExitStrategy <T> exitStrategy, CancellationToken cancellationToken) { return(new ParallelAsyncOperation <T>(shards, operation, exitStrategy, cancellationToken).CompleteAsync()); }
/// <inheritdoc /> public T Apply <T>(IEnumerable <IShard> shards, IShardOperation <T> operation, IExitStrategy <T> exitStrategy) { return(new ParallelOperation <T>(shards, operation, exitStrategy).Complete()); }
public T Apply <T>(IEnumerable <IShard> shards, IShardOperation <T> operation, IExitStrategy <T> exitStrategy) { foreach (IShard shard in GetNextOrderingOfShards(shards)) { var shardOperation = operation.Prepare(shard); var result = shardOperation(); if (result != null && exitStrategy.AddResult(result, shard)) { log.DebugFormat("Short-circuiting operation {0} after execution against shard {1}", operation.OperationName, shard); break; } } return(exitStrategy.CompileResults()); }
public Task <T> ExecuteAsync <T>(IAsyncShardOperation <T> operation, IExitStrategy <T> exitStrategy, CancellationToken cancellationToken) { throw new NotSupportedException(); }
public async Task <T> ApplyAsync <T>(IEnumerable <IShard> shards, IAsyncShardOperation <T> operation, IExitStrategy <T> exitStrategy, CancellationToken cancellationToken) { foreach (var shard in GetNextOrderingOfShards(shards)) { var shardOperation = operation.PrepareAsync(shard); var result = await shardOperation(cancellationToken); if (result != null && exitStrategy.AddResult(result, shard)) { Log.Debug("Short-circuiting operation {0} after execution against shard {1}", operation.OperationName, shard); break; } } return(exitStrategy.CompileResults()); }