51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
|
namespace 5LABA
|
|||
|
{
|
|||
|
public class Alg2
|
|||
|
{
|
|||
|
public int[,] Begin(int[,] matrix1, int[,] matrix2, int numThreads)
|
|||
|
{
|
|||
|
int rowsA = matrix1.GetLength(0);
|
|||
|
int columnsA = matrix1.GetLength(1);
|
|||
|
int rowsB = matrix2.GetLength(0);
|
|||
|
int columnsB = matrix2.GetLength(1);
|
|||
|
|
|||
|
int[,] resultMatrix = new int[rowsA, columnsB];
|
|||
|
|
|||
|
int rowsPerThread = rowsA / numThreads;
|
|||
|
|
|||
|
Thread[] threads = new Thread[numThreads];
|
|||
|
|
|||
|
for (int i = 0; i < numThreads; i++)
|
|||
|
{
|
|||
|
int startRow = i * rowsPerThread;
|
|||
|
int endRow = (i == numThreads - 1) ? rowsA : startRow + rowsPerThread;
|
|||
|
threads[i] = new Thread(() => MultiplyRows(startRow, endRow, matrix1, matrix2, resultMatrix));
|
|||
|
threads[i].Start();
|
|||
|
}
|
|||
|
|
|||
|
foreach (Thread thread in threads)
|
|||
|
{
|
|||
|
thread.Join();
|
|||
|
}
|
|||
|
|
|||
|
return resultMatrix;
|
|||
|
}
|
|||
|
|
|||
|
static void MultiplyRows(int startRow, int endRow, int[,] matrixA, int[,] matrixB, int[,] resultMatrix)
|
|||
|
{
|
|||
|
for (int i = startRow; i < endRow; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < matrixB.GetLength(1); j++)
|
|||
|
{
|
|||
|
int sum = 0;
|
|||
|
for (int k = 0; k < matrixA.GetLength(1); k++)
|
|||
|
{
|
|||
|
sum += matrixA[i, k] * matrixB[k, j];
|
|||
|
}
|
|||
|
resultMatrix[i, j] = sum;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|