From 55e30c1008d85813f3b2aa6a374cc015630a13ed Mon Sep 17 00:00:00 2001 From: margarita-krasnova Date: Mon, 30 Dec 2024 22:57:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?/=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Matrix.cs | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 35 ++++++++++++++ rvip_6.csproj | 10 ++++ rvip_6.sln | 25 ++++++++++ 4 files changed, 201 insertions(+) create mode 100644 Matrix.cs create mode 100644 Program.cs create mode 100644 rvip_6.csproj create mode 100644 rvip_6.sln diff --git a/Matrix.cs b/Matrix.cs new file mode 100644 index 0000000..8a77cc1 --- /dev/null +++ b/Matrix.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace rvip_6 +{ + public class Matrix + { + private readonly double[,] data; + public int Size { get; } + + public Matrix(int size) + { + Size = size; + data = new double[size, size]; + FillRandom(); + } + + public Matrix(double[,] data) + { + this.data = data; + Size = data.GetLength(0); + } + + public double GetElement(int row, int col) + { + return data[row, col]; + } + + public void SetElement(int row, int col, double value) + { + data[row, col] = value; + } + + private void FillRandom() + { + Random random = new Random(); + for (int i = 0; i < Size; i++) + { + for (int j = 0; j < Size; j++) + { + data[i, j] = random.NextDouble() * 10; // Заполняем случайными числами (0 - 10) + } + } + } + + public double DeterminantSequential() + { + if (Size == 1) + return data[0, 0]; + + if (Size == 2) + return data[0, 0] * data[1, 1] - data[0, 1] * data[1, 0]; + + double det = 0; + for (int j = 0; j < Size; j++) + { + det += (j % 2 == 0 ? 1 : -1) * data[0, j] * GetSubMatrix(0, j).DeterminantSequential(); + } + return det; + } + + private Matrix GetSubMatrix(int rowToRemove, int colToRemove) + { + double[,] subMatrix = new double[Size - 1, Size - 1]; + int subRow = 0; + for (int row = 0; row < Size; row++) + { + if (row == rowToRemove) + continue; + + int subCol = 0; + for (int col = 0; col < Size; col++) + { + if (col == colToRemove) + continue; + subMatrix[subRow, subCol] = data[row, col]; + subCol++; + } + subRow++; + } + return new Matrix(subMatrix); + } + + public double DeterminantParallel(int numThreads) + { + if (Size == 1) + return data[0, 0]; + + if (Size == 2) + return data[0, 0] * data[1, 1] - data[0, 1] * data[1, 0]; + + double det = 0; + var results = new double[Size]; + var tasks = new Task[Size]; + + for (int j = 0; j < Size; j++) + { + int currentJ = j; + tasks[j] = Task.Run(() => + { + results[currentJ] = (currentJ % 2 == 0 ? 1 : -1) * data[0, currentJ] * GetSubMatrix(0, currentJ).DeterminantParallel(numThreads); + }); + } + + Task.WaitAll(tasks); + for (int i = 0; i < results.Length; i++) + { + det += results[i]; + } + return det; + } + + public override string ToString() + { + string result = ""; + for (int i = 0; i < Size; i++) + { + for (int j = 0; j < Size; j++) + { + result += data[i, j] + " "; + } + result += "\n"; + } + return result; + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1fb36f4 --- /dev/null +++ b/Program.cs @@ -0,0 +1,35 @@ +using System; +using System.Diagnostics; +using rvip_6; + +public class Benchmark +{ + public static void Main(string[] args) + { + int[] matrixSizes = { 100, 300, 500 }; + int[] threadCounts = { 1, 2, 4, 8, 16 }; + + foreach (int size in matrixSizes) + { + Console.WriteLine($"Размер матрицы: {size}x{size}"); + Matrix matrix = new Matrix(size); + + Console.WriteLine("Бенчмаркинг последовательного алгоритма..."); + Stopwatch stopwatchSequential = Stopwatch.StartNew(); + matrix.DeterminantSequential(); + stopwatchSequential.Stop(); + Console.WriteLine($"Время выполнения последовательного алгоритма: {stopwatchSequential.ElapsedMilliseconds} мс"); + + Console.WriteLine("Бенчмаркинг параллельного алгоритма..."); + foreach (int threads in threadCounts) + { + Stopwatch stopwatchParallel = Stopwatch.StartNew(); + matrix.DeterminantParallel(threads); + stopwatchParallel.Stop(); + Console.WriteLine($"Время выполнения параллельного алгоритма ({threads} потоков): {stopwatchParallel.ElapsedMilliseconds} мс"); + } + Console.WriteLine("-----------------------------"); + } + Console.ReadKey(); + } +} \ No newline at end of file diff --git a/rvip_6.csproj b/rvip_6.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/rvip_6.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/rvip_6.sln b/rvip_6.sln new file mode 100644 index 0000000..1de739e --- /dev/null +++ b/rvip_6.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34202.233 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "rvip_6", "rvip_6.csproj", "{73511D00-AC29-4CA0-B622-6E54B83E261E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {73511D00-AC29-4CA0-B622-6E54B83E261E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73511D00-AC29-4CA0-B622-6E54B83E261E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73511D00-AC29-4CA0-B622-6E54B83E261E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73511D00-AC29-4CA0-B622-6E54B83E261E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {29E1D09E-0F4D-4FAB-B5FC-E96E0D7B37EC} + EndGlobalSection +EndGlobal