distributed-computing/tasks/mikhailov-ys/Main/Program.cs
2024-01-06 23:51:01 +04:00

173 lines
4.8 KiB
C#

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class MatrixMultiplication
{
public static int[][] MultiplySequential(int[][] matrixA, int[][] matrixB)
{
int rowsA = matrixA.Length;
int colsA = matrixA[0].Length;
int colsB = matrixB[0].Length;
int[][] result = new int[rowsA][];
for (int i = 0; i < rowsA; i++)
{
result[i] = new int[colsB];
for (int j = 0; j < colsB; j++)
{
for (int k = 0; k < colsA; k++)
{
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}
public static int[][] MultiplyParallel(int[][] matrixA, int[][] matrixB, int numThreads)
{
int rowsA = matrixA.Length;
int colsA = matrixA[0].Length;
int colsB = matrixB[0].Length;
int[][] result = new int[rowsA][];
Parallel.For(0, rowsA, new ParallelOptions { MaxDegreeOfParallelism = numThreads }, i =>
{
result[i] = new int[colsB];
for (int j = 0; j < colsB; j++)
{
for (int k = 0; k < colsA; k++)
{
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
});
return result;
}
[Benchmark]
public void MultiplySequential_100x100()
{
int[][] matrixA = GenerateRandomMatrix(100, 100);
int[][] matrixB = GenerateRandomMatrix(100, 100);
MultiplySequential(matrixA, matrixB);
}
[Benchmark]
public void MultiplyParallel_100x100_4TH()
{
int[][] matrixA = GenerateRandomMatrix(100, 100);
int[][] matrixB = GenerateRandomMatrix(100, 100);
MultiplyParallel(matrixA, matrixB, 4);
}
[Benchmark]
public void MultiplyParallel_100x100_8TH()
{
int[][] matrixA = GenerateRandomMatrix(100, 100);
int[][] matrixB = GenerateRandomMatrix(100, 100);
MultiplyParallel(matrixA, matrixB, 8);
}
[Benchmark]
public void MultiplyParallel_100x100_16TH()
{
int[][] matrixA = GenerateRandomMatrix(100, 100);
int[][] matrixB = GenerateRandomMatrix(100, 100);
MultiplyParallel(matrixA, matrixB, 16);
}
[Benchmark]
public void MultiplySequential_300x300()
{
int[][] matrixA = GenerateRandomMatrix(300, 300);
int[][] matrixB = GenerateRandomMatrix(300, 300);
MultiplySequential(matrixA, matrixB);
}
[Benchmark]
public void MultiplyParallel_300x300_4TH()
{
int[][] matrixA = GenerateRandomMatrix(300, 300);
int[][] matrixB = GenerateRandomMatrix(300, 300);
MultiplyParallel(matrixA, matrixB, 4);
}
[Benchmark]
public void MultiplyParallel_300x300_8TH()
{
int[][] matrixA = GenerateRandomMatrix(300, 300);
int[][] matrixB = GenerateRandomMatrix(300, 300);
MultiplyParallel(matrixA, matrixB, 8);
}
[Benchmark]
public void MultiplyParallel_300x300_16TH()
{
int[][] matrixA = GenerateRandomMatrix(300, 300);
int[][] matrixB = GenerateRandomMatrix(300, 300);
MultiplyParallel(matrixA, matrixB, 16);
}
[Benchmark]
public void MultiplySequential_500x500()
{
int[][] matrixA = GenerateRandomMatrix(500, 500);
int[][] matrixB = GenerateRandomMatrix(500, 500);
MultiplySequential(matrixA, matrixB);
}
[Benchmark]
public void MultiplyParallel_500x500_4TH()
{
int[][] matrixA = GenerateRandomMatrix(500, 500);
int[][] matrixB = GenerateRandomMatrix(500, 500);
MultiplyParallel(matrixA, matrixB, 4);
}
[Benchmark]
public void MultiplyParallel_500x500_8TH()
{
int[][] matrixA = GenerateRandomMatrix(500, 500);
int[][] matrixB = GenerateRandomMatrix(500, 500);
MultiplyParallel(matrixA, matrixB, 8);
}
[Benchmark]
public void MultiplyParallel_500x500_16TH()
{
int[][] matrixA = GenerateRandomMatrix(500, 500);
int[][] matrixB = GenerateRandomMatrix(500, 500);
MultiplyParallel(matrixA, matrixB, 16);
}
private static int[][] GenerateRandomMatrix(int rows, int cols)
{
int[][] matrix = new int[rows][];
Random rand = new Random();
for (int i = 0; i < rows; i++)
{
matrix[i] = new int[cols];
for (int j = 0; j < cols; j++)
{
matrix[i][j] = rand.Next(1, 10);
}
}
return matrix;
}
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<MatrixMultiplication>();
Console.ReadLine();
}
}