Chris@19: Chris@19: Chris@19: Wisdom Generic 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: Previous: Wisdom String Export/Import from Fortran, Chris@19: Up: Accessing the wisdom API from Fortran Chris@19:


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

7.6.3 Wisdom Generic Export/Import from Fortran

Chris@19: Chris@19:

The most generic wisdom export/import functions allow you to provide Chris@19: an arbitrary callback function to read/write one character at a time Chris@19: in any way you want. However, your callback function must be written Chris@19: in a special way, using the bind(C) attribute to be passed to a Chris@19: C interface. Chris@19: Chris@19:

In particular, to call the generic wisdom export function Chris@19: fftw_export_wisdom, you would write a callback subroutine of the form: Chris@19: Chris@19:

       subroutine my_write_char(c, p) bind(C)
Chris@19:          use, intrinsic :: iso_c_binding
Chris@19:          character(C_CHAR), value :: c
Chris@19:          type(C_PTR), value :: p
Chris@19:          ...write c...
Chris@19:        end subroutine my_write_char
Chris@19: 
Chris@19:

Given such a subroutine (along with the corresponding interface definition), you could then export wisdom using: Chris@19: Chris@19:

Chris@19:

       call fftw_export_wisdom(c_funloc(my_write_char), p)
Chris@19: 
Chris@19:

The standard c_funloc intrinsic converts a Fortran Chris@19: bind(C) subroutine into a C function pointer. The parameter Chris@19: p is a type(C_PTR) to any arbitrary data that you want Chris@19: to pass to my_write_char (or C_NULL_PTR if none). (Note Chris@19: that you can get a C pointer to Fortran data using the intrinsic Chris@19: c_loc, and convert it back to a Fortran pointer in Chris@19: my_write_char using c_f_pointer.) Chris@19: Chris@19:

Similarly, to use the generic fftw_import_wisdom, you would Chris@19: define a callback function of the form: Chris@19: Chris@19:

Chris@19:

       integer(C_INT) function my_read_char(p) bind(C)
Chris@19:          use, intrinsic :: iso_c_binding
Chris@19:          type(C_PTR), value :: p
Chris@19:          character :: c
Chris@19:          ...read a character c...
Chris@19:          my_read_char = ichar(c, C_INT)
Chris@19:        end function my_read_char
Chris@19:      
Chris@19:        ....
Chris@19:      
Chris@19:        integer(C_INT) :: ret
Chris@19:        ret = fftw_import_wisdom(c_funloc(my_read_char), p)
Chris@19:        if (ret .eq. 0) stop 'error importing wisdom'
Chris@19: 
Chris@19:

Your function can return -1 if the end of the input is reached. Chris@19: Again, p is an arbitrary type(C_PTR that is passed Chris@19: through to your function. fftw_import_wisdom returns 0 Chris@19: if an error occurred and nonzero otherwise. Chris@19: Chris@19: Chris@19: Chris@19: