cannam@95: cannam@95: cannam@95: Wisdom String Export/Import from Fortran - FFTW 3.3.3 cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95:
cannam@95: cannam@95: cannam@95:

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


cannam@95:
cannam@95: cannam@95:

7.6.2 Wisdom String Export/Import from Fortran

cannam@95: cannam@95:

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

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

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

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

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

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