using System.Diagnostics;
using System.Threading.Tasks;
public static void Main()
int commonCount = rd.Next(minDim, maxDim);
var intMatrix1 = GenerateMatrix(rd.Next(minDim,maxDim), commonCount);
var intMatrix2 = GenerateMatrix(commonCount, rd.Next(minDim,maxDim));
var sw = new Stopwatch();
var result = intMatrix1.ParallelMultiply(intMatrix2);
Console.WriteLine($"Matrix parallel multiplication [{intMatrix1.RowCount}, {intMatrix1.ColumnCount}] x [{intMatrix2.RowCount}, {intMatrix2.ColumnCount}] tooks: {sw.Elapsed}");
result = intMatrix1.Multiply(intMatrix2);
Console.WriteLine($"Matrix multiplication [{intMatrix1.RowCount}, {intMatrix1.ColumnCount}] x [{intMatrix2.RowCount}, {intMatrix2.ColumnCount}] tooks: {sw.Elapsed}");
public static IntMatrix GenerateMatrix(int rowCount, int columnCount)
var result = new IntMatrix(rowCount, columnCount);
for(int i=0; i<rowCount; i++)
for(int j=0; j<columnCount; j++)
result[i, j] = rd.Next(100)+1;
public int RowCount {get; private set;}
public int ColumnCount {get; private set;}
public IntMatrix(int[,] items)
int row = items.GetLength(0);
int column = items.GetLength(1);
items = new int[row, column];
for(int c=0; c<column; c++)
this.items[r,c] = items[r,c];
public IntMatrix(int rowCount, int columnCount)
items = new int[rowCount, columnCount];
ColumnCount = columnCount;
public int this[int row, int column]
get {return items[row, column];}
set {items[row, column] = value;}
public IntMatrix Multiply(IntMatrix intMatrix)
if(intMatrix.RowCount != ColumnCount)
throw new ArgumentException();
var result = new IntMatrix(RowCount, intMatrix.ColumnCount);
for(int r=0; r<RowCount; r++)
for(int c=0; c<intMatrix.ColumnCount; c++)
for(int i=0; i<ColumnCount; i++)
total += this[r, i] * intMatrix[i, c];
public IntMatrix ParallelMultiply(IntMatrix intMatrix)
if(intMatrix.RowCount != ColumnCount)
throw new ArgumentException();
var result = new IntMatrix(RowCount, intMatrix.ColumnCount);
Parallel.For(0, RowCount, r=> {
for(int c=0; c<intMatrix.ColumnCount; c++)
for(int i=0; i<ColumnCount; i++)
total += this[r, i] * intMatrix[i, c];
public override string ToString()
var sb = new StringBuilder();
for(int i=0; i<RowCount; i++)
for(int j=0; j<ColumnCount; j++)