0

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()
                    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

Synxmax
  • 2,226
  • 1
  • 22
  • 38
  • at what line you get it? – Andrey May 18 '11 at 10:07
  • 2
    First get rid of the parallel side of things to simplify it. Do you still have a problem then? Where are you getting the NullReferenceException? A short but complete program demonstrating the problem would be helpful. – Jon Skeet May 18 '11 at 10:07
  • @Jon Skeet it seems there is a problem with array c , i am trying to get best optimize result under 200ms , so i need to , i am done with c++ but new to c# – Synxmax May 18 '11 at 10:11
  • 3
    @Synxmax: Getting it under 200ms for a mere 500x500 array should be absolutely fine on any reasonable processor... but get it *working* before you try to optimize. – Jon Skeet May 18 '11 at 10:12
  • @Jon Skeet :D sure thanks another thing , do i need to allocating c array !? i am saying due to my c++ knowledge ! – Synxmax May 18 '11 at 10:15
  • @Synxmax: Yes, you need to allocate the arrays to start with. That's why I'm recommending that you should us a *complete* program rather than just these methods. – Jon Skeet May 18 '11 at 10:22

1 Answers1

1

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..

Community
  • 1
  • 1
Voo
  • 29,040
  • 11
  • 82
  • 156