Chris@19: Chris@19: Chris@19: Wisdom String Export/Import from Fortran - FFTW 3.3.4 Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19:
Chris@19: Chris@19: Chris@19:

Chris@19: Next: , Chris@19: Previous: Wisdom File Export/Import from Fortran, Chris@19: Up: Accessing the wisdom API from Fortran Chris@19:


Chris@19:
Chris@19: Chris@19:

7.6.2 Wisdom String Export/Import from Fortran

Chris@19: Chris@19:

Dealing with FFTW's C string export/import is a bit more painful. In Chris@19: particular, the fftw_export_wisdom_to_string function requires Chris@19: you to deal with a dynamically allocated C string. To get its length, Chris@19: you must define an interface to the C strlen function, and to Chris@19: deallocate it you must define an interface to C free: Chris@19: Chris@19:

       use, intrinsic :: iso_c_binding
Chris@19:        interface
Chris@19:          integer(C_INT) function strlen(s) bind(C, name='strlen')
Chris@19:            import
Chris@19:            type(C_PTR), value :: s
Chris@19:          end function strlen
Chris@19:          subroutine free(p) bind(C, name='free')
Chris@19:            import
Chris@19:            type(C_PTR), value :: p
Chris@19:          end subroutine free
Chris@19:        end interface
Chris@19: 
Chris@19:

Given these definitions, you can then export wisdom to a Fortran Chris@19: character array: Chris@19: Chris@19:

       character(C_CHAR), pointer :: s(:)
Chris@19:        integer(C_SIZE_T) :: slen
Chris@19:        type(C_PTR) :: p
Chris@19:        p = fftw_export_wisdom_to_string()
Chris@19:        if (.not. c_associated(p)) stop 'error exporting wisdom'
Chris@19:        slen = strlen(p)
Chris@19:        call c_f_pointer(p, s, [slen+1])
Chris@19:        ...
Chris@19:        call free(p)
Chris@19: 
Chris@19:

Chris@19: Note that slen is the length of the C string, but the length of Chris@19: the array is slen+1 because it includes the terminating null Chris@19: character. (You can omit the ‘+1’ if you don't want Fortran to Chris@19: know about the null character.) The standard c_associated function Chris@19: checks whether p is a null pointer, which is returned by Chris@19: fftw_export_wisdom_to_string if there was an error. Chris@19: Chris@19:

To import wisdom from a string, use Chris@19: fftw_import_wisdom_from_string as usual; note that the argument Chris@19: of this function must be a character(C_CHAR) that is terminated Chris@19: by the C_NULL_CHAR character, like the s array above. Chris@19: Chris@19: Chris@19: Chris@19: