- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve,
You understood well. My apologies for the bad explanation.
Cheers,
Nick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page