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