문제

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++)

도움이 되었습니까?

해결책 2

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top