using System using System Collections Generic using System Linq using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Concurrent;
namespace ParallelMultiply
{
class Multiply
{
private int[,] FirstMatrix,SecondMatrix,FinalMatrix;
private int Size,ThreadsNumber;
private int[] StatOfThreads;
public Multiply() {
}
private void GenerateMatrix(int[,] FirstMatrix, int Size) {
Random rand = new Random();
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
FirstMatrix[i,j] = rand.Next(-100, 100);
}
}
}
public void Generate() {
this.FirstMatrix = new int[this.Size, this.Size];
this.SecondMatrix = new int[this.Size, this.Size];
this.FinalMatrix = new int[this.Size, this.Size];
this.GenerateMatrix(this.FirstMatrix, this.Size);
this.GenerateMatrix(this.SecondMatrix, this.Size);
this.GenerateMatrix(this.FinalMatrix, this.Size);
}
private void ThreadFnc(ParallelOptions op)
{
Parallel.For(0, this.Size,op, i =>
{
for (int j = 0; j < this.Size; j++)
{
int temp = 0;
for (int k = 0; k < this.Size; k++)
{
temp += this.FirstMatrix[i, k] * this.SecondMatrix[k, j];
}
this.FinalMatrix[i, j] = temp;
}
});
}
public void DoMultiply()
{
this.StatOfThreads = new int[this.ThreadsNumber];
ParallelOptions options = new ParallelOptions();
Stopwatch sw = new Stopwatch();
for (int j = 1; j <= this.ThreadsNumber; j++)
{
ThreadPool.SetMinThreads(j,1);
options.MaxDegreeOfParallelism = j;
sw.Reset();
sw.Start();
this.ThreadFnc(options);
sw.Stop();
this.StatOfThreads[j-1] = Convert.ToInt32(sw.ElapsedMilliseconds);
}
}
public void SetSize(int size) { this.Size = size; }
public void SetNumberOfThreads(int threads) { this.ThreadsNumber = threads; }
public int GetSize() { return this.Size; }
public int GetNumberOfThreads() { return this.ThreadsNumber; }
public int[] GetStats() { return this.StatOfThreads; }
}
}