-
Notifications
You must be signed in to change notification settings - Fork 0
Parallel 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
MikePuzanov
wants to merge
24
commits into
master
Choose a base branch
from
ParallelMatrixMultiplication
base: master
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
24 commits
Select commit
Hold shift + click to select a range
a8c4eb9
add FileFunctions.cs and MatrixFunctions.cs
lykwer 9b217a4
delete and add files
lykwer ba0474a
.csproj and .sln
lykwer 8ca1603
wip
lykwer 56e60b3
wip
lykwer 723eb27
wip
lykwer a46fb5c
add two .txt
lykwer 3a35213
appveyor.yml
lykwer 350dc36
add threads and MatrixFunctionsTest.cs
lykwer 8d2f7c9
add StopWatch
lykwer 033f98a
code tests
lykwer dedd9bf
fix tests
lykwer 04d64ad
.gitignore
lykwer 37d8504
fix
lykwer 0521592
rename test
lykwer 70ac922
fix
lykwer 715d2c1
fix styleguide
lykwer 54346a5
fix
lykwer d944a26
fix problems
lykwer 5204153
fix code
lykwer adbe04d
fix code
lykwer 5b1f8b5
fix code
lykwer 6bbf2d3
fix
lykwer f09df3d
fix code
lykwer 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
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
32 changes: 32 additions & 0 deletions
32
ParallelMatrixMultiplication/ParallelMatrixMultiplication.Test/FileFunctionsTest.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,32 @@ | ||
| using NUnit.Framework; | ||
|
|
||
| namespace ParallelMatrixMultiplication.Test | ||
| { | ||
| public class FileFunctionTest | ||
| { | ||
| [Test] | ||
| public void TestNormalFilePath() | ||
| { | ||
| int[,] result = | ||
| { | ||
| {2, 1, 2}, | ||
| {4, 1, 5}, | ||
| {1, 5, 3} | ||
| }; | ||
| var resultFromFile = FunctionsForFile.CreateMatrix("../../../MatrixTest1.txt"); | ||
| Assert.AreEqual(result, resultFromFile); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestAbnormalFilePath() | ||
| { | ||
| Assert.Throws<WrongFilePathException>(() => FunctionsForFile.CreateMatrix(null)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEmptyFile() | ||
| { | ||
| Assert.Throws<EmptyFileException>(() => FunctionsForFile.CreateMatrix("../../../EmptyFileTest.txt")); | ||
| } | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
ParallelMatrixMultiplication/ParallelMatrixMultiplication.Test/MatrixFunctionTest.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 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace ParallelMatrixMultiplication.Test | ||
| { | ||
| public class MatrixFunctionTest | ||
| { | ||
| [TestCaseSource(nameof(FunctionsForTest))] | ||
| public void TestNormalSquareData(Func<int[,], int[,], int[,]> multiplication) | ||
| { | ||
| int[,] result = | ||
| { | ||
| {15, 27, 8}, | ||
| {24, 57, 13}, | ||
| {41, 26, 24} | ||
| }; | ||
| int[,] matrix1 = | ||
| { | ||
| {2, 1, 2}, | ||
| {4, 1, 5}, | ||
| {1, 5, 3} | ||
| }; | ||
| int[,] matrix2 = | ||
| { | ||
| {3, 9, 1}, | ||
| {7, 1, 4}, | ||
| {1, 4, 1} | ||
| }; | ||
| var matrix = multiplication(matrix1, matrix2); | ||
| Assert.AreEqual(result, matrix); | ||
| } | ||
|
|
||
| [TestCaseSource(nameof(FunctionsForTest))] | ||
| public void TestNormalNotSquareData(Func<int[,], int[,], int[,]> multiplication) | ||
| { | ||
| int[,] result = | ||
| { | ||
| {36, 71, 29}, | ||
| {30, 22, 23}, | ||
| {49, 87, 28} | ||
| }; | ||
| int[,] matrix1 = | ||
| { | ||
| {6, 1, 3, 4}, | ||
| {1, 3, 2, 2}, | ||
| {7, 3, 5, 1} | ||
| }; | ||
| int[,] matrix2 = | ||
| { | ||
| {3, 9, 1}, | ||
| {7, 1, 4}, | ||
| {1, 4, 1}, | ||
| {2, 1, 4} | ||
| }; | ||
| var matrix = multiplication(matrix1, matrix2); | ||
| Assert.AreEqual(result, matrix); | ||
| } | ||
|
|
||
| [TestCaseSource(nameof(FunctionsForTest))] | ||
| public void TestAbnormalData(Func<int[,], int[,], int[,]> multiplication) | ||
| { | ||
| var matrix1 = new int[3, 4]; | ||
| var matrix2 = new int[3, 3]; | ||
| Assert.Throws<MultiplicationException>(() => multiplication(matrix1, matrix2)); | ||
| } | ||
|
|
||
| private static IEnumerable<Func<int[,], int[,], int[,]>> FunctionsForTest() | ||
| { | ||
| yield return MatrixFunctions.MatrixMultiplicationParallel; | ||
| yield return MatrixFunctions.MatrixMultiplication; | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
ParallelMatrixMultiplication/ParallelMatrixMultiplication.Test/MatrixTest1.txt
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,3 @@ | ||
| 2 1 2 | ||
| 4 1 5 | ||
| 1 5 3 |
20 changes: 20 additions & 0 deletions
20
...Multiplication/ParallelMatrixMultiplication.Test/ParallelMatrixMultiplication.Test.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,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" /> | ||
| <PackageReference Include="NUnit" Version="3.13.1" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.0.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\ParallelMatrixMultiplication\ParallelMatrixMultiplication.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
31 changes: 31 additions & 0 deletions
31
ParallelMatrixMultiplication/ParallelMatrixMultiplication.sln
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,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.31624.102 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelMatrixMultiplication", "ParallelMatrixMultiplication\ParallelMatrixMultiplication.csproj", "{A8AF63A3-1364-4FA2-8832-D27A339FAD1B}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelMatrixMultiplication.Test", "ParallelMatrixMultiplication.Test\ParallelMatrixMultiplication.Test.csproj", "{A569F36C-B06B-4DE2-AC6A-0A26D8C140A1}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {A8AF63A3-1364-4FA2-8832-D27A339FAD1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A8AF63A3-1364-4FA2-8832-D27A339FAD1B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A8AF63A3-1364-4FA2-8832-D27A339FAD1B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A8AF63A3-1364-4FA2-8832-D27A339FAD1B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {A569F36C-B06B-4DE2-AC6A-0A26D8C140A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A569F36C-B06B-4DE2-AC6A-0A26D8C140A1}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A569F36C-B06B-4DE2-AC6A-0A26D8C140A1}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A569F36C-B06B-4DE2-AC6A-0A26D8C140A1}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {3EC4E190-8ABB-4682-A8B2-190BAE5FEFB0} | ||
| EndGlobalSection | ||
| EndGlobal |
21 changes: 21 additions & 0 deletions
21
ParallelMatrixMultiplication/ParallelMatrixMultiplication/EmptyFileException.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,21 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace ParallelMatrixMultiplication | ||
| { | ||
| /// <summary> | ||
| /// then file is empty | ||
| /// </summary> | ||
| public class EmptyFileException : Exception | ||
| { | ||
| public EmptyFileException() | ||
| { | ||
| } | ||
|
|
||
| public EmptyFileException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
ParallelMatrixMultiplication/ParallelMatrixMultiplication/FunctionsForFile.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,98 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace ParallelMatrixMultiplication | ||
| { | ||
| /// <summary> | ||
| /// Functions for working with files | ||
| /// </summary> | ||
| public static class FunctionsForFile | ||
| { | ||
| private static void CheckFilePath(string filePath) | ||
| { | ||
| if (string.IsNullOrEmpty(filePath)) | ||
| { | ||
| throw new WrongFilePathException("File path is incorrect!"); | ||
| } | ||
| } | ||
|
|
||
| private static (int, int) CountMatrixSize(string filePath) | ||
| { | ||
| CheckFilePath(filePath); | ||
| using var file = new StreamReader(filePath); | ||
| string line = file.ReadLine(); | ||
| int size = 0; | ||
| if (string.IsNullOrEmpty(line)) | ||
| { | ||
| throw new EmptyFileException("File is empty!"); | ||
| } | ||
|
|
||
| string[] lineDrop = line.Split(" ", StringSplitOptions.RemoveEmptyEntries); | ||
| while (line != null) | ||
| { | ||
| size++; | ||
| line = file.ReadLine(); | ||
| } | ||
|
|
||
| return (size, lineDrop.Length); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create Matrix from file | ||
| /// </summary> | ||
| public static int[,] CreateMatrix(string filePath) | ||
| { | ||
| CheckFilePath(filePath); | ||
| (int length, int width) size = CountMatrixSize(filePath); | ||
| var matrix = new int[size.length, size.width]; | ||
| using var file = new StreamReader(filePath); | ||
| string line = file.ReadLine(); | ||
| var index = 0; | ||
| while (line != null) | ||
| { | ||
| string[] lineDrop = line.Split(" ", StringSplitOptions.RemoveEmptyEntries); | ||
| for (int i = 0; i < size.length; ++i) | ||
| { | ||
| matrix[index, i] = Int32.Parse(lineDrop[i]); | ||
| } | ||
|
|
||
| index++; | ||
| line = file.ReadLine(); | ||
| } | ||
|
|
||
| return matrix; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create file and return matrix | ||
| /// </summary> | ||
| public static void CreateFileWithMatrix(string filePath, int[,] matrix) | ||
| { | ||
| CheckFilePath(filePath); | ||
| var fileOut = new FileInfo(filePath); | ||
| if (fileOut.Exists) | ||
| { | ||
| fileOut.Delete(); | ||
| } | ||
|
|
||
| using var newFile = new FileStream(filePath, FileMode.Create); | ||
| using var file = new StreamWriter(newFile); | ||
| for (int i = 0; i < matrix.GetLength(0); i++) | ||
| { | ||
| string line = ""; | ||
| for (int j = 0; j < matrix.GetLength(1); j++) | ||
| { | ||
| line += $"{matrix[i, j]} "; | ||
| } | ||
|
|
||
| file.WriteLine(line); | ||
| } | ||
|
|
||
| file.Close(); | ||
| if (fileOut.Exists) | ||
| { | ||
| fileOut.MoveTo(filePath); | ||
| } | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
ParallelMatrixMultiplication/ParallelMatrixMultiplication/Matrix1.txt
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,3 @@ | ||
| 2 1 2 | ||
| 4 1 5 | ||
| 1 5 3 |
3 changes: 3 additions & 0 deletions
3
ParallelMatrixMultiplication/ParallelMatrixMultiplication/Matrix2.txt
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,3 @@ | ||
| 9 3 1 | ||
| 4 6 9 | ||
| 1 5 7 |
85 changes: 85 additions & 0 deletions
85
ParallelMatrixMultiplication/ParallelMatrixMultiplication/MatrixFunctions.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,85 @@ | ||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace ParallelMatrixMultiplication | ||
| { | ||
| /// <summary> | ||
| /// Class with matrix multiplication | ||
| /// </summary> | ||
| public static class MatrixFunctions | ||
| { | ||
| /// <summary> | ||
| /// Parallel matrix multiplication | ||
| /// </summary> | ||
| public static int[,] MatrixMultiplicationParallel(int[,] matrix1, int[,] matrix2) | ||
| { | ||
| if (matrix1.GetLength(1) != matrix2.GetLength(0)) | ||
| { | ||
| throw new MultiplicationException( | ||
| "Number of columns in the first matrix are not equal to the rows in the second matrix!"); | ||
| } | ||
|
|
||
| var matrix = new int[matrix1.GetLength(0), matrix2.GetLength(1)]; | ||
| var size = matrix1.GetLength(0) < Environment.ProcessorCount | ||
| ? matrix1.GetLength(0) | ||
| : Environment.ProcessorCount; | ||
| var threads = new Thread[size]; | ||
| var chunkSize = matrix.GetLength(0) / threads.Length + 1; | ||
| for (int i = 0; i < threads.Length; ++i) | ||
| { | ||
| var localI = i; | ||
| threads[i] = new Thread(() => | ||
| { | ||
| for (var l = localI * chunkSize; l < (localI + 1) * chunkSize && l < matrix1.GetLength(0); ++l) | ||
| { | ||
| for (int j = 0; j < matrix2.GetLength(1); j++) | ||
| { | ||
| for (int k = 0; k < matrix1.GetLength((1)); k++) | ||
| { | ||
| matrix[l, j] += matrix1[l, k] * matrix2[k, j]; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| foreach (var thread in threads) | ||
| { | ||
| thread.Start(); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| } | ||
|
|
||
| foreach (var thread in threads) | ||
| { | ||
| thread.Join(); | ||
| } | ||
|
|
||
| return matrix; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Not parallel matrix multiplication | ||
| /// </summary> | ||
| public static int[,] MatrixMultiplication(int[,] matrix1, int[,] matrix2) | ||
| { | ||
| if (matrix1.GetLength(1) != matrix2.GetLength(0)) | ||
| { | ||
| throw new MultiplicationException( | ||
| "Number of columns in the first matrix are not equal to the rows in the second matrix!"); | ||
| } | ||
|
|
||
| var matrix = new int[matrix1.GetLength(0), matrix2.GetLength(1)]; | ||
| for (int i = 0; i < matrix1.GetLength(0); i++) | ||
| { | ||
| for (int j = 0; j < matrix2.GetLength(1); j++) | ||
| { | ||
| for (int k = 0; k < matrix1.GetLength((1)); k++) | ||
| { | ||
| matrix[i, j] += matrix1[i, k] * matrix2[k, j]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return matrix; | ||
| } | ||
| } | ||
| } | ||
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.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.