Chris@42: Chris@42: Chris@42: Chris@42: Chris@42:
Chris@42:Chris@42: Next: Wisdom Generic Export/Import from Fortran, Previous: Wisdom File Export/Import from Fortran, Up: Accessing the wisdom API from Fortran [Contents][Index]
Chris@42:Dealing with FFTW’s C string export/import is a bit more painful. In
Chris@42: particular, the fftw_export_wisdom_to_string
function requires
Chris@42: you to deal with a dynamically allocated C string. To get its length,
Chris@42: you must define an interface to the C strlen
function, and to
Chris@42: deallocate it you must define an interface to C free
:
Chris@42:
use, intrinsic :: iso_c_binding Chris@42: interface Chris@42: integer(C_INT) function strlen(s) bind(C, name='strlen') Chris@42: import Chris@42: type(C_PTR), value :: s Chris@42: end function strlen Chris@42: subroutine free(p) bind(C, name='free') Chris@42: import Chris@42: type(C_PTR), value :: p Chris@42: end subroutine free Chris@42: end interface Chris@42:
Given these definitions, you can then export wisdom to a Fortran Chris@42: character array: Chris@42:
Chris@42:character(C_CHAR), pointer :: s(:) Chris@42: integer(C_SIZE_T) :: slen Chris@42: type(C_PTR) :: p Chris@42: p = fftw_export_wisdom_to_string() Chris@42: if (.not. c_associated(p)) stop 'error exporting wisdom' Chris@42: slen = strlen(p) Chris@42: call c_f_pointer(p, s, [slen+1]) Chris@42: ... Chris@42: call free(p) Chris@42:
Note that slen
is the length of the C string, but the length of
Chris@42: the array is slen+1
because it includes the terminating null
Chris@42: character. (You can omit the ‘+1’ if you don’t want Fortran to
Chris@42: know about the null character.) The standard c_associated
function
Chris@42: checks whether p
is a null pointer, which is returned by
Chris@42: fftw_export_wisdom_to_string
if there was an error.
Chris@42:
To import wisdom from a string, use
Chris@42: fftw_import_wisdom_from_string
as usual; note that the argument
Chris@42: of this function must be a character(C_CHAR)
that is terminated
Chris@42: by the C_NULL_CHAR
character, like the s
array above.
Chris@42: