# Using OpenMP in Fortran for common array operations

In Fortran, given arrays A(1000000) and B(1000000), and to compute the sum you simply write:

C = A + B


However, when I want to implement OpenMP, I have to write an explicit loop:

!$OMP PARALLEL DO DO I = 1, 1000000 C(I) = A(I) + B(I) END DO !$OMP END PARALLEL DO


Is there a better way to do this? I know there is -parallel compiler flag but it gives less performance.

• Which compilers do you have access to? I think that -parallel is a GCC/gfortran option only. – Bill Barth Aug 8 '14 at 15:30
• I use ifort intel compiler. – Michael Aug 8 '14 at 15:30
• Anyways, a flag isn't an option for me. The real code is huge and for some reason the -parallel flag gives almost x2 slowdown in my case. – Michael Aug 8 '14 at 15:32
• I stand corrected on which compiler supports that option. – Bill Barth Aug 8 '14 at 15:33
• @Michael -parallel turns on autoparallelization in ifort. Autoparallelization requires the compiler to prove beyond a shadow of a doubt that this is safe. That's hard, because the Fortran language standard is a few hundred pages long. This feature also causes parallelization of loops that might not benefit from it. In short, it's good to parallelize yourself explicitly. – Jeff Hammond Feb 8 '18 at 13:54

For the record, you might try putting and OpenMP worksharing region around your array operation syntax:

!\$omp workshare
C=A+B


Don't forget to build with OpenMP enabled (-openmp for the Intel compilers) and to set OMP_NUM_THREADS.

• Thanks, I was suggested to avoid WORKSHARE and I found out why recently. ifort translates it to SINGLE, I don't know why, but it definitely showed no speedup. So I tried using gfortran instead, and it worked fine. – Michael Aug 8 '14 at 19:27
• What ifort version are you using? – Bill Barth Aug 8 '14 at 19:39
• I'm using ifort 14.0.2 – Michael Aug 8 '14 at 22:43
• Array expressions like that were only parallelized by ifort starting in version 15 (software.intel.com/en-us/articles/…). – Jeff Hammond Feb 8 '18 at 13:55

WORKSHARE is much harder to implement than explicit loop parallelism. Compilers have been known to do correct but useless (aka "no-op") implementations of WORKSHARE that map it to SINGLE. This is correct but obviously provides no performance benefit.
When compilers support WORKSHARE, they may do so selectively, e.g. Intel Fortran 15, which means that depending on it may lead to mixed results.