从复数到实数进行fftw3 MPI Fourier变换时出现奇怪的行为

3
考虑一个问题,你想将f(x,y)的频谱表示转换为坐标空间。因此,exp(i*x) + exp(-i*x) ----> f(x,y),其中f(x,y) = 一些系数 * cos(x)。
在列主排列中(我在下面用Fortran 2003/8编写了示例),频谱数组将被初始化为:
src=(0.,0.)
src(2,1)=(1.,0.)  .

在变换后,您应该得到一个由实数(双精度)组成的tgt数组,其中所有列均相同,并且每列的行为都类似于cos(x)函数。

然而,使用下面给出的代码,我得到一个tgt数组,其中只有奇数列的行为正确,而偶数列则全为零。

我错在哪里了?

我的fftw3版本是3.4.4。

    program c2r
  ! COMPLEX --> REAL 2D MPI TRANSFORM

  ! compiled using: mpif90 -O0 -g -I/usr/include c2rnot.F90 -o c2rnot -L /usr/lib/x86_64-linux-gnu -lfftw3_mpi -lfftw3
  use, intrinsic :: iso_c_binding
  use MPI

  implicit none
  include 'fftw3-mpi.f03'
  integer :: ierr,id

  ! logical dimensions of the transform
  integer(C_INTPTR_T), parameter :: N1 = 32
  integer(C_INTPTR_T), parameter :: N2 = 32

  ! helper variables: storage size, extents along the slow dim, offsets
  integer(C_INTPTR_T) :: alloc_local, slicec,slicer,offc,offr

  ! pointers to planner, real (image) and complex (original) arrays
  type(C_PTR) :: plan, ctgt, csrc

  ! fortran array representation of csrc ...
  complex(8), pointer :: src(:,:)
  ! ..., ctgt and a total array to hold the mpi-gathered result
  real(8), pointer :: tgt(:,:),total(:,:)


  integer(C_INTPTR_T) :: i,y0

  ! init mpi and fftw ...
  call mpi_init(ierr)

  call mpi_comm_rank(MPI_COMM_WORLD,id,ierr)

  call fftw_mpi_init()
  ! done with init


  ! calculate local complex array storage requirements and layout
  alloc_local = fftw_mpi_local_size_2d(N2,N1/2+1, &
        & MPI_COMM_WORLD,slicec,offc)
  ! allocate local complex array
  csrc = fftw_alloc_complex(alloc_local)
  ! fortran representation
  call c_f_pointer(csrc, src, [N1/2+1,slicec])

  ! allocate local real array
  ctgt = fftw_alloc_real(2*alloc_local)
  ! for non-transposed arrays slices are the same
  slicer=slicec
  offr=offc
  ! fortran representation
  call c_f_pointer(ctgt, tgt, [2*(N1/2+1),slicer])

  ! allocate global container (holds gathered tgt arrays)
  if (id==0) allocate(total(N1,N2))

  ! N1 - fast, N2 - slow dimension, in: src, out: tgt
  plan =  fftw_mpi_plan_dft_c2r_2d(N2,N1,src,tgt, MPI_COMM_WORLD, FFTW_MEASURE)


  src=(0.D0,0.D0)

  ! ~ exp(i*x) + exp(-i*x) which should turn into ~ cos(x)
  src(2,1)=(1.D0,0.D0)

  tgt=0.D0
  ! do transform
  call fftw_mpi_execute_dft_c2r(plan,src,tgt)

  ! collect tgt-s into total in process 0
  call mpi_gather(tgt(1:n1,1:slicer),slicer*n1,mpi_double,total,slicer*n1,mpi_double,0,mpi_comm_world,ierr)

  ! The scalar field total has x: 1..N1, y:1..N2 layout. Print
  ! behaviour along the x-axis for a given, constant y. The result
  ! should be ~ cos(x) function for any y0 in [1..N2]. However, I get
  ! such result only for odd y0. For even, total(i,y0) is 0.
  y0=3
  if (id == 0) then
     do i=1,n1
        print *, i, total(i,y0)
     enddo
  endif

  call mpi_finalize(ierr)
end program c2r
1个回答

1
请记住,所有进程都在MPI中执行代码。
因此,当您编写src(2,1)=(1.D0,0.D0)时,您正在设置size频率,每个进程一个。
因此,如果只使用单个进程mpiexec -np 1 c2rnot,则您的代码似乎可以正常工作。但是,如果运行多个进程mpiexec -np 2 c2rnot,则会得到其他结果...
您可以尝试if(id==0) src(2,1)=(1.D0,0.D0)吗? 正如您代码中所示,id是排名。
您还可以在mpi_gather()中使用MPI_REAL8:我的编译器抱怨mpi_double没有IMPLICIT类型。
祝你好运! 再见,

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接