summaryrefslogtreecommitdiff
path: root/lib/qra/q65/q65_workspace.f90
blob: 00af36c066dcc1ae9e7e48b50f9400ef130ee226 (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
module q65_workspace

  use iso_c_binding, only: c_int64_t
  use fftw3
  implicit none

  integer, parameter :: Q65_NN=63
  integer, parameter :: Q65_MAXFFT=20736

  type :: q65_workspace_type
     complex, allocatable :: c0(:)
     real, allocatable :: s3(:)
     complex, allocatable :: cs(:)
     integer(c_int64_t) :: plan=0
     integer :: plan_length=0,planning=-1
   contains
     procedure :: ensure => ensure_q65_workspace
     procedure :: prepare_fft
     procedure :: destroy
     final :: finalize
  end type q65_workspace_type

contains

  subroutine ensure_q65_workspace(this,npts2,ll)
    class(q65_workspace_type), intent(inout) :: this
    integer, intent(in) :: npts2,ll
    integer n3

    ! Keep the largest capacity so repeated searches do not churn the allocator.
    n3=ll*Q65_NN

    if(.not.allocated(this%c0)) then
       allocate(this%c0(0:npts2-1))
    else if(size(this%c0).lt.npts2) then
       deallocate(this%c0)
       allocate(this%c0(0:npts2-1))
    endif

    if(.not.allocated(this%s3)) then
       allocate(this%s3(1:n3))
    else if(size(this%s3).lt.n3) then
       deallocate(this%s3)
       allocate(this%s3(1:n3))
    endif

    if(.not.allocated(this%cs)) allocate(this%cs(0:Q65_MAXFFT-1))
  end subroutine ensure_q65_workspace

  subroutine prepare_fft(this,length)
    class(q65_workspace_type), intent(inout) :: this
    integer, intent(in) :: length
    integer npatience,nthreads,flags
    common/patience/npatience,nthreads
    if(this%plan/=0.and.this%plan_length==length.and.this%planning==npatience) return
    !$omp critical(fftw)
    if(this%plan/=0) call sfftw_destroy_plan(this%plan)
    flags=FFTW_ESTIMATE
    if(npatience==1) flags=FFTW_ESTIMATE_PATIENT
    if(npatience==2) flags=FFTW_MEASURE
    if(npatience==3) flags=FFTW_PATIENT
    if(npatience==4) flags=FFTW_EXHAUSTIVE
    call sfftw_plan_dft_1d(this%plan,length,this%cs,this%cs,FFTW_FORWARD,flags)
    !$omp end critical(fftw)
    this%plan_length=length
    this%planning=npatience
  end subroutine

  subroutine destroy(this)
    class(q65_workspace_type), intent(inout) :: this
    !$omp critical(fftw)
    if(this%plan/=0) call sfftw_destroy_plan(this%plan)
    !$omp end critical(fftw)
    this%plan=0
    if(allocated(this%c0)) deallocate(this%c0)
    if(allocated(this%s3)) deallocate(this%s3)
    if(allocated(this%cs)) deallocate(this%cs)
  end subroutine

  subroutine finalize(this)
    type(q65_workspace_type), intent(inout) :: this
    call this%destroy()
  end subroutine

end module q65_workspace