简体   繁体   中英

c# 10000 x 10000 matrix multiplication

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

I recently did the same thing for a C# benchmark, and here are a few tips to make it run faster. The C# version runs in about 1 minute and 15 seconds on an 8 core laptop.

  1. Calculate the transpose of B before beginning the matrix multiplication because accessing the matrix in row order is MUCH faster than column order. Scott Meyers did a talk about CPU caches that explains why row order traversal is faster.

  2. Make the outer loop a Parallel.For loop. The link also has some examples that use matrix multiplication.

  3. Use the Vector class to take advantage of SIMD.

  4. Consider using float instead of double unless you really need the extra precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM