summaryrefslogtreecommitdiff
path: root/map65/libm65/four2a.f90
blob: 91be0f48c4f9fab24008ae32984f6fa44366b4b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module four2a_mod
  use fftw3
  use iso_c_binding
  use fftw3_f77_interfaces

  implicit none
  
contains

  function loc_cplx(x) result(addr)
    use iso_c_binding, only: c_float_complex, c_intptr_t, c_loc
    implicit none
    complex(c_float_complex), intent(in), target :: x(*)
    integer(c_intptr_t) :: addr
    addr = transfer(c_loc(x), addr)
  end function loc_cplx

  recursive subroutine four2a(a, nfft, ndim, isign, iform) bind(C, name='four2a_')

    implicit none

    !==== Arguments ============================================================
    complex(c_float_complex), dimension(*), intent(inout) :: a
    integer(c_int), intent(in) :: nfft, ndim, isign, iform

    !==== Parameters ===========================================================
    integer, parameter :: NPMAX = 2100
    integer, parameter :: NSMALL = 16384

    !==== Local variables ======================================================
    complex(c_float_complex) :: aa(NSMALL)
    integer :: nn(NPMAX), ns(NPMAX), nf(NPMAX)
    integer(c_intptr_t) :: nl(NPMAX), nloc
    integer(c_intptr_t) :: plan(NPMAX)
    logical :: found_plan
    integer :: i, jz = 0, nflags
    integer :: nplan

    !==== Saved state ==========================================================
    save plan, nplan, nn, ns, nf, nl
    data nplan / 0 /

    integer :: dummy_unused
    dummy_unused = ndim


    !==== Early exit: destroy all plans =======================================
    if (nfft < 0) then
       !$omp critical(four2a)
       do i = 1, nplan
          !$omp critical(fftw)
          call sfftw_destroy_plan(plan(i))
          !$omp end critical(fftw)
       enddo
       nplan = 0
       !$omp end critical(four2a)
       return
    endif

    !==== Locate or create plan ===============================================
    nloc = loc_cplx(a)
    found_plan = .false.

    !$omp critical(four2a_setup)
    do i = 1, nplan
       if (nfft == nn(i) .and. isign == ns(i) .and. iform == nf(i) .and. nloc == nl(i)) then
          found_plan = .true.
          exit
       endif
    enddo

    if (.not. found_plan) then
       nplan = nplan + 1
       i = nplan

       nn(i) = nfft
       ns(i) = isign
       nf(i) = iform
       nl(i) = nloc

       nflags = FFTW_ESTIMATE

       if (nfft <= NSMALL) then
          jz = nfft
          if (iform <= 0) jz = nfft/2 + 1
          aa(1:jz) = a(1:jz)
       endif

       !$omp critical(fftw)
       select case (iform)
       case (1)
          if (isign == -1) then
             call sfftw_plan_dft_1d(plan(i), nfft, a, a, FFTW_FORWARD, nflags)
          else
             call sfftw_plan_dft_1d(plan(i), nfft, a, a, FFTW_BACKWARD, nflags)
          endif
       case (0)
          call sfftw_plan_dft_r2c_1d(plan(i), nfft, a, a, nflags)
       case (-1)
          call sfftw_plan_dft_c2r_1d(plan(i), nfft, a, a, nflags)
       case default
          stop 'Unsupported request in four2a'
       end select
       !$omp end critical(fftw)

       if (nfft <= NSMALL) then
          a(1:jz) = aa(1:jz)
       endif
    endif
    !$omp end critical(four2a_setup)

    call sfftw_execute(plan(i))

  end subroutine four2a

end module four2a_mod