cannam@127: cannam@127: cannam@127: cannam@127: cannam@127:
cannam@127:cannam@127: Next: Accessing the wisdom API from Fortran, Previous: Plan execution in Fortran, Up: Calling FFTW from Modern Fortran [Contents][Index]
cannam@127:In order to obtain maximum performance in FFTW, you should store your
cannam@127: data in arrays that have been specially aligned in memory (see SIMD alignment and fftw_malloc).  Enforcing alignment also permits you to
cannam@127: safely use the new-array execute functions (see New-array Execute Functions) to apply a given plan to more than one pair of in/out
cannam@127: arrays.  Unfortunately, standard Fortran arrays do not provide
cannam@127: any alignment guarantees.  The only way to allocate aligned
cannam@127: memory in standard Fortran is to allocate it with an external C
cannam@127: function, like the fftw_alloc_real and
cannam@127: fftw_alloc_complex functions.  Fortunately, Fortran 2003 provides
cannam@127: a simple way to associate such allocated memory with a standard Fortran
cannam@127: array pointer that you can then use normally.
cannam@127: 
We therefore recommend allocating all your input/output arrays using cannam@127: the following technique: cannam@127:
cannam@127:pointer, arr, to your array of the desired type
cannam@127: and dimensions.  For example, real(C_DOUBLE), pointer :: a(:,:)
cannam@127: for a 2d real array, or complex(C_DOUBLE_COMPLEX), pointer ::
cannam@127: a(:,:,:) for a 3d complex array.
cannam@127: 
cannam@127: integer(C_SIZE_T).  You can either declare a variable of this
cannam@127: type, e.g. integer(C_SIZE_T) :: sz, to store the number of
cannam@127: elements to allocate, or you can use the int(..., C_SIZE_T)
cannam@127: intrinsic function. e.g. set sz = L * M * N or use
cannam@127: int(L * M * N, C_SIZE_T) for an L × M × N array.
cannam@127: 
cannam@127: type(C_PTR) :: p to hold the return value from
cannam@127: FFTW’s allocation routine.  Set p = fftw_alloc_real(sz) for a real array, or p = fftw_alloc_complex(sz) for a complex array.
cannam@127: 
cannam@127: arr with the allocated memory p
cannam@127: using the standard c_f_pointer subroutine: call
cannam@127: c_f_pointer(p, arr, [...dimensions...]), where
cannam@127: [...dimensions...]) are an array of the dimensions of the array
cannam@127: (in the usual Fortran order). e.g. call c_f_pointer(p, arr,
cannam@127: [L,M,N]) for an L × M × N array.  (Alternatively, you can
cannam@127: omit the dimensions argument if you specified the shape explicitly
cannam@127: when declaring arr.)  You can now use arr as a usual
cannam@127: multidimensional array.
cannam@127: 
cannam@127: call
cannam@127: fftw_free(p) on p.
cannam@127: 
cannam@127: For example, here is how we would allocate an L × M 2d real array: cannam@127:
cannam@127:real(C_DOUBLE), pointer :: arr(:,:) cannam@127: type(C_PTR) :: p cannam@127: p = fftw_alloc_real(int(L * M, C_SIZE_T)) cannam@127: call c_f_pointer(p, arr, [L,M]) cannam@127: ...use arr and arr(i,j) as usual... cannam@127: call fftw_free(p) cannam@127:
and here is an L × M × N 3d complex array: cannam@127:
cannam@127:complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:) cannam@127: type(C_PTR) :: p cannam@127: p = fftw_alloc_complex(int(L * M * N, C_SIZE_T)) cannam@127: call c_f_pointer(p, arr, [L,M,N]) cannam@127: ...use arr and arr(i,j,k) as usual... cannam@127: call fftw_free(p) cannam@127:
See Reversing array dimensions for an example allocating a cannam@127: single array and associating both real and complex array pointers with cannam@127: it, for in-place real-to-complex transforms. cannam@127:
cannam@127:cannam@127: Next: Accessing the wisdom API from Fortran, Previous: Plan execution in Fortran, Up: Calling FFTW from Modern Fortran [Contents][Index]
cannam@127: