-
Notifications
You must be signed in to change notification settings - Fork 0
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
artemiipatov
wants to merge
11
commits into
main
Choose a base branch
from
MatrixMultiplication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8d51ab9
Initial commit
artemiipatov 084fa1b
Changed number of threads
artemiipatov 23e0242
Fixed mistakes; removed automatic file creating after instantiation
artemiipatov 3a385b9
Make WriteMatrixToFile method public
artemiipatov 23d26de
Refactoring
artemiipatov e88ed1d
Generate new comparison table
artemiipatov 2dd68e0
Refactor; Round standard deviation to one decimal place; Remove unuse…
artemiipatov 0cd6e25
Refactor
artemiipatov eceadbc
refactor
artemiipatov 7978507
migrate to .NET 7.0
artemiipatov 54aff10
migrate tests to .NET 7.0
artemiipatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
MatrixMultiplication/MatrixMultiplication.Tests/MatrixMultiplication.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
75 changes: 75 additions & 0 deletions
75
MatrixMultiplication/MatrixMultiplication.Tests/MatrixMultiplicationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
[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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Неплохо бы, чтобы тест удалял создаваемые им файлы. Во избежание случайных зависимостей тестов по данным.