Files
2025-10-10 13:50:40 +04:00

125 lines
4.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Console.Write("Введите размер матрицы: ");
int size = int.Parse(Console.ReadLine());
double[,] matrix = GenerateMatrix(size);
WriteArrayToFile("Data/Result.txt", matrix);
Console.Write("Введите количество потоков: ");
int threadCount = int.Parse(Console.ReadLine());
Stopwatch parallelStopwatch = Stopwatch.StartNew();
double determinant = ParallelGaussianDeterminant(matrix, threadCount);
parallelStopwatch.Stop();
Console.WriteLine($"\nОпределитель матрицы: {determinant}");
// Сравнение с однопоточным вычислением
Stopwatch sequentialStopwatch = Stopwatch.StartNew();
double sequentialDet = ParallelGaussianDeterminant(matrix,1);
sequentialStopwatch.Stop();
Console.WriteLine($"Однопоточный результат: {sequentialDet}");
Console.WriteLine($"\nОднопоточный алгоритм: {sequentialStopwatch.ElapsedMilliseconds} мс ");
Console.WriteLine($"Параллельный алгоритм ({threadCount} потоков): {parallelStopwatch.ElapsedMilliseconds} мс");
}
static double ParallelGaussianDeterminant(double[,] matrix, int threadCount)
{
int n = matrix.GetLength(0);
double[,] tempMatrix = (double[,])matrix.Clone();
double determinant = 1;
for (int i = 0; i < n - 1; i++)
{
int pivotRow = i;
double maxVal = Math.Abs(tempMatrix[i, i]);
for (int k = i + 1; k < n; k++)
{
if (Math.Abs(tempMatrix[k, i]) > maxVal)
{
maxVal = Math.Abs(tempMatrix[k, i]);
pivotRow = k;
}
}
if (pivotRow != i)
{
SwapRows(tempMatrix, i, pivotRow, n);
determinant *= -1;
}
if (Math.Abs(tempMatrix[i, i]) < 1e-15)
return 0;
determinant *= tempMatrix[i, i];
Parallel.For(i + 1, n, new ParallelOptions { MaxDegreeOfParallelism = threadCount },
k =>
{
double factor = tempMatrix[k, i] / tempMatrix[i, i];
for (int j = i + 1; j < n; j++)
{
tempMatrix[k, j] -= factor * tempMatrix[i, j];
}
tempMatrix[k, i] = 0;
});
}
determinant *= tempMatrix[n - 1, n - 1];
return determinant;
}
static double[,] GenerateMatrix(int size)
{
Random rand = new Random();
double[,] matrix = new double[size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
matrix[i, j] = rand.Next(1, 10) + rand.NextDouble();
}
}
return matrix;
}
static void SwapRows(double[,] matrix, int row1, int row2, int n)
{
for (int j = 0; j < n; j++)
{
double temp = matrix[row1, j];
matrix[row1, j] = matrix[row2, j];
matrix[row2, j] = temp;
}
}
static void WriteArrayToFile(string filePath, double[,] array)
{
using (StreamWriter writer = new StreamWriter(filePath))
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
writer.Write(array[i, j]);
if (j < cols - 1)
writer.Write(" ");
}
writer.WriteLine();
}
}
}
}