开发者

Matrix Multiplication?

开发者 https://www.devze.com 2023-03-06 22:34 出处:网络
What i am trying here is multiplication of two 500x500 matrices of doubles. And i am getting null reference exception !

What i am trying here is multiplication of two 500x500 matrices of doubles. And i am getting null reference exception !

Could u take a look into it

void Topt(double[][] A, double[][] B, double[][] C) {
    var source = Enumerable.Range(0, N);
    var pquery = from num in source.AsParallel()
             开发者_JAVA技巧       select num;
    pquery.ForAll((e) => Popt(A, B, C, e));
}

void Popt(double[][] A, double[][] B, double[][] C, int i) {
    double[] iRowA = A[i];
    double[] iRowC = C[i];
    for (int k = 0; k < N; k++) {
        double[] kRowB = B[k];
        double ikA = iRowA[k];
        for (int j = 0; j < N; j++) {
            iRowC[j] += ikA * kRowB[j];
        }
    }
}

Thanks in advance


Since your nullpointer problem was already solved, why not a small performance tip ;) One thing you could try would be a cache oblivious algorithm. For 2k x 2k matrices I get 24.7sec for the recursive cache oblivious variant and 50.26s with the trivial iterative method on a e8400 @3ghz single threaded in C (both could be further optimized obviously, some better arguments for the compiler and making sure SSE is used etc.). Though 500x500 is quite small so you'd have to try if that gives you noticeable improvements.

The recursive one is easier to make multithreaded usually though.

Oh but the most important thing since you're writing in c#: Read this - it's for java, but the same should apply for any modern JIT..

0

精彩评论

暂无评论...
验证码 换一张
取 消