using System.Diagnostics;
using System.Threading.Tasks;
namespace ParallelProgramming.ConsoleApp
public static string IntegrationMethod { get; set; }
public static double IntervalBegin { get; set; }
public static double IntervalEnd { get; set; }
public static int NPrecisionValue { get; set; }
public static bool IsParallel { get; set; }
public static int ThreadValue { get; set; }
public static Stopwatch Stopwatch { get; set; }
public static double Result { get; set; }
static void Main(string[] args)
Console.WriteLine("Function | Elapsed Time | Estimated Integral");
Console.WriteLine("-----------------------------------------------------------------");
NPrecisionValue = 100000000;
NumericalIntegrationMethods integral = new();
Stopwatch = Stopwatch.StartNew();
Result = integral.RectangularIntegration(IntervalBegin, IntervalEnd, NPrecisionValue);
Console.WriteLine($"{nameof(integral.RectangularIntegration)} – Sequential | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.RectangularIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 1);
Console.WriteLine($"{nameof(integral.RectangularIntegrationParallel)} – 1 Thread | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.RectangularIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 2);
Console.WriteLine($"{nameof(integral.RectangularIntegrationParallel)} – 2 Threads | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.RectangularIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 3);
Console.WriteLine($"{nameof(integral.RectangularIntegrationParallel)} – 3 Threads | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.RectangularIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 4);
Console.WriteLine($"{nameof(integral.RectangularIntegrationParallel)} – 4 Threads | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.TrapezoidalIntegration(IntervalBegin, IntervalEnd, NPrecisionValue);
Console.WriteLine($"{nameof(integral.TrapezoidalIntegration)} – Sequential | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.TrapezoidalIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 1);
Console.WriteLine($"{nameof(integral.TrapezoidalIntegrationParallel)} – 1 Thread | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.TrapezoidalIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 2);
Console.WriteLine($"{nameof(integral.TrapezoidalIntegrationParallel)} – 2 Threads | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.TrapezoidalIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 3);
Console.WriteLine($"{nameof(integral.TrapezoidalIntegrationParallel)} – 3 Threads | {Stopwatch.Elapsed} | {Result}");
Stopwatch = Stopwatch.StartNew();
Result = integral.TrapezoidalIntegrationParallel(IntervalBegin, IntervalEnd, NPrecisionValue, 4);
Console.WriteLine($"{nameof(integral.TrapezoidalIntegrationParallel)} – 4 Threads | {Stopwatch.Elapsed} | {Result}");
Console.WriteLine("Press any key to continue...");
public class NumericalIntegrationMethods
double Function(double x)
public double RectangularIntegration(double xp, double xk, int n)
for (int i = 1; i <= n; i++)
integral += dx * Function(xp + i * dx);
public double TrapezoidalIntegration(double xp, double xk, int n)
for (int i = 1; i <= n; i++)
integral += Function(xp + i * dx);
integral += (Function(xp) + Function(xk)) / 2;
public double RectangularIntegrationParallel(double xp, double xk, int n, int maxThreads)
Parallel.For(1, n + 1, new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, i =>
integral += dx * Function(xp + i * dx);
public double TrapezoidalIntegrationParallel(double xp, double xk, int n, int maxThreads)
Parallel.For(1, n + 1, new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, i =>
integral += Function(xp + i * dx);
integral += (Function(xp) + Function(xk)) / 2;