Anul 3 Semestrul 1
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab3
|
||||
{
|
||||
public class Matrix
|
||||
{
|
||||
public List<List<int>> result = [];
|
||||
public Matrix(int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
result.Add(new List<int>());
|
||||
for (int j = 0; j < size; j++)
|
||||
{
|
||||
result[i].Add(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int MatrixCellProduct(List<List<int>> A, List<List<int>> B, int i, int j)
|
||||
{
|
||||
int N = A.Count;
|
||||
int result = 0;
|
||||
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
result += A[i][k] * B[k][j];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void MatrixProduct(List<List<int>> A, List<List<int>> B, int numberOfThreads, int threadIndex)
|
||||
{
|
||||
int N = A.Count;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
for (int j = 0; j < N; j++)
|
||||
{
|
||||
if ((i + j) % numberOfThreads == threadIndex)
|
||||
{
|
||||
result[i][j] = MatrixCellProduct(A, B, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void MatrixProductUsingThreads(List<List<int>> A, List<List<int>> B, int numberOfThreads)
|
||||
{
|
||||
List<Thread> threads = new List<Thread>();
|
||||
for (int i = 0; i < numberOfThreads; i++)
|
||||
{
|
||||
int threadIndex = i;
|
||||
Thread thread = new Thread(() => MatrixProduct(A, B, numberOfThreads, threadIndex));
|
||||
threads.Add(thread);
|
||||
thread.Start();
|
||||
}
|
||||
for (int i = 0; i < numberOfThreads; i++)
|
||||
{
|
||||
threads[i].Join();
|
||||
}
|
||||
}
|
||||
|
||||
public void MatrixProductUsingThreadPool(List<List<int>> A, List<List<int>> B, int numberOfTasks)
|
||||
{
|
||||
var toProcess = numberOfTasks;
|
||||
ThreadPool.SetMinThreads(4, 4);
|
||||
using (ManualResetEvent resetEvent = new ManualResetEvent(false))
|
||||
{
|
||||
|
||||
for (int i = 0; i < numberOfTasks; i++)
|
||||
{
|
||||
int threadIndex = i;
|
||||
ThreadPool.QueueUserWorkItem((state) => {
|
||||
MatrixProduct(A, B, numberOfTasks, threadIndex);
|
||||
if (Interlocked.Decrement(ref toProcess) == 0)
|
||||
resetEvent.Set();
|
||||
});
|
||||
}
|
||||
resetEvent.WaitOne();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Lab3
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
List<List<int>> A = new List<List<int>>()
|
||||
{
|
||||
new List<int> { 1, 2, 3 },
|
||||
new List<int> { 4, 5, 6 },
|
||||
new List<int> { 7, 8, 9 }
|
||||
};
|
||||
|
||||
List<List<int>> B = new List<List<int>>()
|
||||
{
|
||||
new List<int> { 9, 8, 7 },
|
||||
new List<int> { 6, 5, 4 },
|
||||
new List<int> { 3, 2, 1 }
|
||||
};
|
||||
|
||||
var matrix = new Matrix(A.Count);
|
||||
|
||||
var timer = new Stopwatch();
|
||||
timer.Start();
|
||||
|
||||
//matrix.MatrixProductUsingThreads(A, B, 4);
|
||||
matrix.MatrixProductUsingThreadPool(A, B, 4);
|
||||
|
||||
timer.Stop();
|
||||
|
||||
Console.WriteLine($"Time elapsed: {timer.ElapsedMilliseconds} ms");
|
||||
|
||||
foreach (var row in matrix.result)
|
||||
{
|
||||
Console.WriteLine(string.Join(" ", row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user