Reputation: 11
#region Parallel_Loop
static void MultiplyMatricesParallel(int[,] matA, int[,] matB, int[,] result)
{
int matACols = matA.GetLength(1);
int matBCols = matB.GetLength(1);
int matARows = matA.GetLength(0);
int count = 0;
// A basic matrix multiplication.
// Parallelize the outer loop to partition the source array by rows.
Parallel.For(0, matARows, i =>
{
for (int j = 0; j < matBCols; j++)
{
int temp = 0;
int numThreads = (commonFeatures.resultMatrix.GetLength(0)* commonFeatures.resultMatrix.GetLength(1));
int toProcess = numThreads;
var resetEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
new WaitCallback(delegate (object state) {
rowColCall(matACols, temp, matA, matB, i, j);
if (Interlocked.Decrement(ref toProcess) == 0) resetEvent.Set();
}), null);
resetEvent.WaitOne();
}
}); // Parallel.For
}
#endregion
public static int rowCol(int matACols, int temp, int [,] matA, int [,] matB, int i, int j) {
for (int k = 0; k < matACols; k++)
{
temp += matA[i, k] * matB[k, j];
}
return temp;
}
public static void rowColCall(int matACols, int temp, int[,] matA, int[,] matB, int i, int j) {
int a = rowCol(matACols, temp, matA, matB, i, j);
Console.WriteLine(i + "," + j + " = " + a);
Console.ReadLine();
}
Hi,
I'm new on threadpool usage and doing matrix multiplication on c# by using thread pool(ThreadPool.QueueUserWorkItem.) on row*col multiplication. When I execute my code with Matrices {1 2 3 4; 0 0 2 4} and {1 4, 1 5, 3 6, 4,7} it returns me as output:
0,0 = 28 1,0 = 22 The thread 0x179c has exited with code 0 (0x0). The thread 0x1fd4 has exited with code 0 (0x0).
So it calculates first two threads and waits in infinite loop; I don't know why.
Upvotes: 0
Views: 240