Parallel matrix multiplication in C# using task parallel library

static double[][] MatrixMultiplication(double[][] matrixOne, double[][] matrixTwo)
{
  //aRows = rows in matrixOne
  //bCols = columns in matrixTwo  
  double[][] result = CreateMatrix(aRows, bCols);
  Parallel.For(0, aRows, i =>
	{
	  for (int j = 0; j < bCols; ++j)
		for (int k = 0; k < aCols; ++k)
		  result[i][j] += matrixOne[i][k] * matrixTwo[k][j];
	}
  );
  return result;
}
static double[][] CreateMatrix(int rows, int cols)
{
  double[][] result = new double[rows][];
  for (int i = 0; i < rows; ++i)
	result[i] = new double[cols]; 
  return result;
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.