Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: FFTW 3.3.5: Allocating aligned memory in Fortran Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42:
Chris@42:

Chris@42: Next: , Previous: , Up: Calling FFTW from Modern Fortran   [Contents][Index]

Chris@42:
Chris@42:
Chris@42: Chris@42:

7.5 Allocating aligned memory in Fortran

Chris@42: Chris@42: Chris@42: Chris@42: Chris@42:

In order to obtain maximum performance in FFTW, you should store your Chris@42: data in arrays that have been specially aligned in memory (see SIMD alignment and fftw_malloc). Enforcing alignment also permits you to Chris@42: 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@42: arrays. Unfortunately, standard Fortran arrays do not provide Chris@42: any alignment guarantees. The only way to allocate aligned Chris@42: memory in standard Fortran is to allocate it with an external C Chris@42: function, like the fftw_alloc_real and Chris@42: fftw_alloc_complex functions. Fortunately, Fortran 2003 provides Chris@42: a simple way to associate such allocated memory with a standard Fortran Chris@42: array pointer that you can then use normally. Chris@42:

Chris@42:

We therefore recommend allocating all your input/output arrays using Chris@42: the following technique: Chris@42:

Chris@42:
    Chris@42:
  1. Declare a pointer, arr, to your array of the desired type Chris@42: and dimensions. For example, real(C_DOUBLE), pointer :: a(:,:) Chris@42: for a 2d real array, or complex(C_DOUBLE_COMPLEX), pointer :: Chris@42: a(:,:,:) for a 3d complex array. Chris@42: Chris@42:
  2. The number of elements to allocate must be an Chris@42: integer(C_SIZE_T). You can either declare a variable of this Chris@42: type, e.g. integer(C_SIZE_T) :: sz, to store the number of Chris@42: elements to allocate, or you can use the int(..., C_SIZE_T) Chris@42: intrinsic function. e.g. set sz = L * M * N or use Chris@42: int(L * M * N, C_SIZE_T) for an L × M × N array. Chris@42: Chris@42:
  3. Declare a type(C_PTR) :: p to hold the return value from Chris@42: 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@42: Chris@42:
  4. Chris@42: Associate your pointer arr with the allocated memory p Chris@42: using the standard c_f_pointer subroutine: call Chris@42: c_f_pointer(p, arr, [...dimensions...]), where Chris@42: [...dimensions...]) are an array of the dimensions of the array Chris@42: (in the usual Fortran order). e.g. call c_f_pointer(p, arr, Chris@42: [L,M,N]) for an L × M × N array. (Alternatively, you can Chris@42: omit the dimensions argument if you specified the shape explicitly Chris@42: when declaring arr.) You can now use arr as a usual Chris@42: multidimensional array. Chris@42: Chris@42:
  5. When you are done using the array, deallocate the memory by call Chris@42: fftw_free(p) on p. Chris@42: Chris@42:
Chris@42: Chris@42:

For example, here is how we would allocate an L × M 2d real array: Chris@42:

Chris@42:
Chris@42:
  real(C_DOUBLE), pointer :: arr(:,:)
Chris@42:   type(C_PTR) :: p
Chris@42:   p = fftw_alloc_real(int(L * M, C_SIZE_T))
Chris@42:   call c_f_pointer(p, arr, [L,M])
Chris@42:   ...use arr and arr(i,j) as usual...
Chris@42:   call fftw_free(p)
Chris@42: 
Chris@42: Chris@42:

and here is an L × M × N 3d complex array: Chris@42:

Chris@42:
Chris@42:
  complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:)
Chris@42:   type(C_PTR) :: p
Chris@42:   p = fftw_alloc_complex(int(L * M * N, C_SIZE_T))
Chris@42:   call c_f_pointer(p, arr, [L,M,N])
Chris@42:   ...use arr and arr(i,j,k) as usual...
Chris@42:   call fftw_free(p)
Chris@42: 
Chris@42: Chris@42:

See Reversing array dimensions for an example allocating a Chris@42: single array and associating both real and complex array pointers with Chris@42: it, for in-place real-to-complex transforms. Chris@42:

Chris@42:
Chris@42:
Chris@42:

Chris@42: Next: , Previous: , Up: Calling FFTW from Modern Fortran   [Contents][Index]

Chris@42:
Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: