Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,4 @@ FodyWeavers.xsd

# Rider
.idea
.git
.git
22 changes: 22 additions & 0 deletions HW1/HW1.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatrixMultiplication", "MatrixMultiplication\MatrixMultiplication.csproj", "{3C1ACBE6-BD12-4934-A58E-10141C3D6419}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "matrixmultiplication.Test", "matrixmultiplication.Test\matrixmultiplication.Test.csproj", "{93242727-EAF9-411B-AFFD-8FFFF083BE84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3C1ACBE6-BD12-4934-A58E-10141C3D6419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C1ACBE6-BD12-4934-A58E-10141C3D6419}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C1ACBE6-BD12-4934-A58E-10141C3D6419}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C1ACBE6-BD12-4934-A58E-10141C3D6419}.Release|Any CPU.Build.0 = Release|Any CPU
{93242727-EAF9-411B-AFFD-8FFFF083BE84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93242727-EAF9-411B-AFFD-8FFFF083BE84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93242727-EAF9-411B-AFFD-8FFFF083BE84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93242727-EAF9-411B-AFFD-8FFFF083BE84}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
89 changes: 89 additions & 0 deletions HW1/MatrixMultiplication/MatrixBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// <copyright file="MatrixBench.cs" company="khusainovilas">
// Copyright (c) khusainovilas. All rights reserved.
// </copyright>

namespace MatrixMultiplication;

using System;
using System.Diagnostics;
using System.Linq;

/// <summary>
/// Compare the operating speed with the sequential version depending on the matrix sizes.
/// </summary>
public static class MatrixBench
{
/// <summary>
/// Runs the benchmark for multiple matrix sizes and saves the results to a file.
/// </summary>
public static void RunBenchmark()
{
int[] sizes = [100, 200, 300, 400, 500];
const string filePath = "resultBenchmark.txt";

File.WriteAllText(filePath, $"{"Size",4} {"Seq_Exp",10} {"Seq_Dev",10} {"Par_Exp",10} {"Par_Dev",10}\n");

foreach (var size in sizes)
{
var sequentialResult = BenchmarkMatrixMultiplication(size, false);
var parallelResult = BenchmarkMatrixMultiplication(size, true);

var line =
$"{size,4} {sequentialResult.Expectation,10:F4} {sequentialResult.Deviation,10:F4} {parallelResult.Expectation,10:F4} {parallelResult.Deviation,10:F4}";

File.AppendAllText("resultBenchmark.txt", line + Environment.NewLine);
}
}

/// <summary>
/// Runs a matrix multiplication benchmark and computes mathematical expectation and standard deviation.
/// </summary>
/// <param name="size">The number of rows and columns of the square matrix.</param>
/// <param name="useParallel">If true, uses parallel matrix multiplication.</param>
/// <param name="repeat">Number of measurement repetitions for statistics (default 100).</param>
/// <returns>
/// Tuple (expectation, deviation):.
/// - Expectation: mathematical expectation time in milliseconds.
/// - Deviation: standard deviation time in milliseconds.
/// </returns>
private static (double Expectation, double Deviation) BenchmarkMatrixMultiplication(int size, bool useParallel, int repeat = 10)
{
var elapsedTimes = new double[repeat];
for (var i = 0; i < repeat; i++)
{
elapsedTimes[i] = PerformSingleRun(size, useParallel);
}

var expectation = elapsedTimes.Average();
var deviation = Math.Sqrt(elapsedTimes.Average(t => Math.Pow(t - expectation, 2)));

return (expectation, deviation);
}

/// <summary>
/// Performs a single run of matrix multiplication with randomly generated matrix.
/// </summary>
/// <param name="size">The number of rows and columns of the square matrix.</param>
/// <param name="useParallel">If true, uses parallel matrix multiplication.</param>
/// <returns>Elapsed time in milliseconds for a single multiplication.</returns>
private static long PerformSingleRun(int size, bool useParallel)
{
var matrix1 = MatrixUtils.GenerateRandomMatrix(size, size);
var matrix2 = MatrixUtils.GenerateRandomMatrix(size, size);

var stopwatch = Stopwatch.StartNew();

if (useParallel)
{
MatrixUtils.MultiplyMatrixParallel(matrix1, matrix2);
}
else
{
MatrixUtils.MultiplyMatrix(matrix1, matrix2);
}

stopwatch.Stop();

return stopwatch.ElapsedMilliseconds;
}
}
37 changes: 37 additions & 0 deletions HW1/MatrixMultiplication/MatrixFormatException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <copyright file="MatrixFormatException.cs" company="khusainovilas">
// Copyright (c) khusainovilas. All rights reserved.
// </copyright>

namespace MatrixMultiplication;

/// <summary>
/// The exception that is thrown when a matrix file has an invalid format.
/// </summary>
public class MatrixFormatException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="MatrixFormatException"/> class.
/// </summary>
public MatrixFormatException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MatrixFormatException"/> class with a message.
/// </summary>
/// <param name="message">The error message.</param>
public MatrixFormatException(string message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MatrixFormatException"/> class with a message and inner exception.
/// </summary>
/// <param name="message">The error message.</param>
/// <param name="innerException">The inner exception.</param>
public MatrixFormatException(string message, Exception innerException)
: base(message, innerException)
{
}
}
27 changes: 27 additions & 0 deletions HW1/MatrixMultiplication/MatrixMultiplication.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<Folder Include="TestMatrix\" />
</ItemGroup>

</Project>
Loading