Optimising and why openmp is much slower than sequential way?

16,905

Solution 1

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.

Solution 2

Your code partially suffers from the so-called false sharing, typical for all cache-coherent systems. In short, many elements of the result[] array fit in the same cache line. When thread i writes to result[i] as a result of the += operator, the cache line holding that part of result[] becomes dirty. The cache coherency protocol then invalidates all copies of that cache line in the other cores and they have to refresh their copy from the upper level cache or from the main memory. As result is an array of long long, then one cache line (64 bytes on x86) holds 8 elements and besides result[i] there are 7 other array elements in the same cache line. Therefore it is possible that two "neighbouring" threads will constantly fight for ownership of the cache line (assuming that each thread runs on a separate core).

To mitigate false sharing in your case, the easiest thing to do is to ensure that each thread gets an iteration block, whose size is divisible by the number of elements in the cache line. For example you can apply the schedule(static,something*8) where something should be big enough so that the iteration space is not fragmented into too many pieces, but in the same time it should be small enough so that each thread gets a block. E.g. for m_size equal to 999 and 4 threads you would apply the schedule(static,256) clause to the parallel for construct.

Another partial reason for the code to run slower might be that when OpenMP is enabled, the compiler might become reluctant to apply some code optimisations when shared variables are being assigned to. OpenMP provides for the so-called relaxed memory model where it is allowed that the local memory view of a shared variable in each threads is different and the flush construct is provided in order to synchronise the views. But compilers usually see shared variables as being implicitly volatile if they cannot prove that other threads would not need to access desynchronised shared variables. You case is one of those, since result[i] is only assigned to and the value of result[i] is never used by other threads. In the serial case the compiler would most likely create a temporary variable to hold the result from the inner loop and would only assign to result[i] once the inner loop has finished. In the parallel case it might decide that this would create a temporary desynchronised view of result[i] in the other threads and hence decide not to apply the optimisation. Just for the record, GCC 4.7.1 with -O3 -ftree-vectorize does the temporary variable trick with both OpenMP enabled and not.

Share:
16,905
Alex Zhou
Author by

Alex Zhou

Updated on June 12, 2022

Comments

  • Alex Zhou
    Alex Zhou almost 2 years

    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?

  • Alex Zhou
    Alex Zhou about 11 years
    Thanks for your reply! although the first two solutions were not so useful, the last did reduce the execution time to 6838ms
  • András Aszódi
    András Aszódi about 11 years
    Glad I could help. Could you maybe share the timings? That could be instructive to others. And maybe click on the upward arrow left to my reply :-)
  • Hristo Iliev
    Hristo Iliev about 11 years
    This is a common misconception. Most OpenMP implementations do absolutely nothing in order to protect shared variables from possible data races. It is the programmer's job to ensure that no races occur by explicitly adding synchronisation primitives.
  • Admin
    Admin about 11 years
    That's a very interesting comment @Hristo! I normally would loop over m_size/256 and the jump to to 256*i or something like that instead of using schedule(static,256). I'm going to try some of your suggestions this week.
  • Admin
    Admin about 11 years
    I posted some code as an answer trying your suggestion. It makes no difference to me to set the block size. Setting the block size is one of the areas i'm least familiar with with OpenMP. Maybe it's not safe to assume that OpenMP will distribute the threads in a way that avoids false sharing in general and so it's better to set the block size yourself?
  • Hristo Iliev
    Hristo Iliev about 11 years
    As with many other cases - it depends. Some scenarios benefit from setting a specific block size. In other cases the default one suffices. In this particular case it is impossible to say what is the reason for the slowdown with so little information provided by the OP.