Chris@10: Chris@10:
Chris@10:Chris@10: Previous: Wisdom String Export/Import from Fortran, Chris@10: Up: Accessing the wisdom API from Fortran Chris@10:
The most generic wisdom export/import functions allow you to provide
Chris@10: an arbitrary callback function to read/write one character at a time
Chris@10: in any way you want. However, your callback function must be written
Chris@10: in a special way, using the bind(C)
attribute to be passed to a
Chris@10: C interface.
Chris@10:
Chris@10:
In particular, to call the generic wisdom export function
Chris@10: fftw_export_wisdom
, you would write a callback subroutine of the form:
Chris@10:
Chris@10:
subroutine my_write_char(c, p) bind(C) Chris@10: use, intrinsic :: iso_c_binding Chris@10: character(C_CHAR), value :: c Chris@10: type(C_PTR), value :: p Chris@10: ...write c... Chris@10: end subroutine my_write_char Chris@10:Chris@10:
Given such a subroutine (along with the corresponding interface definition), you could then export wisdom using: Chris@10: Chris@10:
call fftw_export_wisdom(c_funloc(my_write_char), p) Chris@10:Chris@10:
The standard c_funloc
intrinsic converts a Fortran
Chris@10: bind(C)
subroutine into a C function pointer. The parameter
Chris@10: p
is a type(C_PTR)
to any arbitrary data that you want
Chris@10: to pass to my_write_char
(or C_NULL_PTR
if none). (Note
Chris@10: that you can get a C pointer to Fortran data using the intrinsic
Chris@10: c_loc
, and convert it back to a Fortran pointer in
Chris@10: my_write_char
using c_f_pointer
.)
Chris@10:
Chris@10:
Similarly, to use the generic fftw_import_wisdom
, you would
Chris@10: define a callback function of the form:
Chris@10:
Chris@10:
integer(C_INT) function my_read_char(p) bind(C) Chris@10: use, intrinsic :: iso_c_binding Chris@10: type(C_PTR), value :: p Chris@10: character :: c Chris@10: ...read a character c... Chris@10: my_read_char = ichar(c, C_INT) Chris@10: end function my_read_char Chris@10: Chris@10: .... Chris@10: Chris@10: integer(C_INT) :: ret Chris@10: ret = fftw_import_wisdom(c_funloc(my_read_char), p) Chris@10: if (ret .eq. 0) stop 'error importing wisdom' Chris@10:Chris@10:
Your function can return -1
if the end of the input is reached.
Chris@10: Again, p
is an arbitrary type(C_PTR
that is passed
Chris@10: through to your function. fftw_import_wisdom
returns 0
Chris@10: if an error occurred and nonzero otherwise.
Chris@10:
Chris@10:
Chris@10:
Chris@10: