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