Optimising and why openmp is much slower than sequential way?

c, matrix, openmp, performance, vector

Solution

Because when OpenMP distributes the work among threads there is a lot of administration/synchronisation going on to ensure the values in your shared matrix and vector are not corrupted somehow. Even though they are read-only: humans see that easily, your compiler may not.

Things to try out for pedagogic reasons:

0) What happens if `matrix` and `vector` are not `shared`?

1) Parallelize the inner "j-loop" first, keep the outer "i-loop" serial. See what happens.

2) Do not collect the sum in `result[i]`, but in a variable `temp` and assign its contents to `result[i]` only after the inner loop is finished to avoid repeated index lookups. Don't forget to init `temp` to 0 before the inner loop starts.

Problem

I am a newbie in programming with OpenMp. I wrote a simple c program to multiply matrix with a vector. Unfortunately, by comparing executing time I found that the OpenMP is much slower than the Sequential way. Here is my code (Here the matrix is N*N int, vector is N int, result is N long long): ``` #pragma omp parallel for private(i,j) shared(matrix,vector,result,m_size) for(i=0;i<m_size;i++) { for(j=0;j<m_size;j++) { result[i]+=matrix[i][j]*vector[j]; } } ``` And this is the code for sequential way: ``` for (i=0;i<m_size;i++) for(j=0;j<m_size;j++) result[i] += matrix[i][j] * vector[j]; ``` When I tried these two implementations with a 999x999 matrix and a 999 vector, the execution time is: Sequential: 5439 ms Parallel: 11120 ms I really cannot understand why OpenMP is much slower than sequential algo (over 2 times slower!) Anyone who can solve my problem?

Original source