Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

A Bug Occur when I call a pure subroutine in OpenMP

志强_赵_
Beginner
1,779 Views

1, The pure subroutine defined in a.f90, there is no OpenMP used here.

2, the OpenMP used in m.f90

when I compile the a.f90 like `ifort a.f90 -c -o a.f90` without -openmp

wrong result returned from the pure subroutine.

 

But, when I compile the a.f90 like `ifort -c -openmp -o a.f90`

correct result returned.

 

As I know, the pure subroutine is process safe. Why my subroutine return two kinds of results.

The a.f90 doesn't use OpenMP , why I shold compile it use -openmp

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
1,779 Views

>>-auto can set call variables AUTOMATIC which means each subprogram will have their own copy.

Maybe something is lost in translation.

To me, "call variables" are the arguments on the call statement and listed arguments on the subroutine statement. In Fortran-speak these are referred to as Dummy arguments. The subroutine scoped variables (those not dummy arguments) are the variables affected by the -auto, the dummy arguments are not affected by the -auto.

In Fortran without -auto

real :: vec(3)

is equivalent to C

static float vec[3];

And with -auto is equivalent to C

float vec[3];

When the vec[3] is defined inside a C function static produces a shared copy, whereas non-static (stack local) provides a private copy to the caller (thread and/or recursion level).

Jim Dempsey

View solution in original post

0 Kudos
13 Replies
志强_赵_
Beginner
1,779 Views

I use ifort 13.0.0

0 Kudos
Steven_L_Intel1
Employee
1,779 Views

You will need to show us a complete test case with a small (if possible) source that shows the problem.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,779 Views

>> `ifort a.f90 -c -o a.f90`

The above command line is producing an object file (a.o).

If this file is subsequently linked with a threaded program any local arrays contained within subroutines or functions within a.o will not be multi-threaded safe.

The default option is -auto-scalar, which means scalar variables are stored on stack (but not arrays). You can override this by using -auto or indirectly by using -openmp.

Jim Dempsey

0 Kudos
TimP
Honored Contributor III
1,779 Views

As Jim pointed out, -openmp option could make the subroutine safe to execute in parallel (or re-entrant), as would adding RECURSIVE in the subroutine declaration.  This requires that the local arrays be correctly initialized; as it works for you, they may be OK.


 

0 Kudos
志强_赵_
Beginner
1,779 Views

Tim Prince wrote:

As Jim pointed out, -openmp option could make the subroutine safe to execute in parallel (or re-entrant), as would adding RECURSIVE in the subroutine declaration.  This requires that the local arrays be correctly initialized; as it works for you, they may be OK.

 

Awesome, It's FIne

0 Kudos
志强_赵_
Beginner
1,779 Views

jimdempseyatthecove wrote:

>> `ifort a.f90 -c -o a.f90`

The above command line is producing an object file (a.o).

If this file is subsequently linked with a threaded program any local arrays contained within subroutines or functions within a.o will not be multi-threaded safe.

The default option is -auto-scalar, which means scalar variables are stored on stack (but not arrays). You can override this by using -auto or indirectly by using -openmp.

Jim Dempsey

Thanks for your reply. Everything is Fine. But I still have a question.

-auto can set call variables AUTOMATIC which means each subprogram will have their own copy.

But if not, why the error occur.  If one thread has allocate memory for the variables in one subroutine, another thread can overwrite the memory directly? 

0 Kudos
TimP
Honored Contributor III
1,779 Views

Yes, without -auto, all threads will share a single copy of the local arrays, and any modification creates a race condition.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,780 Views

>>-auto can set call variables AUTOMATIC which means each subprogram will have their own copy.

Maybe something is lost in translation.

To me, "call variables" are the arguments on the call statement and listed arguments on the subroutine statement. In Fortran-speak these are referred to as Dummy arguments. The subroutine scoped variables (those not dummy arguments) are the variables affected by the -auto, the dummy arguments are not affected by the -auto.

In Fortran without -auto

real :: vec(3)

is equivalent to C

static float vec[3];

And with -auto is equivalent to C

float vec[3];

When the vec[3] is defined inside a C function static produces a shared copy, whereas non-static (stack local) provides a private copy to the caller (thread and/or recursion level).

Jim Dempsey

0 Kudos
志强_赵_
Beginner
1,779 Views

jimdempseyatthecove wrote:

>>-auto can set call variables AUTOMATIC which means each subprogram will have their own copy.

Maybe something is lost in translation.

To me, "call variables" are the arguments on the call statement and listed arguments on the subroutine statement. In Fortran-speak these are referred to as Dummy arguments. The subroutine scoped variables (those not dummy arguments) are the variables affected by the -auto, the dummy arguments are not affected by the -auto.

In Fortran without -auto

real :: vec(3)

is equivalent to C

static float vec[3];

And with -auto is equivalent to C

float vec[3];

When the vec[3] is defined inside a C function static produces a shared copy, whereas non-static (stack local) provides a private copy to the caller (thread and/or recursion level).

Jim Dempsey

Thanks so much. This is so magic. It's the first time for me to find the array is static default. You are so awesome. This problem is clear now.:)

0 Kudos
TimP
Honored Contributor III
1,779 Views

The local array static default seems to be a "legacy relic" from long-ago DEC Fortran (which didn't have good support for threading).  There may have been some cases which would have run slower when the default of /Qsave was removed.  It may create enough confusion now that perhaps -auto ought to be considered for inclusion in -standard-semantics

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,779 Views

Tim,

You can add this option to the environment variable INTEL_PRE_FFLAGS and/or ifort.cfg

Changing it to standard-semantics might blindside users during updates. On the other hand it would benefit newbie C programmers from making posts like this thread.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,779 Views

Tim, as it happens, the Fortran standards committee recently voted to make RECURSIVE the default for all procedures in Fortran 2015, so,, eventually, that will indeed happen. My guess is that we'll just make it the normal default. After all, C has been dealing with this for years.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,779 Views

Yea!!!

0 Kudos
Reply