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

Return fortran Pointer to user-type to C

vikrantca
Beginner
875 Views
Hello,

I am trying to convert existing fortran code (with lots of COMMON blocks) to a threadsafe version. Here is the idea:

Each caller calls an initialize method on the fortran DLL which returns a pointer to a user-defined type. This user-type has all the data that goes in the common block.

For subsequent subroutine calls, the caller passed this pointer to the subroutines and the routines then use the data that this pointer points to.

Note that the caller is not going to use the pointer at all - as far as the caller is concerned, it is just a handle.

My questions: Is this a good approach ? Is there a better approach ?
Where can I find sample code or example of such calls ?

Thank you.
vikrantca
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
875 Views
Um, we had a similar discussion recently, and the original poster was also named Vikrant. I presume it's you -- or is it just a coincidence?

If this is just supposed to be the continuation of that thread, you can make use of the fact that scalar Fortran pointers in IVF (and most other compilers) are just 4-byte addresses, thus they can be represented by a C type whatever**:

Fortran:
subroutine Init (cmnData)
type(CmnBlock), pointer:: cmnData
allocate (cmnData)cmnData%whatever = ...
...
end subroutine Init

subroutine DoSomething(cmnData)
type(CmnBlock):: cmnData
...
!do something with cmnData
end subroutine DoSomething
C:
typedef struct {
int dummy
}CmnBlock, lpCmnBlock*;
extern "C" void init(lpCmnBlock*);
extern "C" void dosomething(CmnBlock*);
...
lpCmnBlock handle;
init(&handle);
...
dosomething(handle);

Frankly, I'm not positive if I got the C side right, but I hope you get the idea.
0 Kudos
vikrantca
Beginner
875 Views
Thanks for the reply Jugoslav.
Yes, the original poster for that discussion was me. We discussed several options in that thread and I implemented all of them, except for the pointer option, which I couldnt get to work.
Hence the second followup question.

Thankyou.
-VA
0 Kudos
Reply