Загрузить файлы в «/»

This commit is contained in:
margarita-krasnova 2024-12-30 22:57:49 +04:00
parent 53b31845fe
commit 55e30c1008
4 changed files with 201 additions and 0 deletions

131
Matrix.cs Normal file
View File

@ -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;
}
}
}

35
Program.cs Normal file
View File

@ -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();
}
}

10
rvip_6.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

25
rvip_6.sln Normal file
View File

@ -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