6

Hey there, I have a very long list of shared variables in OpenMP so I have to split lines in fortran and use the "&"-syntax to make sure the lines stick together!

Something like that:

!$OMP PARALLEL DEFAULT(private) SHARED(vars....,
     & more_vars...,
     & more_vars...
     & )

That gives me errors when compiling without OpenMP, since only the first like is recognized as a comment! The problem now is that I can't add a "!" in front of those lines with a "&" in front to support compiling without OpenMP:

!$OMP PARALLEL DEFAULT(private) SHARED(vars....,
!     & more_vars...,
!     & more_vars...
!     & )

because than it doesn't compile with OpenMP anymore... But I want to support both sorts of compiling in just one code... Any advices on how to do it?

tim
  • 9,896
  • 20
  • 81
  • 137

2 Answers2

9

You are not using the correct syntax. If you look at the OpenMP V3.0 specification, section 2.1.2 Free Source Form Directives, you will see the following:

The sentinel can appear in any column as long as it is preceded only by white space (spaces and tab characters). It must appear as a single word with no intervening character. Fortran free form line length, white space, and continuation rules apply to the directive line. Initial directive lines must have a space after the sentinel. Continued directive lines must have an ampersand as the last nonblank character on the line, prior to any comment placed inside the directive. Continuation directive lines can have an ampersand after the directive sentinel with optional white space before and after the ampersand.

So the correct form should be:

!$OMP PARALLEL DEFAULT(private) SHARED(vars...., &
!$OMP& more_vars..., &
!$OMP& more_vars...  &
!$OMP& )

For fixed form, it is the same type of thing. You start each line with the OMP sentinel and make sure continuation lines have a non-blank and non-zero character in column 6.

ejd
  • 1,717
  • 1
  • 11
  • 10
  • Sorry, I was mistaken, that doesn't work for me: Syntax error, found '&' when expecting one of: / – tim Apr 13 '11 at 17:07
  • 1
    What compiler are you using? Also, my example was for free form, though your example really looks more like you are using fixed form. If fixed form, then you just need to have the sentinel and column 6 non-blank (i.e., c$omp+ shared_var, shared_var,) on the continuation lines. – ejd Apr 13 '11 at 17:57
  • Yeah I just left out the final ampersands (the ones at the end of each line) in the code out of your post above and that worked out well! – tim Apr 13 '11 at 19:10
0

Okay guys... I found out the solution: The loop-identifier (I mean i in the following code: do i=1,end) has to be shared and as I am using DEFAULT(private) I had to write this into the list of shared vars :) Hope this helps somebody someday :)

tim
  • 9,896
  • 20
  • 81
  • 137
  • 1
    Since you haven't shown how you are using this loop, it is possible what you are saying is correct. However, in general, if you have a loop within a parallel region you want the loop index to be private. In fact, by default, OpenMP will make the loop index private so that you do not have a race condition. This comment also seems to be totally unrelated to the original question, so I am not sure how it is the best answer. – ejd Apr 19 '11 at 17:15
  • Oh sorry, yeah you were right, sorry about this; I originally wanted to post this to my question "OpenMP in Fortran: Changes to data types?" but just messed it up, sorry! However, I feel that the loop index can't be private, are you sure about your statement? I can't see why the program works immediately when setting it to shared. I just can't post the code since it's a little bit too long and I wouldn't know what to strip out but as it works now, I probably needn't too – tim Apr 19 '11 at 17:33