/// <summary> /// Constructor. /// </summary> /// <param name="evaluators"></param> public NNEvaluatorPooled(NNEvaluator[] evaluators, int batchSizeThreshold = DEFAULT_BATCH_THRESHOLD, float batchDelayMilliseconds = DEFAULT_DELAY_MS, bool retrieveSupplementalResults = false) : base(evaluators) { const int MAX_BATCH_SIZE_THRESHOLD = NNEvaluator.MAX_BATCH_SIZE / 2; if (batchSizeThreshold > MAX_BATCH_SIZE_THRESHOLD) { // Don't allow the batch size threshold to be too close to // the absolute batch size threshold to avoid overflow in the final pooling. throw new ArgumentOutOfRangeException(nameof(batchSizeThreshold), $"NNEvaluatorPooled batchSizeThreshold is too large, maximum value: {MAX_BATCH_SIZE_THRESHOLD}"); } // Verify the evaluators not the same object but are equivalent. for (int i = 0; i < evaluators.Length; i++) { for (int j = 0; j < evaluators.Length; j++) { if (i != j && object.ReferenceEquals(evaluators[i], evaluators[j])) { throw new ArgumentException("The evaluators passed to NNEvaluatorPooled must not be duplicated"); } } if (!evaluators[0].IsEquivalentTo(evaluators[i])) { throw new Exception("Cannot combine evaluators with different output in same NNEvaluatorsMultiBatched"); } } BatchSizeThreshold = batchSizeThreshold; BatchDelayMS = batchDelayMilliseconds; RetrieveSupplementalResults = retrieveSupplementalResults; pendingPooledBatches = new BlockingCollection <NNEvaluatorPoolBatch>(); cancelSource = new CancellationTokenSource(); cancelToken = cancelSource.Token; // Create and launch task for each evaluator. int index = 0; evaluatorTasks = new List <Task>(); foreach (NNEvaluator evaluator in evaluators) { int thisIndex = index; evaluatorTasks.Add(Task <int> .Run(() => EvaluatorThreadMethod(thisIndex))); index++; } // Create an initial empty batch. currentPooledBatch = new NNEvaluatorPoolBatch(); // Finally, launch the dispatch task which watches when // pooled batches have filled sufficiently to be launched. dispatchTask = Task.Run(DispatchTaskProcessor, cancelToken); }
/// <summary> /// Launches the current batch. /// </summary> void Launch() { pendingPooledBatches.Add(currentPooledBatch); currentPooledBatch = new NNEvaluatorPoolBatch(); lastBatchTime = DateTime.Now; }