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:

DO I = 1, 1000000
C(I) = A(I) + B(I)

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

  • $\begingroup$ Which compilers do you have access to? I think that -parallel is a GCC/gfortran option only. $\endgroup$
    – Bill Barth
    Aug 8 '14 at 15:30
  • $\begingroup$ I use ifort intel compiler. $\endgroup$
    – Michael
    Aug 8 '14 at 15:30
  • $\begingroup$ 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. $\endgroup$
    – Michael
    Aug 8 '14 at 15:32
  • 1
    $\begingroup$ I stand corrected on which compiler supports that option. $\endgroup$
    – Bill Barth
    Aug 8 '14 at 15:33
  • $\begingroup$ @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. $\endgroup$ Feb 8 '18 at 13:54

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

!$omp workshare

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

  • $\begingroup$ 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. $\endgroup$
    – Michael
    Aug 8 '14 at 19:27
  • $\begingroup$ What ifort version are you using? $\endgroup$
    – Bill Barth
    Aug 8 '14 at 19:39
  • $\begingroup$ I'm using ifort 14.0.2 $\endgroup$
    – Michael
    Aug 8 '14 at 22:43
  • $\begingroup$ Array expressions like that were only parallelized by ifort starting in version 15 (software.intel.com/en-us/articles/…). $\endgroup$ Feb 8 '18 at 13:55

Short answer

No, there is not a better way to do this.

Long answer

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.

Even when compilers do a good job, in parallel computing in general, you will find that explicit parallelization wins, because the user is required to make good design choices up front and compilers, which are much less intelligent than humans, don't have to make important decisions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.