cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: FFTW 3.3.5: Wisdom String Export/Import from Fortran cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127:
cannam@127:

cannam@127: Next: , Previous: , Up: Accessing the wisdom API from Fortran   [Contents][Index]

cannam@127:
cannam@127:
cannam@127: cannam@127:

7.6.2 Wisdom String Export/Import from Fortran

cannam@127: cannam@127: cannam@127:

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

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

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

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

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

cannam@127: cannam@127:

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

cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: