-
Notifications
You must be signed in to change notification settings - Fork 0
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
Matrix Multiplication #1
base: main
Are you sure you want to change the base?
Changes from 1 commit
8d51ab9
084fa1b
23e0242
3a385b9
23d26de
e88ed1d
2dd68e0
0cd6e25
eceadbc
7978507
54aff10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</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,73 @@ | ||||||
using System.IO; | ||||||
using NUnit.Framework; | ||||||
|
||||||
namespace MatrixMultiplication.Tests; | ||||||
|
||||||
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} }); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } }, "matrix1.txt"); | ||||||
var matrix2 = new DenseMatrix(new[,] { { 2, 9 }, { 1, 7 }, {4, 19} }, "matrix2.txt"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 } }, "matrix1.txt"); | ||||||
var matrix2 = new DenseMatrix(new[,] { { 100 } }, "matrix2.txt"); | ||||||
var result1 = DenseMatrix.MultiplyConcurrently(matrix1, matrix2); | ||||||
var result2 = DenseMatrix.MultiplySerially(matrix1, matrix2); | ||||||
Assert.AreEqual(result1.Matrix, new[,] { {900} }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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); | ||||||
} | ||||||
} |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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="N"></param> | ||
/// <param name="calculationTime"></param> | ||
/// <returns></returns> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пустые тэги не нужны |
||
public static double CalculateDeviation(int N, long[] calculationTime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не, параметры именуются со строчной всегда |
||
{ | ||
var expectedValue = CalculateExpectedValue(N, calculationTime); | ||
double variance = 0; | ||
for (var i = 0; i < N; i++) | ||
{ | ||
variance += Math.Pow(calculationTime[i] - expectedValue, 2) / N; | ||
} | ||
|
||
return Math.Sqrt(variance); | ||
} | ||
|
||
/// <summary> | ||
/// Calculates expected value (which is arithmetic average in the context of matrix multiplication time). | ||
/// </summary> | ||
/// <param name="N">Number of multiplications.</param> | ||
/// <param name="calculationTime">Array, which elements are time of matrix multiplications.</param> | ||
/// <returns></returns> | ||
public static long CalculateExpectedValue(int N, long[] calculationTime) => calculationTime.Sum() / N; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Верно ли, что N всегда должно быть равно calculationTime.Length? |
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
using System.Diagnostics; | ||||||
using MatrixMultiplication; | ||||||
using Matrix = MatrixMultiplication.DenseMatrix; | ||||||
|
||||||
const int N = 10; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И даже константы именуются со строчной обычно |
||||||
|
||||||
Table.CreateTable("table.txt"); | ||||||
|
||||||
for (var j = 1; j < 9; j++) | ||||||
{ | ||||||
long[] calculationTime = new long[N]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var size = j * 100; | ||||||
for (var i = 0; i < N; i++) | ||||||
{ | ||||||
Matrix.MatrixGenerator("matrix1.txt", (size, size)); | ||||||
Matrix.MatrixGenerator("matrix2.txt", (size, size)); | ||||||
var matrix1 = new Matrix("matrix1.txt"); | ||||||
var matrix2 = new Matrix("matrix2.txt"); | ||||||
var stopwatch = new Stopwatch(); | ||||||
stopwatch.Start(); | ||||||
Matrix.MultiplyConcurrently(matrix1, matrix2); | ||||||
stopwatch.Stop(); | ||||||
calculationTime[i] = stopwatch.ElapsedMilliseconds; | ||||||
} | ||||||
|
||||||
var expectedValue1 = Comparison.CalculateExpectedValue(N, calculationTime); | ||||||
var deviation1 = Comparison.CalculateDeviation(N, calculationTime); | ||||||
|
||||||
for (var i = 0; i < N; i++) | ||||||
{ | ||||||
Matrix.MatrixGenerator("matrix1.txt", (size, size)); | ||||||
Matrix.MatrixGenerator("matrix2.txt", (size, size)); | ||||||
var matrix1 = new Matrix("matrix1.txt"); | ||||||
var matrix2 = new Matrix("matrix2.txt"); | ||||||
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 = Comparison.CalculateExpectedValue(N, calculationTime); | ||||||
var deviation2 = Comparison.CalculateDeviation(N, calculationTime); | ||||||
|
||||||
Table.WriteDataToTable("table.txt", (size, size), expectedValue1, deviation1, expectedValue2, deviation2); | ||||||
} |
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.
Лучше namespace писать первой строкой файла (сразу после шапки с лицензией, которую мы по традиции не пишем почему-то). Это более консистентно с синтаксисом других языков типа Java, и в целом более аккуратно.