cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: FFTW 3.3.8: Wisdom String Export/Import from Fortran cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167:
cannam@167:

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

cannam@167:
cannam@167:
cannam@167: cannam@167:

7.6.2 Wisdom String Export/Import from Fortran

cannam@167: cannam@167: cannam@167:

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

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

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

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

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

cannam@167: cannam@167:

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

cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: