program main
    implicit none
    integer,parameter :: FTSIZE=8192
    integer :: tic_start,tic_finish,tic_rate
    real*8 :: t
    real*8,parameter :: M_PI=3.141592653589793238460D0
    integer :: i
    complex*16,dimension(0:FTSIZE-1):: X,B,C
    do i=0,FTSIZE-1
        X(i)=cmplx(1D0/(i+1),1D0/(FTSIZE-i),kind(X(i)));
    end do
    print '("N=",I0)',FTSIZE
    call tic
    call dft(X,B)
    t=toc()
    print '("B(0)=(",G0," ",G0,")")',B(0)
    print '("dft took ",G0," seconds.")',t
!$omp parallel
!$omp single
    call tic
    call pdft(X,B)
    t=toc()
!$omp end single
!$omp end parallel
    print '("B(0)=(",G0," ",G0,")")',B(0)
    print '("parallel dft took ",G0," seconds.")',t
    return

contains 

subroutine tic
    call system_clock(count_rate=tic_rate)
    call system_clock(tic_start)
end subroutine

real*8 function toc()
    call system_clock(tic_finish)
    toc=float(tic_finish-tic_start)/tic_rate
end function

subroutine dft(x,b)
    complex*16,dimension(0:) :: x,b
    integer :: N,l,k
    N=size(x)
    do l=0,N-1
        b(l)=0
    end do
    do k=0,N-1
        do l=0,N-1
            b(k)=b(k)+exp(cmplx(0D0,-2*M_PI*k*l/N,kind(b(k))))*x(l)
        end do
    end do
end subroutine

subroutine pdft(x,b)
    complex*16,dimension(0:) :: x,b
    integer :: N,l,k
    N=size(x)
    do l=0,N-1
        b(l)=0
    end do
!$omp taskloop default(shared)
    do k=0,N-1
        do l=0,N-1
            b(k)=b(k)+exp(cmplx(0D0,-2*M_PI*k*l/N,kind(b(k))))*x(l)
        end do
    end do
!$omp end taskloop
end subroutine

end program
