Пример #1
0
        /// <inheritdoc />
        public override async Task <object> ExecuteAsync(IWorkerExecutionStatusNotificationService workerExecutionStatusNotificationService, IWorkUnit workUnit, IEnumerable <Result> dependencyResults)
        {
            await this.GetLogger().LogInformationAsync("PiWorker Execute", Assembly.GetExecutingAssembly().GetName().Version.ToString());

            var seed = workUnit.GetHashCode();

            await this.GetLogger().LogInformationAsync("PiWorker Execute", $"Random seed = {seed}");

            var random = new Random(seed);

            var inputData       = workUnit.GetInput <PiEstimateInput>();
            var numberOfSamples = inputData.NumberOfSamples;

            await this.GetLogger().LogInformationAsync("PiWorker Execute", $"Input requesting NumberOfSamples={numberOfSamples}");

            var numberWithinUnitCircle = 0;

            for (var iteration = 0; iteration < numberOfSamples; iteration++)
            {
                var x      = (random.NextDouble() * 2.0) - 1.0;
                var y      = (random.NextDouble() * 2.0) - 1.0;
                var bucket = Math.Pow(x, 2.0) + Math.Pow(y, 2.0);
                if (bucket <= 1.0)
                {
                    numberWithinUnitCircle++;
                }

                var progress = (double)iteration / numberOfSamples;
                workerExecutionStatusNotificationService?.AddWorkItemStatus(WorkStatus.Executing, progress, $"progress update {(int)progress * 100}%");
            }

            var result = new PiEstimateIntermediateResult
            {
                NumberOfSamples        = numberOfSamples,
                NumberWithinUnitCircle = numberWithinUnitCircle
            };

            await this.GetLogger().LogInformationAsync("PiWorker Execute", $"Intermediate result is NumberWithinUnitCircle={numberWithinUnitCircle}, NumberOfSamples={numberOfSamples}");

            return(result);
        }