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