0

I need to perform a Matrix multiplication of two matrices with dimension 10,000 x 10,000. I am using simple array multiplication, and it takes more than 2 hours to complete the calculation. I need to reduce the times to complete the multiplication.

 Double[,] array1 = new double [10000, 10000];
 Double[,] array2 = new double [10000, 10000];
 Random randNum = new Random();

 for (int i = 0; i < array1.GetLength(0); i++)
 {
     for (int j = 0; j < array1.GetLength(1); j++)
     {
         array1[i, j] = randNum.Next(1, 10);
     }
 }

 for (int i = 0; i < array2.GetLength(0); i++)
 {
     for (int j = 0; j < array2.GetLength(1); j++)
     {
         array2[i, j] = randNum.Next(1, 10);
     }
 }

Double [,] mt = mMat(array1,array2);

public static double[,] mMat(double[,] A, double[,] B)
{

    int aRows = A.GetLength(0);
    int aColumns = A.GetLength(1);
    int bRows = B.GetLength(0);
    int bColumns = B.GetLength(1);

    if (aColumns != bRows)
    {

        throw new ArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
    }

    double[,] C = new double[aRows, bColumns];

    for (int i = 0; i < aRows; i++)
    { // aRow
        for (int j = 0; j < bColumns; j++)
        { // bColumn
            for (int k = 0; k < aColumns; k++)
            { // aColumn
                C[i, j] += A[i, k] * B[k, j];
            }
        }
    }

    return C;
}

I am newbie in programming world and need to do task to perform large matrix multiplication

Harith
  • 35
  • 4
  • 2
    Out of the top of my head you'll probably be best served to rewrite it for Multithreading. – Vulpex Mar 04 '21 at 04:57
  • 1
    There are several vector algebra libraries out there, see e.g. https://stackoverflow.com/questions/392857/c-sharp-linear-algebra-library – Klaus Gütter Mar 04 '21 at 04:59
  • 1
    you can check this https://www.geeksforgeeks.org/strassens-matrix-multiplication/ – vivek nuna Mar 04 '21 at 05:01
  • 1
    @Vulpex multithreading won't help. Even if you can run in 8 threads then the time will still be ~15 minutes. This is the worst matrix multiplication algorithm which takes O(n³) time. Besides it's extremely cache-unfriendly due to the vertical memory access. The current best algorithm is O(n^2.3728596) will run ~323 times faster than this. And then use something more cache friendly, run in parallel and use SIMD or GPU to accelerate and it'll run thousands of times faster (or more). See [Why is MATLAB so fast in matrix multiplication?](https://stackoverflow.com/q/6058139/995714) – phuclv Mar 04 '21 at 10:37
  • 1
    [how to optimize and speed up the multiplication of matrix](https://stackoverflow.com/q/55245000/995714) – phuclv Mar 04 '21 at 10:39

0 Answers0