Распараллеливание нахождения интеграла методом трапеций - C#
Формулировка задачи:
сделал через PLINQ и через потоки, но через потоки уже час висит и никак, я думал вначале что реально просто так долго, но щас уже начал сомневаться, посмотрите пожалуйста, вроде все правильно, но почему же так долго...
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace lab1
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- Console.Write("start end step: ");
- var values = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
- var start = values[0];
- var end = values[1];
- var step = values[2];
- Func<double, double> function = x => Math.Exp(-x);
- var stopwatch = new Stopwatch();
- stopwatch.Start();
- var result = TheMethodOfTrapezoidsSequential(function, start, end, step);
- stopwatch.Stop();
- Console.WriteLine($"[sequential] result: {result}; time: {stopwatch.ElapsedMilliseconds}");
- stopwatch.Restart();
- var steps = TheMethodOfTrapezoidsSteps(function, start, end, step);
- var stepsParallel = steps.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
- .WithDegreeOfParallelism(Environment.ProcessorCount);
- result = stepsParallel.Sum();
- stopwatch.Stop();
- Console.WriteLine($"[PLINQ parallel] result: {result}; time: {stopwatch.ElapsedMilliseconds}");
- stopwatch.Restart();
- result = TheMethodOfTrapezoidsParallel(function, start, end, step);
- stopwatch.Stop();
- Console.WriteLine($"[multi-threading parallel] result: {result}; time: {stopwatch.ElapsedMilliseconds}");
- }
- catch (Exception e)
- {
- Console.Write(e.Message);
- }
- Console.ReadKey();
- }
- public static double TheMethodOfTrapezoids(Func<double, double> function, double x, double step)
- {
- return (function(x) + function(x + step)) * step / 2;
- }
- public static double TheMethodOfTrapezoidsSequential(Func<double, double> function, double start, double end, double step)
- {
- var result = 0.0;
- for (double x = start; x <= end; x += step)
- result += TheMethodOfTrapezoids(function, x, step);
- return result;
- }
- public static IEnumerable<double> TheMethodOfTrapezoidsSteps(Func<double, double> function, double start, double end, double step)
- {
- for (double x = start; x <= end; x += step)
- yield return TheMethodOfTrapezoids(function, x, step);
- }
- public static double TheMethodOfTrapezoidsParallel(Func<double, double> function, double start, double end, double step)
- {
- var count = 0;
- var steps = new List<double>();
- ParameterizedThreadStart threadHandler = obj =>
- {
- var x = (double)obj;
- var r = TheMethodOfTrapezoids(function, x, step);
- steps.Add(r);
- };
- for (double x = start; x <= end; x += step, count++)
- new Thread(threadHandler).Start(x);
- while (steps.Count != count) ;
- return steps.Sum();
- }
- }
- }
Решение задачи: «Распараллеливание нахождения интеграла методом трапеций»
textual
Листинг программы
- public static double TheMethodOfTrapezoidsParallel(Func<double, double> function, double start, double end, double step)
- {
- var count = 0;
- var steps = new List<double>();
- List<Task> tasks = new List<Task>();
- for (double x = start; x <= end; x += step, count++)
- {
- tasks.Add(Task.Run(() =>
- {
- var r = TheMethodOfTrapezoids(function, x, step);
- lock (steps)
- {
- steps.Add(r);
- }
- }));
- }
- Task.WaitAll(tasks.ToArray());
- return steps.Sum();
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д