-
Notifications
You must be signed in to change notification settings - Fork 0
Matrix mult #49
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
IgnatSergeev
wants to merge
14
commits into
main
Choose a base branch
from
matrix-mult
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
Matrix mult #49
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a239fdc
Init commit
IgnatSergeev 146e0a0
Added realisation
f4f6f17
Updated multithread variant
b3db32d
Added tests
IgnatSergeev e2b5d05
Fixed tests and added benchmark
IgnatSergeev e3edce6
Finished benchmark
IgnatSergeev d427e9c
Finished
IgnatSergeev 25adb99
Finished
IgnatSergeev bdbf47a
Changed ci
IgnatSergeev cef52da
Update C#/forSpbu/MatrixMult.Benchmark/MatrixMultBenchmark.cs
IgnatSergeev d3bedeb
Update C#/forSpbu/MatrixMult/Matrix.cs
IgnatSergeev b8e84d8
Fixed
IgnatSergeev d9bd4bd
Merge remote-tracking branch 'origin/matrix-mult' into matrix-mult
IgnatSergeev b041281
Changed order
IgnatSergeev 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
18 changes: 18 additions & 0 deletions
18
C#/forSpbu/MatrixMult.Benchmark/MatrixMult.Benchmark.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,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BenchmarkDotNet" Version="0.13.9" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\MatrixMult\MatrixMult.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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,41 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| namespace MatrixMult.Benchmark; | ||
|
|
||
| public class MatrixMultBenchmark | ||
| { | ||
| [Params(12, 24, 48, 96, 192, 384, 768, 1536)] | ||
| public int Size { get; set; } | ||
| private Matrix _fst = new Matrix(); | ||
| private Matrix _sec = new Matrix(); | ||
|
|
||
| [GlobalSetup] | ||
| public void GenerateRandomMatrix() | ||
| { | ||
| var random = new Random(); | ||
| var fstElements = new int[Size, Size]; | ||
| var secElements = new int[Size, Size]; | ||
| for (int i = 0; i < Size; i++) | ||
| { | ||
| for (int j = 0; j < Size; j++) | ||
| { | ||
| fstElements[i, j] = random.Next(); | ||
| secElements[i, j] = random.Next(); | ||
| } | ||
| } | ||
|
|
||
| _fst = new Matrix(fstElements); | ||
| _sec = new Matrix(secElements); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void MultiThreaded() | ||
| { | ||
| MatrixMultiplier.MultiThreadedMultiply(_fst, _sec); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void SingleThreaded() | ||
| { | ||
| MatrixMultiplier.SingleThreadedMultiply(_fst, _sec); | ||
| } | ||
| } |
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,5 @@ | ||
| using MatrixMult.Benchmark; | ||
| using BenchmarkDotNet.Running; | ||
|
|
||
| var table = BenchmarkRunner.Run<MatrixMultBenchmark>(); | ||
| Console.WriteLine(table); |
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,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
| <PackageReference Include="NUnit" Version="3.13.3" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.3.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.5.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\MatrixMult\MatrixMult.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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,108 @@ | ||
| namespace MatrixMult.Tests; | ||
|
|
||
| public static class MultTests | ||
| { | ||
| private static readonly (Matrix, Matrix, Matrix?)[] Tests = | ||
| { | ||
| ( | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 1, 2 }, | ||
| { 3, 4 } | ||
| }), | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 2, 3 }, | ||
| { 4, 5 } | ||
| }), | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 10, 13 }, | ||
| { 22, 29 } | ||
| }) | ||
| ), | ||
| ( | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 1, 2, 3 }, | ||
| { 4, 5, 6 } | ||
| }), | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 1, 2 }, | ||
| { 4, 5 } | ||
| }), | ||
| null | ||
| ), | ||
| ( | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 1, 2, 3 }, | ||
| { 4, 5, 6 } | ||
| }), | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 1, 2 }, | ||
| { 3, 4 }, | ||
| { 5, 6 } | ||
| }), | ||
| new Matrix( | ||
| new int[,] | ||
| { | ||
| { 22, 28 }, | ||
| { 49, 64 } | ||
| }) | ||
| ) | ||
| }; | ||
|
|
||
| private static IEnumerable<TestCaseData> TestCases() | ||
| { | ||
| foreach (var test in Tests) | ||
| { | ||
| yield return new TestCaseData(new Func<Matrix, Matrix, Matrix>(MatrixMultiplier.MultiThreadedMultiply), test); | ||
| yield return new TestCaseData(new Func<Matrix, Matrix, Matrix>(MatrixMultiplier.SingleThreadedMultiply), test); | ||
| } | ||
| } | ||
|
|
||
| private static bool AreMatricesEqual(Matrix fst, Matrix sec) | ||
| { | ||
| if (fst.Height != sec.Height || fst.Width != sec.Width) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < fst.Height; i++) | ||
| { | ||
| for (int j = 0; j < sec.Width; j++) | ||
| { | ||
| if (fst.GetElement(i, j) != sec.GetElement(i, j)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(TestCases))] | ||
| public static void MultiplyMatricesTest(Func<Matrix, Matrix, Matrix> matrixMultImpl, (Matrix, Matrix, Matrix?) test) | ||
| { | ||
| if (test.Item3 == null) | ||
| { | ||
| Assert.Throws<MatrixMultiplierException>(() => matrixMultImpl(test.Item1, test.Item2)); | ||
| } | ||
| else | ||
| { | ||
| var multResult = matrixMultImpl(test.Item1, test.Item2); | ||
| Assert.That(AreMatricesEqual(multResult, test.Item3), Is.True); | ||
| } | ||
| } | ||
| } |
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 @@ | ||
| global using NUnit.Framework; |
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,101 @@ | ||
| namespace MatrixMult; | ||
|
|
||
| /// <summary> | ||
| /// Class with matrix container | ||
| /// </summary> | ||
| public class Matrix | ||
| { | ||
| /// <summary> | ||
| /// Matrix elements | ||
| /// </summary> | ||
| private readonly int[,] _elements; | ||
|
|
||
| /// <summary> | ||
| /// Number of matrix rows | ||
| /// </summary> | ||
| public int Height => this._elements.GetLength(0); | ||
|
|
||
| /// <summary> | ||
| /// Number of matrix columns | ||
| /// </summary> | ||
| public int Width => this._elements.GetLength(1); | ||
|
|
||
| /// <summary> | ||
| /// Constructs empty matrix | ||
| /// </summary> | ||
| public Matrix() | ||
| { | ||
| this._elements = new int[0, 0]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Constructs matrix with given elements | ||
| /// </summary> | ||
| /// <param name="newElements">Matrix elements</param> | ||
| public Matrix(int [,] newElements) | ||
| { | ||
| this._elements = newElements; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Constructs matrix with given string lines | ||
| /// </summary> | ||
| /// <param name="lines">Array of lines to build matrix from</param> | ||
| /// <exception cref="MatrixCreationException">If given array of lines is incorrect</exception> | ||
| public Matrix(string[] lines) | ||
| { | ||
| var splitLines = lines.Select(str => str.Split(' ')).ToArray(); | ||
| var localHeight = splitLines.Length; | ||
| var localWidth = splitLines[0].Length; | ||
|
|
||
| this._elements = new int[localHeight, localWidth]; | ||
| for (int i = 0; i < localHeight; i++) | ||
| { | ||
| if (splitLines[i].Length != localWidth) | ||
| { | ||
| throw new MatrixCreationException("Inconsistent lines length"); | ||
| } | ||
|
|
||
| for (var j = 0; j < localWidth; j++) | ||
| { | ||
| if (!int.TryParse(splitLines[i][j], out var parseValue)) | ||
| { | ||
| throw new MatrixCreationException("Incorrect matrix element"); | ||
| } | ||
| this._elements[i, j] = parseValue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns element by index | ||
| /// </summary> | ||
| /// <param name="firstInd">First index</param> | ||
| /// <param name="secondInd">Second index</param> | ||
| /// <returns>Value of matrix element in given position</returns> | ||
| /// <exception cref="IndexOutOfRangeException">If some index is out of range</exception> | ||
| public int GetElement(int firstInd, int secondInd) => | ||
| firstInd < 0 || firstInd >= this.Height | ||
| ? throw new ArgumentOutOfRangeException(nameof(firstInd)) | ||
| : secondInd < 0 || secondInd >= this.Width | ||
| ? throw new ArgumentOutOfRangeException(nameof(secondInd)) | ||
| : this._elements[firstInd, secondInd]; | ||
|
|
||
| /// <summary> | ||
| /// Returns matrix as array of string | ||
| /// </summary> | ||
| /// <returns>String array matrix representation</returns> | ||
| public string[] GetStringRepresentation() | ||
| { | ||
| var lines = new string[this.Height]; | ||
| for (int i = 0; i < this.Height; i++) | ||
| { | ||
| for (int j = 0; j < this.Width; j++) | ||
| { | ||
| lines[i] += _elements[i, j] + " "; | ||
| } | ||
| } | ||
|
|
||
| return lines; | ||
| } | ||
| } | ||
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,15 @@ | ||
| namespace MatrixMult; | ||
|
|
||
| /// <summary> | ||
| /// Class for matrix creation exceptions | ||
| /// </summary> | ||
| public class MatrixCreationException : Exception | ||
| { | ||
| public MatrixCreationException() : base() | ||
| { | ||
| } | ||
|
|
||
| public MatrixCreationException(string message) : base(message) | ||
| { | ||
| } | ||
| } |
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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
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.
Всё-таки лучше как StyleCop советует: поля, конструкторы, свойства, методы