Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open MP if OpenMP:...else

Problem: I have a some code that myself and a few others have been writing, I took the code and made it use mpi and openmp with great results (helps that I am running it on a Blue Gene/Q).

One thing I am not a fan of is that now I cannot compile the code without the -openmp directive because to get the speedup I needed I used reduction variables. Example:

!$OMP parallel do schedule(DYNAMIC, 4) reduction(min:min_val)
....
    min_val = some_expression(i)
....
!$OMP end parallel do
result = sqrt(min_val)

I am looking for something like:

!$OMP if OMP:
!$OMP min_val = some_expression(i)
!$OMP else:
if ( min_val .gt. some_expression(i) ) min_val = some_expression(i)
!$OMP end else

Anybody know of something like this? Notice that without -openmp the !$OMP lines are ignored and the code runs normally with the correct, er same, answer.

Thanks,

(Yes it is FORTRAN code, but its almost identical to C and C++)

like image 990
ShaBANG Avatar asked Jun 25 '26 20:06

ShaBANG


2 Answers

To your exact question:

!$ whatever_statement

will use that statement only when compiled with OpenMP.


Otherwise, in your specific case, can't you just use:

!$OMP parallel do schedule(DYNAMIC, 4) reduction(min:min_val)
....
    min_val = min(min_val, some_expression(i))
....
!$OMP end parallel do

result = sqrt(min_val)

?

I'm using this normally with and without -openmp quite often.

like image 130
Vladimir F Героям слава Avatar answered Jun 28 '26 18:06

Vladimir F Героям слава


If you are willing to use pre-processed FORTRAN source file, you can always rely on the macro _OPENMP to be defined when using OpenMP. The simplest example is:

program pippo

#ifdef _OPENMP
print *, "OpenMP program"
#else
print *, "Non-OpenMP program"
#endif

end program pippo

Compiled with:

gfortran -fopenmp main.F90

the program will give the following output:

OpenMP program

If you are unwilling to use pre-processed source files, then you can set a variable using FORTRAN conditional compilation sentinel:

program pippo

  implicit none

  logical :: use_openmp = .false.

  !$ use_openmp = .true.
  !$ print *, "OpenMP program"
  if( .not. use_openmp) then
     print *, "Non-OpenMP program"
  end if

end program pippo
like image 31
Massimiliano Avatar answered Jun 28 '26 19:06

Massimiliano