Skip to content

Matrix Multiplication #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
dotnet-version: '7.x'
- name: Build
run: for dir in */; do cd $dir; for sln in *.sln; do dotnet build $sln; cd ..; done; done
shell: bash
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<LangVersion>11</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MatrixMultiplication\MatrixMultiplication.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace MatrixMultiplication.Tests;

using System.IO;
using NUnit.Framework;

public class Tests
{
[Test]
public void FileIsReadCorrectly()
{
using var file1 = new StreamWriter(File.Create("matrix1.txt"));
using var file2 = new StreamWriter(File.Create("matrix2.txt"));
file1.WriteLine("10 4 5");
file1.WriteLine("8 9 1");
file2.WriteLine("2 9");
file2.WriteLine("1 7");
file2.WriteLine("4 19");
file1.Close();
file2.Close();
var matrix1 = new DenseMatrix("matrix1.txt");
var matrix2 = new DenseMatrix("matrix2.txt");
Assert.AreEqual(matrix1.Matrix, new[,] { { 10, 4, 5 }, { 8, 9, 1 } });
Assert.AreEqual(matrix2.Matrix, new[,] { { 2, 9 }, { 1, 7 }, { 4, 19 } });
File.Delete("matrix1.txt");
File.Delete("matrix2.txt");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неплохо бы, чтобы тест удалял создаваемые им файлы. Во избежание случайных зависимостей тестов по данным.


[Test]
public void SerialCalculationsAreCorrect()
{
var correctResult = new[,] { { 44, 213 }, { 29, 154 } };
var matrix1 = new DenseMatrix(new[,] { { 10, 4, 5 }, { 8, 9, 1 } });
var matrix2 = new DenseMatrix(new[,] { { 2, 9 }, { 1, 7 }, { 4, 19 } });
var result = DenseMatrix.MultiplySerially(matrix1, matrix2);
Assert.AreEqual(correctResult, result.Matrix);
}

[Test]
public void SerialCalculationsAndConcurrentCalculationsAreEqual()
{
for (int i = 0; i < 10; i++)
{
DenseMatrix.MatrixGenerator("matrix1.txt", (9, 10));
DenseMatrix.MatrixGenerator("matrix2.txt", (10, 8));
var matrix1 = new DenseMatrix("matrix1.txt");
var matrix2 = new DenseMatrix("matrix2.txt");
var result1 = DenseMatrix.MultiplyConcurrently(matrix1, matrix2);
var result2 = DenseMatrix.MultiplySerially(matrix1, matrix2);
Assert.AreEqual(result1.Matrix, result2.Matrix);
}
}

[Test]
public void SerialAndConcurrentCalculationsCanMultiply1X1Matrices()
{
var matrix1 = new DenseMatrix(new[,] { { 9 } });
var matrix2 = new DenseMatrix(new[,] { { 100 } });
var result1 = DenseMatrix.MultiplyConcurrently(matrix1, matrix2);
var result2 = DenseMatrix.MultiplySerially(matrix1, matrix2);
Assert.AreEqual(result1.Matrix, new[,] { { 900 } });
Assert.AreEqual(result1.Matrix, result2.Matrix);
}

[Test]
public void SerialAndConcurrentCalculationsCanMultiplyVectors()
{
DenseMatrix.MatrixGenerator("matrix1.txt", (1, 900));
DenseMatrix.MatrixGenerator("matrix2.txt", (900, 1));
var matrix1 = new DenseMatrix("matrix1.txt");
var matrix2 = new DenseMatrix("matrix2.txt");
var result1 = DenseMatrix.MultiplyConcurrently(matrix1, matrix2);
var result2 = DenseMatrix.MultiplySerially(matrix1, matrix2);
Assert.AreEqual(result1.Matrix, result2.Matrix);
}
}
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions MatrixMultiplication/MatrixMultiplication.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", "{12DC35BA-E0BB-4B8A-8DD4-B762EA24490A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatrixMultiplication.Tests", "MatrixMultiplication.Tests\MatrixMultiplication.Tests.csproj", "{A5D3DA1C-4802-43C2-8C7E-29C49AAC7B4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12DC35BA-E0BB-4B8A-8DD4-B762EA24490A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12DC35BA-E0BB-4B8A-8DD4-B762EA24490A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12DC35BA-E0BB-4B8A-8DD4-B762EA24490A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12DC35BA-E0BB-4B8A-8DD4-B762EA24490A}.Release|Any CPU.Build.0 = Release|Any CPU
{A5D3DA1C-4802-43C2-8C7E-29C49AAC7B4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5D3DA1C-4802-43C2-8C7E-29C49AAC7B4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5D3DA1C-4802-43C2-8C7E-29C49AAC7B4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5D3DA1C-4802-43C2-8C7E-29C49AAC7B4F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions MatrixMultiplication/MatrixMultiplication/Comparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace MatrixMultiplication;

/// <summary>
/// Tools for matrix multiplication comparison.
/// </summary>
public static class Comparison
{
/// <summary>
/// Calculates standard deviation using time of matrix multiplications and number of multiplications.
/// </summary>
/// <param name="calculationTime">Array, which elements are time of matrix multiplications.</param>
/// <returns>Returns double value -- rounded to one decimal place standard deviation of the given data.</returns>
public static double CalculateDeviation(long[] calculationTime)
{
var expectedValue = calculationTime.Average();
double variance = 0;

for (var i = 0; i < calculationTime.Length; i++)
{
variance += Math.Pow(calculationTime[i] - expectedValue, 2) / calculationTime.Length;
}

return Math.Round(Math.Sqrt(variance), 1);
}
}
45 changes: 45 additions & 0 deletions MatrixMultiplication/MatrixMultiplication/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Diagnostics;
using MatrixMultiplication;
using Matrix = MatrixMultiplication.DenseMatrix;

const int n = 10;

TableBuilder.CreateTable("table.txt");

for (var j = 1; j < 9; j++)
{
var size = j * 100;
var calculationTime = new long[n];

Matrix.MatrixGenerator("matrix1.txt", (size, size));
Matrix.MatrixGenerator("matrix2.txt", (size, size));
var matrix1 = new Matrix("matrix1.txt");
var matrix2 = new Matrix("matrix2.txt");

for (var i = 0; i < n; i++)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Matrix.MultiplyConcurrently(matrix1, matrix2);
stopwatch.Stop();
calculationTime[i] = stopwatch.ElapsedMilliseconds;
}

// var expectedValue1 = Comparison.CalculateExpectedValue(calculationTime);
var expectedValue1 = calculationTime.Average();
var deviation1 = Comparison.CalculateDeviation(calculationTime);

for (var i = 0; i < n; i++)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Matrix.MultiplySerially(matrix1, matrix2);

This comment was marked as resolved.

stopwatch.Stop();
calculationTime[i] = stopwatch.ElapsedMilliseconds;
}

var expectedValue2 = calculationTime.Average();
var deviation2 = Comparison.CalculateDeviation(calculationTime);

TableBuilder.WriteDataToTable("table.txt", (size, size), expectedValue1, deviation1, expectedValue2, deviation2);
}
Loading