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