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

Reference to a non-target variable

NsK
Novice
1,004 Views

Hi,

A really simple question: is there any way (trick?) to point/save a reference to a non-target variable?

Why do I want to do that?
I'm developping a module for a huge f77 code that I cannot change, and it would be very neat to be able to use pointers to reference the old variables (mainly multidimensional arrays here).
Many thanks in advance,

Nick

0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,004 Views

The problem is bigger than TARGET - you need to make sure that the variable passed in remains "in scope" at all times while you may be referencing it, and that the compiled code of the caller makes no assumptions about asynchronous reads or writes of the variable. You can't impose this arbitrarily - the caller needs to cooperate.

The mechanics of saving the pointer are pretty easy - you can use C_F_POINTER(C_LOC(arg), pointername) to create a pointer to anything. But it's then up to you to make sure the pointer remains valid. The caller would, at the least, need to give the variable the VOLATILE attribute (in the next Fortran standard, the ASYNCHRONOUS attribute would be applicable to such variables without the heavier weight of VOLATILE), but the caller would still need to make sure that the variable stayed "in scope" until your module was done with it. MPI has similar issues.

View solution in original post

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,004 Views

Do I understand correctly that you want routines in your module to save pointers to arguments passed in without requiring that the caller give the passed variables the TARGET attribute? If not, please explain in more detail what you'd like to do - show a short example.

0 Kudos
NsK
Novice
1,004 Views

Hi Steve,

You understood well. My apologies for the bad explanation.
Cheers,

Nick

0 Kudos
Steven_L_Intel1
Employee
1,005 Views

The problem is bigger than TARGET - you need to make sure that the variable passed in remains "in scope" at all times while you may be referencing it, and that the compiled code of the caller makes no assumptions about asynchronous reads or writes of the variable. You can't impose this arbitrarily - the caller needs to cooperate.

The mechanics of saving the pointer are pretty easy - you can use C_F_POINTER(C_LOC(arg), pointername) to create a pointer to anything. But it's then up to you to make sure the pointer remains valid. The caller would, at the least, need to give the variable the VOLATILE attribute (in the next Fortran standard, the ASYNCHRONOUS attribute would be applicable to such variables without the heavier weight of VOLATILE), but the caller would still need to make sure that the variable stayed "in scope" until your module was done with it. MPI has similar issues.

0 Kudos
NsK
Novice
1,004 Views

Many thanks Steve, it just might be what I need.
I'm gonna take a wild guess and assume that this issue is especially sensitive in the case of non-blocking MPI calls.
Therefore, I suppose that everything should be ok as long as the caller does not touch the variable before the callee has finished its operations –and this even without the VOLATILE attribute. Does it make sense?

0 Kudos
Steven_L_Intel1
Employee
1,004 Views

Yes, it's much the same as non-blocking MPI calls. You do have to worry about the compiler moving references to the variable, which it can do if the variable is not declared VOLATILE.

0 Kudos
Reply