以下是我写的一个单线程的固定3*3矩阵相称,如果要把它改成多线程矩阵相乘,该怎么做,没多少分望高手帮忙!谢谢了先。
using System;
using System.Threading;
class Program
{ private static bool done = false;
private static int MatrixrowA = 0;
private static int MatrixcolA = 0;
private static int MatrixcolB = 1;
private static int MatrixrowB = 0;
static void Main(string[] args)
{
while (!done)
{
int rowA = 2;
int colA = 2;
int rowB = 2;
int colB = 2;
//Console.Console.WriteLine("rowA=" + rowA + "\t" + "colA=" + colA);
MatrixrowA = rowA;
MatrixcolA = colA;
MatrixcolB = colB;
MatrixrowB = rowB; //whether colA = rowB
if (colA == rowB)
{
//Create Matrix A and Matrix B
int[,] aryA = new int[rowA, colA];
int[,] aryB = new int[rowB, colB];
//Create Matrix C(the result)
int[,] aryC = new int[rowA, colB];
//Random numbers
Random random = new Random();
//fill random numbers to A
Console.WriteLine("Matrix A:");
for (int i = 0; i < rowA; i++)
{
for (int k = 0; k < colA; k++)
{
aryA[i, k] = random.Next(1, 10);
Console.Write(aryA[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
//fill random numbers to B
Console.WriteLine("Matrix B:");
for (int i = 0; i < rowB; i++)
{
for (int k = 0; k < colB; k++)
{
aryB[i, k] = random.Next(1, 10);
Console.Write(aryB[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
int x = 0;
//fill the result to C
Console.WriteLine("Matrix C:"); for (int i = 0; i < rowA; i++)
{
for (int k = 0; k < colB; k++)
{
for (int j = 0; j < colA; j++)
{
x += aryA[i, j] * aryB[j, k];
}
aryC[i, k] = x;
x = 0;
Console.Write(aryC[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is:"); for (int i = 0; i < aryC.GetLength(0); i++)
{
for (int j = 0; j < aryC.GetLength(1); j++)
{
Console.Write("{0} ", aryC[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
}
using System;
using System.Threading;
class Program
{ private static bool done = false;
private static int MatrixrowA = 0;
private static int MatrixcolA = 0;
private static int MatrixcolB = 1;
private static int MatrixrowB = 0;
static void Main(string[] args)
{
while (!done)
{
int rowA = 2;
int colA = 2;
int rowB = 2;
int colB = 2;
//Console.Console.WriteLine("rowA=" + rowA + "\t" + "colA=" + colA);
MatrixrowA = rowA;
MatrixcolA = colA;
MatrixcolB = colB;
MatrixrowB = rowB; //whether colA = rowB
if (colA == rowB)
{
//Create Matrix A and Matrix B
int[,] aryA = new int[rowA, colA];
int[,] aryB = new int[rowB, colB];
//Create Matrix C(the result)
int[,] aryC = new int[rowA, colB];
//Random numbers
Random random = new Random();
//fill random numbers to A
Console.WriteLine("Matrix A:");
for (int i = 0; i < rowA; i++)
{
for (int k = 0; k < colA; k++)
{
aryA[i, k] = random.Next(1, 10);
Console.Write(aryA[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
//fill random numbers to B
Console.WriteLine("Matrix B:");
for (int i = 0; i < rowB; i++)
{
for (int k = 0; k < colB; k++)
{
aryB[i, k] = random.Next(1, 10);
Console.Write(aryB[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
int x = 0;
//fill the result to C
Console.WriteLine("Matrix C:"); for (int i = 0; i < rowA; i++)
{
for (int k = 0; k < colB; k++)
{
for (int j = 0; j < colA; j++)
{
x += aryA[i, j] * aryB[j, k];
}
aryC[i, k] = x;
x = 0;
Console.Write(aryC[i, k]);
Console.Write("\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is:"); for (int i = 0; i < aryC.GetLength(0); i++)
{
for (int j = 0; j < aryC.GetLength(1); j++)
{
Console.Write("{0} ", aryC[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
}
如果确实需要,多线程的实现方式很多,具体的需求决定实现方式,希望您能多提供更详细的需求信息。除非用到硬件(如GPU)加速,多线程对性能不会有积极作用。3*3矩阵的话,GPU可轻松完成,而且有现成的运算函数,但需要调用相关的SDK(像DirectX, OpenCL什么的)。多线程的另一的作用是,通过把花费时间的运算放在后台线程,来保证前台界面的相应速度或者监控运算进度。这种多线程如果不受边界效应的影响的话(楼主目前的代码好像就不会,不知完整代码怎么样),通过异步调用很容易实现.当然监控运算进度的话还需要额外的代码.所以关键是多线程的需求和动机.