168 lines
4.5 KiB
C#
168 lines
4.5 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.Threading.Tasks;
|
||
class Program
|
||
{
|
||
static void Main(string[] args)
|
||
{
|
||
|
||
Console.WriteLine("2 - Бенчмарки");
|
||
|
||
string userChoice = Console.ReadLine();
|
||
if (userChoice == "1")
|
||
{
|
||
// Task 1: Calculate determinant
|
||
}
|
||
else if (userChoice == "2")
|
||
{
|
||
int numberOfThreads = GetNumberOfThreads(); // Get the number of threads from the user
|
||
|
||
Console.WriteLine("\n Вычисление матрицы с размером 10*10:");
|
||
RunDeterminantBenchmark(10, numberOfThreads);
|
||
|
||
Console.ReadLine();
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("Invalid choice.");
|
||
Console.WriteLine("\nPress Enter to return to the main menu...");
|
||
Console.ReadLine();
|
||
}
|
||
Console.ReadLine();
|
||
}
|
||
|
||
static void RunDeterminantBenchmark(int matrixSize, int numberOfThreads)
|
||
{
|
||
int[,] randomMatrix = GenerateRandomMatrix(matrixSize);
|
||
|
||
Console.WriteLine("Матрица:");
|
||
PrintMatrix(randomMatrix);
|
||
|
||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||
stopwatch.Restart();
|
||
double sequentialDeterminant = CalculateDeterminantSequential(randomMatrix);
|
||
stopwatch.Stop();
|
||
Console.WriteLine($"Обычный: детерминант = {sequentialDeterminant}, время = {stopwatch.Elapsed.TotalSeconds} с");
|
||
|
||
stopwatch.Restart();
|
||
double parallelDeterminant = CalculateDeterminantParallel(randomMatrix, numberOfThreads);
|
||
stopwatch.Stop();
|
||
Console.WriteLine($"Параллельный: детерминант = {parallelDeterminant}, время = {stopwatch.Elapsed.TotalSeconds} с");
|
||
}
|
||
|
||
static int[,] GenerateRandomMatrix(int size)
|
||
{
|
||
Random random = new Random();
|
||
int[,] matrix = new int[size, size];
|
||
|
||
for (int i = 0; i < size; i++)
|
||
{
|
||
for (int j = 0; j < size; j++)
|
||
{
|
||
matrix[i, j] = random.Next(10);
|
||
}
|
||
}
|
||
|
||
return matrix;
|
||
}
|
||
|
||
static double CalculateDeterminantSequential(int[,] matrix)
|
||
{
|
||
int size = matrix.GetLength(0);
|
||
double determinant = 0;
|
||
|
||
if (size == 1)
|
||
{
|
||
determinant = matrix[0, 0];
|
||
}
|
||
else if (size == 2)
|
||
{
|
||
determinant = matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
|
||
}
|
||
else
|
||
{
|
||
for (int j = 0; j < size; j++)
|
||
{
|
||
determinant += matrix[0, j] * CalculateMinor(matrix, 0, j) * Math.Pow(-1, j);
|
||
}
|
||
}
|
||
|
||
return determinant;
|
||
}
|
||
|
||
static double CalculateDeterminantParallel(int[,] matrix, int threads)
|
||
{
|
||
int size = matrix.GetLength(0);
|
||
double determinant = 0;
|
||
|
||
if (size == 1)
|
||
{
|
||
determinant = matrix[0, 0];
|
||
}
|
||
else if (size == 2)
|
||
{
|
||
determinant = matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
|
||
}
|
||
else
|
||
{
|
||
Parallel.For(0, size, new ParallelOptions { MaxDegreeOfParallelism = threads }, j =>
|
||
{
|
||
determinant += matrix[0, j] * CalculateMinor(matrix, 0, j) * Math.Pow(-1, j);
|
||
});
|
||
}
|
||
|
||
return determinant;
|
||
}
|
||
|
||
static double CalculateMinor(int[,] matrix, int row, int col)
|
||
{
|
||
int size = matrix.GetLength(0);
|
||
int[,] minor = new int[size - 1, size - 1];
|
||
|
||
int minorRow = 0;
|
||
for (int i = 0; i < size; i++)
|
||
{
|
||
if (i == row)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
int minorCol = 0;
|
||
for (int j = 0; j < size; j++)
|
||
{
|
||
if (j == col)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
minor[minorRow, minorCol] = matrix[i, j];
|
||
minorCol++;
|
||
}
|
||
|
||
minorRow++;
|
||
}
|
||
|
||
return CalculateDeterminantSequential(minor);
|
||
}
|
||
|
||
static int GetNumberOfThreads()
|
||
{
|
||
int threads = 1;
|
||
Console.WriteLine("Кол-во потоков:");
|
||
int.TryParse(Console.ReadLine(), out threads);
|
||
return threads;
|
||
}
|
||
|
||
static void PrintMatrix(int[,] matrix)
|
||
{
|
||
int size = matrix.GetLength(0);
|
||
for (int i = 0; i < size; i++)
|
||
{
|
||
for (int j = 0; j < size; j++)
|
||
{
|
||
Console.Write(matrix[i, j] + " ");
|
||
}
|
||
Console.WriteLine();
|
||
}
|
||
}
|
||
} |