我有一个任务 - 编写多线程矩阵乘法。每个向量乘积必须在新线程中计算。(如果我们有矩阵n乘m和m乘k,我们必须有n个k个线程)。另外,我必须显示结果矩阵元素的计算顺序。我写了代码并得到了奇怪的结果 - 计算的顺序几乎是顺序的。但我计算新线程中的每个元素,所以我必须得到结果矩阵元素的随机计算顺序。怎么了?这是我的代码。
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();
}
}
}
答案 0 :(得分:2)
您所描述的内容是正常的 - 请参阅今天早些时候的this post,其中演示了单独线程中的进程并不总是按预期顺序运行。他们可能会做很多或大部分时间,但是你会得到一些意想不到的行为。
计算是否需要按特定顺序进行,还是只需要能够看到它们发生的顺序?
如果您正在开始新线程,则无法控制序列。 (您已经看过了。)您也无法捕获它们的完成顺序,因为完成计算并记录结果(控制台或任何其他输出)不是原子操作。
可能会发生这种情况:
当操作必须按特定顺序发生时,多线程并不是很好。
您可以在计算完成后将计算结果插入ConcurrentQueue
,并且序列 正确。
答案 1 :(得分:0)
首先,您应该使代码尽可能简单明了。
您应该计算2个 而不是计算i < a.Row * b.Column
:
for (int i = 0; i < a.Row; i++)
for (int j = 0; j < b.Column; j++)
{
int tempi = i;
int tempj = j;
Thread thread = new Thread(() => VectorMult(tempi, tempj, a, b, result));
thread.Start();
threads.Add(thread);
}
然后,在VectorMult(int tmpi, int tmpj, Matrix a, Matrix b, Matrix result)
函数中,不用计算j = tempi % b.Column
的内容,而是将参数 tempj 给出。 i 和 j 变为:
int i = tmpi;
int j = tmpj;
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];
最后,不要忘记,在使用共享资源和线程时,事情可能会变得很疯狂。确保使用互斥锁。
这是我的完整代码,
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;
public static Mutex mutex = new Mutex();
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; i++)
for (int j = 0; j < b.Column; j++)
{
int tempi = i;
int tempj = j;
Thread thread = new Thread(() => VectorMult(tempi, tempj, a, b, result));
thread.Start();
threads.Add(thread);
}
foreach (Thread t in threads)
t.Join();
return result;
}
public static void VectorMult(int tmpi, int tmpj, Matrix a, Matrix b, Matrix result)
{
mutex.WaitOne();
int i = tmpi;
int j = tmpj;
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];
mutex.ReleaseMutex();
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("n=");
int n = int.Parse(Console.ReadLine());
Console.Write("m=");
int m = int.Parse(Console.ReadLine());
Console.Write("k=");
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();
Console.ReadLine();
}
}
}
祝你一切顺利! :)