Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 382 additions & 46 deletions .gitignore

Large diffs are not rendered by default.

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"));
}
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 1 2
4 1 5
1 5 3
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 ParallelMatrixMultiplication/ParallelMatrixMultiplication.sln
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
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)
{
}
}
}
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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 1 2
4 1 5
1 5 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
9 3 1
4 6 9
1 5 7
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)

This comment was marked as resolved.

{
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.

}

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;
}
}
}
Loading