I have a task - write multithreading matrix multiplication. Each vector product must be calculated in new thread.(If we have matrices n by m and m by k we must have n by k threads). Also I must show order of calculation for elements of result matrix. I wrote code and got strange result - order of calculation is almost sequentially. But I calculate each element in new thread, so I must get random order of calculation for elements of the result matrix. What is wrong? This is my code.
using System;
using System.Threading;
using System.Collections.Generic;
namespace MatrixMultiplication
{
class Matrix
{
public int Row{get; set;}
public int Column { get; set;}
double[,] arr;
Matrix() { }
public Matrix(int row,int column)
{
Row = row;
Column = column;
arr = new double[row, column];
}
public double[] GetColumn(int i)
{
double[] res=new double[Row];
for (int j = 0; j < Row; j++)
res[j] = arr[j, i];
return res;
}
public double[] GetRow(int i)
{
double[] res = new double[Column];
for (int j = 0; j < Column; j++)
res[j] = arr[i, j];
return res;
}
public double this[int i,int j]
{
get { return arr[i, j]; }
set { arr[i, j] = value; }
}
public Matrix RandomValues()
{
Random rnd=new Random();
for (int i = 0; i < Row; i++)
for (int j = 0; j < Column; j++)
arr[i, j] =rnd.Next(10);
return this;
}
public void Print()
{
for(int i=0;i<Row;i++){
for (int j = 0; j < Column; j++)
Console.Write(arr[i,j]+" ");
Console.WriteLine();
}
}
public static Matrix operator*(Matrix a, Matrix b)
{
Matrix result=new Matrix(a.Row,b.Column);
List<Thread> threads = new List<Thread>();
for (int i = 0; i <a.Row*b.Column;i++ )
{
int tempi = i;
Thread thread = new Thread(()=>VectorMult(tempi, a, b, result));
thread.Start();
threads.Add(thread);
}
foreach (Thread t in threads)
t.Join();
return result;
}
public static void VectorMult(int tmp, Matrix a, Matrix b,Matrix result){
int i = tmp / b.Column;
int j = tmp % b.Column;
double[] x = a.GetRow(i);
double[] y = b.GetColumn(j);
for (int k = 0; k < x.Length; k++)
result[i, j] += x[k] * y[k];
Console.WriteLine("Calculate element{0}{1}", i, j);
}
}
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
int k = int.Parse(Console.ReadLine());
Matrix A = new Matrix(n,m).RandomValues();
Matrix B = new Matrix(m,k).RandomValues();
A.Print();
Console.WriteLine(new String('-',20));
B.Print();
Console.WriteLine(new String('-', 20));
Matrix C = A * B;
C.Print();
}
}
}