forked from v.moiseev/distributed-computing
Compare commits
3 Commits
ostrovskay
...
ostrovskay
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f75432d5f7 | ||
|
|
6dc434cbf3 | ||
|
|
267ba3b73c |
@@ -1,73 +0,0 @@
|
||||
# Отчет по лабораторной работе №5
|
||||
|
||||
Выполнила студентка гр. ИСЭбд-41 Островская С.Ф.
|
||||
|
||||
## Создание приложения
|
||||
|
||||
Было выбрано консольное приложение, язык программирования - c#.
|
||||
|
||||
Обычный алгоритм:
|
||||
|
||||
```cs
|
||||
static int[][] MultiplyMatrix(int[][] matrix1, int[][] matrix2)
|
||||
{
|
||||
int rows = matrix1.Length;
|
||||
int columns = matrix2[0].Length;
|
||||
int[][] result = new int[rows][];
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
result[i] = new int[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
result[i][j] = 0;
|
||||
for (int k = 0; k < matrix1[i].Length; k++)
|
||||
{
|
||||
result[i][j] += matrix1[i][k] * matrix2[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
Параллельный алгоритм:
|
||||
|
||||
```cs
|
||||
static int[][] MultiplyMatrixParallel(int[][] matrix1, int[][] matrix2, int numThreads)
|
||||
{
|
||||
int rows = matrix1.Length;
|
||||
int columns = matrix2[0].Length;
|
||||
int[][] result = new int[rows][];
|
||||
|
||||
|
||||
Parallel.For(0, rows, new ParallelOptions { MaxDegreeOfParallelism = numThreads }, i =>
|
||||
{
|
||||
result[i] = new int[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
result[i][j] = 0;
|
||||
Parallel.For(0, matrix1[i].Length, k =>
|
||||
{
|
||||
result[i][j] += matrix1[i][k] * matrix2[k][j];
|
||||
});
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
В результате обычный алгоритм выполнился за ``0,0002112`` секунды, а паралелльный за ``0,0064956`` секунды.
|
||||
|
||||
## Бенчмарки
|
||||
|
||||
Протестируем обычный и параллельный алгоритмы на матрицах различных размеров: 100х100, 300х300 и 500х500.
|
||||
|
||||
Количество потоков: ``4``
|
||||

|
||||
|
||||
Количество потоков: ``12``
|
||||

|
||||
|
||||
``Вывод``: Обыный алгоритм работает быстрее, если количество элементов не слишком много. Параллельный же алгоритм работает быстрее только при наличии большого количества операций и данных. Оптимальное количество потоков для эффективной работы - 12.
|
||||
@@ -1,192 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine("Задания: \n1. проверка умножения матриц;\n2. бэнчмарки;");
|
||||
Console.Write("Введите номер задания: ");
|
||||
int numTask = int.Parse(Console.ReadLine());
|
||||
if (numTask == 1)
|
||||
{
|
||||
int[][] matrix1 = GenerateMatrix(10);
|
||||
int[][] matrix2 = GenerateMatrix(10);
|
||||
|
||||
Console.WriteLine();
|
||||
PrintMatrix(matrix1); // Вывод результата
|
||||
Console.WriteLine();
|
||||
PrintMatrix(matrix2); // Вывод результата
|
||||
|
||||
// Вводим количество потоков вручную
|
||||
Console.WriteLine();
|
||||
Console.Write("Введите количество потоков: ");
|
||||
int numThreads = int.Parse(Console.ReadLine());
|
||||
Console.WriteLine();
|
||||
|
||||
Stopwatch sw1 = new Stopwatch();
|
||||
sw1.Start();
|
||||
int[][] resultSequential = MultiplyMatrix(matrix1, matrix2); // Вызов обычного алгоритма
|
||||
sw1.Stop();
|
||||
|
||||
Stopwatch sw2 = new Stopwatch();
|
||||
sw2.Start();
|
||||
int[][] resultParallel = MultiplyMatrixParallel(matrix1, matrix2, numThreads); // Вызов параллельного алгоритма
|
||||
sw2.Stop();
|
||||
|
||||
Console.WriteLine("Выполнение обычного алгоритма");
|
||||
PrintMatrix(resultSequential); // Вывод результата
|
||||
TimeSpan elapsedTime1 = sw1.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime1.TotalSeconds:F7} с");
|
||||
|
||||
Console.WriteLine("\nВыполнение параллельного алгоритма");
|
||||
PrintMatrix(resultParallel); // Вывод результата
|
||||
TimeSpan elapsedTime2 = sw2.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime2.TotalSeconds:F7} с");
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
else if (numTask == 2)
|
||||
{
|
||||
int[][] matrix1 = GenerateMatrix(100);
|
||||
int[][] matrix2 = GenerateMatrix(100);
|
||||
|
||||
Console.Write("Введите количество потоков: ");
|
||||
int numThreads = int.Parse(Console.ReadLine());
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("Размер матрицы: 100x100");
|
||||
|
||||
Console.WriteLine("\nПоследовательный алгоритм:");
|
||||
Stopwatch sw1 = new Stopwatch();
|
||||
sw1.Start();
|
||||
int[][] resultSequential = MultiplyMatrix(matrix1, matrix2);
|
||||
sw1.Stop();
|
||||
TimeSpan elapsedTime1 = sw1.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime1.TotalSeconds:F7} мс");
|
||||
|
||||
Console.WriteLine("\nПараллельный алгоритм:");
|
||||
Stopwatch sw2 = new Stopwatch();
|
||||
sw2.Start();
|
||||
int[][] resultParallel = MultiplyMatrixParallel(matrix1, matrix2, numThreads);
|
||||
sw2.Stop();
|
||||
TimeSpan elapsedTime2 = sw2.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime2.TotalSeconds:F7} с");
|
||||
|
||||
Console.WriteLine("\n------------------------------------------\n");
|
||||
|
||||
matrix1 = GenerateMatrix(300);
|
||||
matrix2 = GenerateMatrix(300);
|
||||
|
||||
Console.WriteLine("Размер матрицы: 300x300");
|
||||
|
||||
Console.WriteLine("\nПоследовательный алгоритм:");
|
||||
sw1.Restart();
|
||||
resultSequential = MultiplyMatrix(matrix1, matrix2);
|
||||
sw1.Stop();
|
||||
elapsedTime1 = sw1.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime1.TotalSeconds:F7} мс");
|
||||
|
||||
Console.WriteLine("\nПараллельный алгоритм:");
|
||||
sw2.Restart();
|
||||
resultParallel = MultiplyMatrixParallel(matrix1, matrix2, numThreads);
|
||||
sw2.Stop();
|
||||
elapsedTime2 = sw2.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime2.TotalSeconds:F7} с");
|
||||
|
||||
Console.WriteLine("\n------------------------------------------\n");
|
||||
|
||||
matrix1 = GenerateMatrix(500);
|
||||
matrix2 = GenerateMatrix(500);
|
||||
|
||||
Console.WriteLine("Размер матрицы: 500x500");
|
||||
|
||||
Console.WriteLine("\nПоследовательный алгоритм:");
|
||||
sw1.Restart();
|
||||
resultSequential = MultiplyMatrix(matrix1, matrix2);
|
||||
sw1.Stop();
|
||||
elapsedTime1 = sw1.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime1.TotalSeconds:F7} мс");
|
||||
|
||||
Console.WriteLine("\nПараллельный алгоритм:");
|
||||
sw2.Restart();
|
||||
resultParallel = MultiplyMatrixParallel(matrix1, matrix2, numThreads);
|
||||
sw2.Stop();
|
||||
elapsedTime2 = sw2.Elapsed;
|
||||
Console.WriteLine($"Время выполнения: {elapsedTime2.TotalSeconds:F7} с");
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int[][] GenerateMatrix(int size)
|
||||
{
|
||||
Random random = new Random();
|
||||
int[][] matrix = new int[size][];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
matrix[i] = new int[size];
|
||||
for (int j = 0; j < size; j++)
|
||||
{
|
||||
matrix[i][j] = random.Next(1, 100);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
static int[][] MultiplyMatrix(int[][] matrix1, int[][] matrix2)
|
||||
{
|
||||
int rows = matrix1.Length;
|
||||
int columns = matrix2[0].Length;
|
||||
int[][] result = new int[rows][];
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
result[i] = new int[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
result[i][j] = 0;
|
||||
for (int k = 0; k < matrix1[i].Length; k++)
|
||||
{
|
||||
result[i][j] += matrix1[i][k] * matrix2[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static int[][] MultiplyMatrixParallel(int[][] matrix1, int[][] matrix2, int numThreads)
|
||||
{
|
||||
int rows = matrix1.Length;
|
||||
int columns = matrix2[0].Length;
|
||||
int[][] result = new int[rows][];
|
||||
|
||||
|
||||
Parallel.For(0, rows, new ParallelOptions { MaxDegreeOfParallelism = numThreads }, i =>
|
||||
{
|
||||
result[i] = new int[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
result[i][j] = 0;
|
||||
Parallel.For(0, matrix1[i].Length, k =>
|
||||
{
|
||||
result[i][j] += matrix1[i][k] * matrix2[k][j];
|
||||
});
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
static void PrintMatrix(int[][] matrix)
|
||||
{
|
||||
for (int i = 0; i < matrix.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix[i].Length; j++)
|
||||
{
|
||||
Console.Write(matrix[i][j] + " ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj",
|
||||
"projectName": "lab_5_matrix",
|
||||
"projectPath": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj",
|
||||
"packagesPath": "C:\\Users\\oostr\\.nuget\\packages\\",
|
||||
"outputPath": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"fallbackFolders": [
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
|
||||
],
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\oostr\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.100/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\oostr\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\oostr\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
||||
@@ -1,73 +0,0 @@
|
||||
{
|
||||
"version": 3,
|
||||
"targets": {
|
||||
"net8.0": {}
|
||||
},
|
||||
"libraries": {},
|
||||
"projectFileDependencyGroups": {
|
||||
"net8.0": []
|
||||
},
|
||||
"packageFolders": {
|
||||
"C:\\Users\\oostr\\.nuget\\packages\\": {},
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
|
||||
},
|
||||
"project": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj",
|
||||
"projectName": "lab_5_matrix",
|
||||
"projectPath": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\lab_5_matrix.csproj",
|
||||
"packagesPath": "C:\\Users\\oostr\\.nuget\\packages\\",
|
||||
"outputPath": "D:\\Study\\4kurs\\РВИП\\distributed-computing\\tasks\\ostrovskaya-sf\\lab_5\\lab_5_matrix\\lab_5_matrix\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"fallbackFolders": [
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
|
||||
],
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\oostr\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.100/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 93 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 148 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 75 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 75 KiB |
66
tasks/ostrovskaya-sf/lab_6/README.md
Normal file
66
tasks/ostrovskaya-sf/lab_6/README.md
Normal file
@@ -0,0 +1,66 @@
|
||||
# Отчет по лабораторной работе №6
|
||||
|
||||
Выполнила студентка гр. ИСЭбд-41 Островская С. Ф.
|
||||
|
||||
## Создание приложения
|
||||
|
||||
Было выбрано консольное приложение, язык программирования - c#.
|
||||
|
||||
Обычный алгоритм:
|
||||
|
||||
```cs
|
||||
static void SequentialDeterminantCalculation(int matrixSize, int lowerLimit, int upperLimit)
|
||||
{
|
||||
int[][] randomMatrix = GenerateRandomMatrix(matrixSize, lowerLimit, upperLimit);
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int result = Determinant(randomMatrix);
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
Console.WriteLine($"Последовательный детерминант: {result}");
|
||||
Console.WriteLine($"Последовательное время: {elapsedTime.TotalSeconds:F7} секунд");
|
||||
}
|
||||
```
|
||||
Параллельный алгоритм:
|
||||
|
||||
```cs
|
||||
static void ParallelDeterminantCalculation(int matrixSize, int lowerLimit, int upperLimit, int numProcesses)
|
||||
{
|
||||
int[][] randomMatrix = GenerateRandomMatrix(matrixSize, lowerLimit, upperLimit);
|
||||
|
||||
int[][] matricesToProcess = new int[matrixSize][];
|
||||
for (int col = 0; col < matrixSize; col++)
|
||||
{
|
||||
matricesToProcess[col] = Submatrix(randomMatrix, 0, col)[0];
|
||||
}
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int[] determinants = new int[matrixSize];
|
||||
Parallel.For(0, matrixSize, new ParallelOptions { MaxDegreeOfParallelism = numProcesses }, col =>
|
||||
{
|
||||
determinants[col] = Determinant(new int[][] { matricesToProcess[col] });
|
||||
});
|
||||
|
||||
int result = 0;
|
||||
for (int col = 0; col < matrixSize; col++)
|
||||
{
|
||||
result += ((-1) * col) * randomMatrix[0][col] * determinants[col];
|
||||
}
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
Console.WriteLine($"Параллельный детерминант: {result}");
|
||||
Console.WriteLine($"Параллельное время: {elapsedTime.TotalSeconds:F7} секунд");
|
||||
}
|
||||
```
|
||||
|
||||
## Бенчмарки
|
||||
|
||||
Для примера была взята матрица размерностью 10х10, поскольку для матриц больших размеров детерминант вычисляется слишком долго.
|
||||
|
||||
|
||||

|
||||
|
||||
|
||||
``Вывод``: Обыный алгоритм работает быстрее, если количество элементов не слишком много. Параллельный же алгоритм работает быстрее только при наличии большого количества операций и данных.
|
||||
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34330.188
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lab_5_matrix", "lab_5_matrix\lab_5_matrix.csproj", "{A71D963B-1625-458C-A90F-5DB95C03B17C}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lab_6_matrix", "lab_6_matrix\lab_6_matrix.csproj", "{50F84649-668E-464D-94A9-7C36E1FCB2FF}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -11,15 +11,15 @@ Global
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A71D963B-1625-458C-A90F-5DB95C03B17C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A71D963B-1625-458C-A90F-5DB95C03B17C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A71D963B-1625-458C-A90F-5DB95C03B17C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A71D963B-1625-458C-A90F-5DB95C03B17C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{50F84649-668E-464D-94A9-7C36E1FCB2FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{50F84649-668E-464D-94A9-7C36E1FCB2FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{50F84649-668E-464D-94A9-7C36E1FCB2FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{50F84649-668E-464D-94A9-7C36E1FCB2FF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {1A186ABB-2B32-4398-9A58-04ED799B0B80}
|
||||
SolutionGuid = {45DFA84B-6962-4667-98B3-13BD962CF727}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
135
tasks/ostrovskaya-sf/lab_6/lab_6_matrix/lab_6_matrix/Program.cs
Normal file
135
tasks/ostrovskaya-sf/lab_6/lab_6_matrix/lab_6_matrix/Program.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void SequentialDeterminantCalculation(int matrixSize, int lowerLimit, int upperLimit)
|
||||
{
|
||||
int[][] randomMatrix = GenerateRandomMatrix(matrixSize, lowerLimit, upperLimit);
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int result = Determinant(randomMatrix);
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
Console.WriteLine($"Последовательный детерминант: {result}");
|
||||
Console.WriteLine($"Последовательное время: {elapsedTime.TotalSeconds:F7} секунд");
|
||||
}
|
||||
|
||||
static void ParallelDeterminantCalculation(int matrixSize, int lowerLimit, int upperLimit, int numProcesses)
|
||||
{
|
||||
int[][] randomMatrix = GenerateRandomMatrix(matrixSize, lowerLimit, upperLimit);
|
||||
|
||||
int[][] matricesToProcess = new int[matrixSize][];
|
||||
for (int col = 0; col < matrixSize; col++)
|
||||
{
|
||||
matricesToProcess[col] = Submatrix(randomMatrix, 0, col)[0];
|
||||
}
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int[] determinants = new int[matrixSize];
|
||||
Parallel.For(0, matrixSize, new ParallelOptions { MaxDegreeOfParallelism = numProcesses }, col =>
|
||||
{
|
||||
determinants[col] = Determinant(new int[][] { matricesToProcess[col] });
|
||||
});
|
||||
|
||||
int result = 0;
|
||||
for (int col = 0; col < matrixSize; col++)
|
||||
{
|
||||
result += ((-1) * col) * randomMatrix[0][col] * determinants[col];
|
||||
}
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
Console.WriteLine($"Параллельный детерминант: {result}");
|
||||
Console.WriteLine($"Параллельное время: {elapsedTime.TotalSeconds:F7} секунд");
|
||||
}
|
||||
|
||||
static int[][] GenerateRandomMatrix(int matrixSize, int lowerLimit, int upperLimit)
|
||||
{
|
||||
Random random = new Random();
|
||||
int[][] matrix = new int[matrixSize][];
|
||||
|
||||
for (int i = 0; i < matrixSize; i++)
|
||||
{
|
||||
matrix[i] = new int[matrixSize];
|
||||
for (int j = 0; j < matrixSize; j++)
|
||||
{
|
||||
matrix[i][j] = random.Next(lowerLimit, upperLimit);
|
||||
}
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
static int[][] Submatrix(int[][] matrix, int rowToDelete, int colToDelete)
|
||||
{
|
||||
int size = matrix.Length - 1;
|
||||
int[][] submatrix = new int[size][];
|
||||
|
||||
int rowIndex = 0;
|
||||
|
||||
for (int i = 0; i < matrix.Length; i++)
|
||||
{
|
||||
if (i == rowToDelete)
|
||||
continue;
|
||||
|
||||
submatrix[rowIndex] = new int[size];
|
||||
|
||||
int colIndex = 0;
|
||||
for (int j = 0; j < matrix.Length; j++)
|
||||
{
|
||||
if (j == colToDelete)
|
||||
continue;
|
||||
|
||||
submatrix[rowIndex][colIndex] = matrix[i][j];
|
||||
colIndex++;
|
||||
}
|
||||
|
||||
rowIndex++;
|
||||
}
|
||||
|
||||
return submatrix;
|
||||
}
|
||||
|
||||
static int Determinant(int[][] matrix)
|
||||
{
|
||||
int n = matrix.Length;
|
||||
if (n == 1)
|
||||
{
|
||||
return matrix[0][0];
|
||||
}
|
||||
else if (n == 2)
|
||||
{
|
||||
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
|
||||
}
|
||||
else
|
||||
{
|
||||
int determinant = 0;
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
int[][] submatrix = Submatrix(matrix, 0, j);
|
||||
int subDeterminant = Determinant(submatrix);
|
||||
determinant += ((-1) * j) * matrix[0][j] * subDeterminant;
|
||||
}
|
||||
|
||||
return determinant;
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
SequentialDeterminantCalculation(10, 0, 10);
|
||||
Console.WriteLine();
|
||||
|
||||
int[] numThreads = { 1, 2, 4, 6, 8, 12, 16, 32 };
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
Console.WriteLine($"Количество потоков: {numThreads[i]}");
|
||||
ParallelDeterminantCalculation(10, 0, 10, numThreads[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
tasks/ostrovskaya-sf/lab_6/pic/pic1.jpg
Normal file
BIN
tasks/ostrovskaya-sf/lab_6/pic/pic1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
Reference in New Issue
Block a user