From 629ac45adb14ea383bbd48921d4daf318bd1e8c5 Mon Sep 17 00:00:00 2001 From: margarita-krasnova Date: Mon, 30 Dec 2024 20:43:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D1=8C=D1=82?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Matrix.cs | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 36 +++++++++++++++ rvip_5.csproj | 10 +++++ rvip_5.sln | 25 +++++++++++ 4 files changed, 190 insertions(+) create mode 100644 Matrix.cs create mode 100644 Program.cs create mode 100644 rvip_5.csproj create mode 100644 rvip_5.sln diff --git a/Matrix.cs b/Matrix.cs new file mode 100644 index 0000000..b7b25dc --- /dev/null +++ b/Matrix.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace rvip_5 +{ + public class Matrix + { + private readonly int[,] data; + public int Size { get; } + + public Matrix(int size) + { + Size = size; + data = new int[size, size]; + FillRandom(); + } + + public Matrix(int[,] data) + { + this.data = data; + Size = data.GetLength(0); + } + + public int GetElement(int row, int col) + { + return data[row, col]; + } + + public void SetElement(int row, int col, int 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.Next(10); // Случайные числа от 0 до 9 + } + } + } + + public Matrix MultiplySequential(Matrix other) + { + if (Size != other.Size) + { + throw new ArgumentException("Матрицы должны быть одинакового размера."); + } + + int[,] resultData = new int[Size, Size]; + for (int i = 0; i < Size; i++) + { + for (int j = 0; j < Size; j++) + { + for (int k = 0; k < Size; k++) + { + resultData[i, j] += data[i, k] * other.data[k, j]; + } + } + } + return new Matrix(resultData); + } + + public Matrix MultiplyParallel(Matrix other, int numThreads) + { + if (Size != other.Size) + { + throw new ArgumentException("Матрицы должны быть одинакового размера."); + } + + int[,] resultData = new int[Size, Size]; + Task[] tasks = new Task[numThreads]; + int rowsPerThread = Size / numThreads; + + for (int i = 0; i < numThreads; i++) + { + int startRow = i * rowsPerThread; + int endRow = (i == numThreads - 1) ? Size : (i + 1) * rowsPerThread; + tasks[i] = Task.Run(() => + { + for (int row = startRow; row < endRow; row++) + { + for (int col = 0; col < Size; col++) + { + for (int k = 0; k < Size; k++) + { + resultData[row, col] += data[row, k] * other.data[k, col]; + } + } + } + }); + } + + Task.WaitAll(tasks); + return new Matrix(resultData); + } + + 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..2eb21a0 --- /dev/null +++ b/Program.cs @@ -0,0 +1,36 @@ +using rvip_5; +using System.Diagnostics; +using System; +using System.Collections.Generic; +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 matrixA = new Matrix(size); + Matrix matrixB = new Matrix(size); + + Console.WriteLine("Бенчмаркинг последовательного алгоритма..."); + Stopwatch stopwatchSequential = Stopwatch.StartNew(); + matrixA.MultiplySequential(matrixB); + stopwatchSequential.Stop(); + Console.WriteLine($"Время выполнения последовательного алгоритма: {stopwatchSequential.ElapsedMilliseconds} мс"); + + Console.WriteLine("Бенчмаркинг параллельного алгоритма..."); + foreach (int threads in threadCounts) + { + Stopwatch stopwatchParallel = Stopwatch.StartNew(); + matrixA.MultiplyParallel(matrixB, threads); + stopwatchParallel.Stop(); + Console.WriteLine($"Время выполнения параллельного алгоритма ({threads} потоков): {stopwatchParallel.ElapsedMilliseconds} мс"); + } + Console.WriteLine("-----------------------------"); + } + Console.ReadKey(); + } +} \ No newline at end of file diff --git a/rvip_5.csproj b/rvip_5.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/rvip_5.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/rvip_5.sln b/rvip_5.sln new file mode 100644 index 0000000..85129b8 --- /dev/null +++ b/rvip_5.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_5", "rvip_5.csproj", "{47D7D33F-69C9-4F82-81B8-A73DB26841A7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {47D7D33F-69C9-4F82-81B8-A73DB26841A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {47D7D33F-69C9-4F82-81B8-A73DB26841A7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {47D7D33F-69C9-4F82-81B8-A73DB26841A7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {47D7D33F-69C9-4F82-81B8-A73DB26841A7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1475762B-0C48-49F1-AC87-AC748E6684DE} + EndGlobalSection +EndGlobal