简体   繁体   English

c# 10000 x 10000 矩阵乘法

[英]c# 10000 x 10000 matrix multiplication

I need to perform a Matrix multiplication of two matrices with dimension 10,000 x 10,000.我需要对两个维度为 10,000 x 10,000 的矩阵执行矩阵乘法。 I am using simple array multiplication, and it takes more than 2 hours to complete the calculation.我用的是简单的数组乘法,用了2个多小时才算完。 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.我最近为 C# 基准测试做了同样的事情,这里有一些技巧可以让它运行得更快。 The C# version runs in about 1 minute and 15 seconds on an 8 core laptop. C# 版本在 8 核笔记本电脑上运行时间约为 1 分 15 秒。

  1. Calculate the transpose of B before beginning the matrix multiplication because accessing the matrix in row order is MUCH faster than column order.在开始矩阵乘法之前计算 B 的转置,因为按行顺序访问矩阵比按列顺序访问矩阵快得多。 Scott Meyers did a talk about CPU caches that explains why row order traversal is faster. Scott Meyers做了一个关于 CPU 缓存的演讲,解释了为什么行顺序遍历更快。

  2. Make the outer loop a Parallel.For loop.使外部循环成为Parallel.For循环。 The link also has some examples that use matrix multiplication.该链接还有一些使用矩阵乘法的示例。

  3. Use the Vector class to take advantage of SIMD.使用Vector类来利用 SIMD。

  4. Consider using float instead of double unless you really need the extra precision.除非你真的需要额外的精度,否则请考虑使用 float 而不是 double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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