[petsc-dev] XXXGetContext() in Fortran
Barry Smith
bsmith at mcs.anl.gov
Thu Nov 10 20:51:49 CST 2011
On Nov 10, 2011, at 4:46 PM, Jed Brown wrote:
> On Wed, Nov 9, 2011 at 12:52, Barry Smith <bsmith at mcs.anl.gov> wrote:
> So what both Satish's and Ethan's email are saying is so long as you tell the Fortran function the interface that shows the type being returned you can "trick" the Fortran into thinking that data type is just coming from another Fortran routine.
>
> I think this should resolve your issues and everything should be straightforward (no need for transfer function). Please let us know if this is problematic.
>
> Thanks everyone. This works, and in hindsight, I should have looked at snes/examples/tutorials/ex5f90.F because it does this with SNESGetApplicationContext.
>
> The unfortunate thing is that it appears that the user needs to write a custom interface for each such function in order to get the compiler to pass the pointer in a useful way. Is there a way for us to put some interface definitions into the library? An (ugly) alternative would be to provide some CPP macros that the user can use to get interface definitions without needing to hand-type it (they would pass in their user-defined type).
I think it is fine that the user writes the interface themselves and I think trying to have ways to "help" them won't help them. So long as we have examples to show how to do this and they are linked (automatically) from the manual pages I think that is the right approach.
>
> How can we manage multiple contexts with different types in the same file? It looks like perhaps I can put the interface definitions in different modules, which I can import at subroutine granularity. If I want to get the context out of two MatShells inside the same function, I would have to write helper functions so that they can import different interface definitions via different modules?
There is a truly perverse, but relatively easy way to handle this. Fortran allows having several interfaces that map a single function name to several actual function names. This is done, for example, in include/finclude/petscdmda.h90. So basically we would need to provide several C functions for each operation. For example SNESGetApplicationContext1(), SNESGetApplicationContext2() that have identical source code. Then the user writes one interface that refers to both of these with different derived types as the arguments.
Barry
Note we would also provide the usual SNESGetApplicationContext() C stub for use in the normal case of only one needed.
More information about the petsc-dev
mailing list