Ejemplo n.º 1
0
        public async Task <List <Aggregate> > GenerateAggregates_Parallel_Async(int maxCPU, IProgress <ProgressReportModel> progress, CancellationToken ct)
        {
            var opt = new ParallelOptions
            {
                MaxDegreeOfParallelism = maxCPU,
                CancellationToken      = ct
            };
            var aggregateSizes = GenerateAggregateSizes();
            var aggregates     = new List <Aggregate>();
            var report         = new ProgressReportModel();
            var stopwatch      = new Stopwatch();

            stopwatch.Start();
            try
            {
                await Task.Run(() =>
                               Parallel.ForEach <int>(aggregateSizes, opt, size =>
                {
                    aggregates.Add(_particleFactory.Build(size));
                    UpdateProgress(progress, aggregateSizes, aggregates, report, stopwatch);

                    // If the job is canceled, an error is thrown which is catched below
                    opt.CancellationToken.ThrowIfCancellationRequested();
                }));
            }
            catch (OperationCanceledException e)
            {
                _logger.Warn($"Operation canceled: {e.Message}");
            }

            AggregateIndexingHelper.SetAggregateIndices(aggregates);
            return(aggregates);
        }
Ejemplo n.º 2
0
        public async Task <List <Aggregate> > GenerateAggregates_Async()
        {
            var aggregateSizes    = GenerateAggregateSizes();
            var aggregateGenTasks = new List <Task <Aggregate> >();

            foreach (var size in aggregateSizes)
            {
                aggregateGenTasks.Add(Task.Run(() => _particleFactory.Build(size)));
            }

            var aggregates = await Task.WhenAll(aggregateGenTasks);

            AggregateIndexingHelper.SetAggregateIndices(aggregates);
            return(aggregates.ToList());
        }
Ejemplo n.º 3
0
        public List <Aggregate> GenerateAggregates()
        {
            var totalTime      = new Stopwatch();
            var aggregateSizes = GenerateAggregateSizes();
            var aggregates     = new List <Aggregate>();

            foreach (var size in aggregateSizes)
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                aggregates.Add(_particleFactory.Build(size));
                Debug.WriteLine($"{DateTime.Now}: Built aggregate {aggregates.Count}/{aggregateSizes.Count} with {aggregates.Last().NumberOfPrimaryParticles} PP in {stopwatch.Elapsed}");
            }
            Debug.WriteLine($"Total ComputationTime {totalTime.Elapsed}");
            totalTime.Stop();

            AggregateIndexingHelper.SetAggregateIndices(aggregates);
            return(aggregates);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This runs the generation in parallel but not asyncronosly.
        /// It first generates sizes for aggregates and subsequently generates them
        /// </summary>
        /// <param name="maxCPU"></param>
        /// <returns></returns>
        public List <Aggregate> GenerateAggregates_Parallel(int maxCPU)
        {
            var opt = new ParallelOptions
            {
                MaxDegreeOfParallelism = maxCPU
            };
            var aggregateSizes = GenerateAggregateSizes();
            var aggregates     = new List <Aggregate>();

            Parallel.ForEach <int>(aggregateSizes, opt, size =>
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                aggregates.Add(_particleFactory.Build(size));
                Debug.WriteLine($"{DateTime.Now}: Built aggregate {aggregates.Count}/{aggregateSizes.Count} with {aggregates.Last().NumberOfPrimaryParticles} PP in {stopwatch.Elapsed}");
            });
            AggregateIndexingHelper.SetAggregateIndices(aggregates);
            return(aggregates);
        }