Добавьте файлы проекта.
This commit is contained in:
parent
10a65638be
commit
629ac45adb
119
Matrix.cs
Normal file
119
Matrix.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
36
Program.cs
Normal file
36
Program.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
10
rvip_5.csproj
Normal file
10
rvip_5.csproj
Normal 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_5.sln
Normal file
25
rvip_5.sln
Normal 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_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
|
Loading…
Reference in New Issue
Block a user