如何使用fftw Guru界面

How to use fftw Guru interface

我曾经使用fftw_plan_dft进行多维傅里叶变换。

fftw_plan fftw_plan_dft(int rank, const int *n, fftw_complex *in,
                        fftw_complex *out, int sign, unsigned flags);

现在我想将64位整数传递给fftw,看来我需要使用fftw guru接口。

 fftw_plan fftw_plan_guru64_dft(
     int rank, const fftw_iodim64 *dims,
     int howmany_rank, const fftw_iodim64 *howmany_dims,
     fftw_complex *in, fftw_complex *out,
     int sign, unsigned flags);

但是我不明白howmany_rankhowmany_dims是什么意思。 fftw_plan_guru_dft 的手册说:

These two functions plan a complex-data, multi-dimensional DFT for the interleaved and split format, respectively. Transform dimensions are given by (rank, dims) over a multi-dimensional vector (loop) of dimensions (howmany_rank, howmany_dims). dims and howmany_dims should point to fftw_iodim arrays of length rank and howmany_rank, respectively.

我知道 "multi-dimensional vector (loop) of dimensions (howmany_rank, howmany_dims)" 是什么意思。你能举个例子或解释一下如何使用这个 guru 界面吗?

如果多维数组的大小和跨度大于 2^32,64 bit guru interface 就很有用了。

创建复杂到复杂 DTF 的函数原型是:

fftw_plan fftw_plan_guru64_dft(
 int rank, const fftw_iodim64 *dims,
 int howmany_rank, const fftw_iodim64 *howmany_dims,
 fftw_complex *in, fftw_complex *out,
 int sign, unsigned flags);

其中:

  • rank是要进行的FFTW变换的秩,也就是维数。
  • dims 是一个大小为 rank 的数组。对于每个维度 idims[i].n 是行的大小,dims[i].is 是输入数组行之间的步幅,dims[i].os 是输出数组行之间的步幅.例如,如果数组在内存中是连续的,那么 the documentation of the guru interface 建议使用递归 dims[i].is = n[i+1] * dims[i+1].ishowmany_rankhowmany_dims 给出了要执行的变换数和起点之间的偏移量。
  • howmany_rank 指定具有特定偏移量的变换数。
  • howmany_dims 是一个大小为 howmany_rank 的数组。对于每个变换 ihowmany_dims[i].n 是要计算的变换数,每个变换都具有输入之间的偏移量 howmany_dims[i].is 和输出之间的偏移量 howmany_dims[i].os

中提供了有关这些参数的更多详细信息

以下代码调用 fftw_plan_guru64_dft(),以便它执行与 fftw_plan_dft_3d() 相同的操作。可以通过gcc main.c -o main -lfftw3 -lm -Wall:

编译
#include<stdlib.h>
#include<complex.h>
#include<math.h>
#include<fftw3.h>

int main(void){

    fftw_plan p;
    unsigned long int N = 10;
    unsigned long int M = 12;
    unsigned long int P = 14;
    fftw_complex *in=fftw_malloc(N*M*P*sizeof(fftw_complex));
    if(in==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    fftw_complex *out=fftw_malloc(N*M*P*sizeof(fftw_complex));
    if(out==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    unsigned int i,j,k;

    int rank=3;
    fftw_iodim64 *dims=malloc(rank*sizeof(fftw_iodim64));
    if(dims==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    dims[0].n=N;
    dims[0].is=P*M;
    dims[0].os=P*M;
    dims[1].n=M;
    dims[1].is=P;
    dims[1].os=P;
    dims[2].n=P;
    dims[2].is=1;
    dims[2].os=1;

    int howmany_rank=1;
    fftw_iodim64 *howmany_dims=malloc(howmany_rank*sizeof(fftw_iodim64));
    if(howmany_dims==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    howmany_dims[0].n=1;
    howmany_dims[0].is=1;
    howmany_dims[0].os=1;

    printf("sizeof fftw complex %ld\n",sizeof(fftw_complex));
    printf("sizeof fftw_iodim64 %ld\n",sizeof(fftw_iodim64));
    printf("creating the plan\n");
    p=fftw_plan_guru64_dft(rank, dims,howmany_rank, howmany_dims,in, out,FFTW_FORWARD, FFTW_ESTIMATE);
    if (p==NULL){fprintf(stderr,"plan creation failed\n");exit(1);}
    printf("created the plan\n");

    for(i=0;i<N;i++){
        for(j=0;j<M;j++){
            for(k=0;k<P;k++){
                //printf("ijk\n");
                in[(i*M+j)*P+k]=30.+12.*sin(2*3.1415926535*i/((double)N))*sin(2*3.1415926535*j/((double)M))*sin(2*3.1415926535*k/((double)P))*I;
            }
        }
    }

    fftw_execute(p);

    for (i = 0; i < N; i++){
        for (j = 0; j < M; j++){
            for (k = 0; k < P; k++){
                printf("result: %d %d %d %g %gI\n", i,j,k, creal(out[(i*M+j)*P+k]), cimag(out[(i*M+j)*P+k]));
            }
        }
    }


    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);

    free(dims);
    free(howmany_dims);

    return(0);
}

例如,guru 界面可用于计算复杂 3D 电场的 DFT。在网格的每个点,电场都是大小为 3 的向量。因此,我可以将电场存储为 4D 数组,最后一个维度指定向量的分量。最后,可以使用大师界面一次执行三个 3D DFT:

#include<stdlib.h>
#include<complex.h>
#include<math.h>
#include<fftw3.h>

int main(void){

    fftw_plan p;
    unsigned long int N = 10;
    unsigned long int M = 12;
    unsigned long int P = 14;
    unsigned long int DOF = 3;
    fftw_complex *in=fftw_malloc(N*M*P*DOF*sizeof(fftw_complex));
    if(in==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    fftw_complex *out=fftw_malloc(N*M*P*DOF*sizeof(fftw_complex));
    if(out==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    unsigned int i,j,k;

    int rank=3;
    fftw_iodim64 *dims=malloc(rank*sizeof(fftw_iodim64));
    if(dims==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    dims[0].n=N;
    dims[0].is=P*M*DOF;
    dims[0].os=P*M*DOF;
    dims[1].n=M;
    dims[1].is=P*DOF;
    dims[1].os=P*DOF;
    dims[2].n=P;
    dims[2].is=DOF;
    dims[2].os=DOF;

    int howmany_rank=1;
    fftw_iodim64 *howmany_dims=malloc(howmany_rank*sizeof(fftw_iodim64));
    if(howmany_dims==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    howmany_dims[0].n=DOF;
    howmany_dims[0].is=1;
    howmany_dims[0].os=1;

    printf("sizeof fftw complex %ld\n",sizeof(fftw_complex));
    printf("sizeof fftw_iodim64 %ld\n",sizeof(fftw_iodim64));
    printf("creating the plan\n");
    p=fftw_plan_guru64_dft(rank, dims,howmany_rank, howmany_dims,in, out,FFTW_FORWARD, FFTW_ESTIMATE);
    if (p==NULL){fprintf(stderr,"plan creation failed\n");exit(1);}
    printf("created the plan\n");

    for(i=0;i<N;i++){
        for(j=0;j<M;j++){
            for(k=0;k<P;k++){
                //printf("ijk\n");
                in[((i*M+j)*P+k)*DOF]=30.+12.*sin(2*3.1415926535*i/((double)N))*sin(2*3.1415926535*j/((double)M))*sin(2*3.1415926535*k/((double)P))*I;
                in[((i*M+j)*P+k)*DOF+1]=42.0;
                in[((i*M+j)*P+k)*DOF+2]=1.0;
            }
        }
    }

    fftw_execute(p);

    for (i = 0; i < N; i++){
        for (j = 0; j < M; j++){
            for (k = 0; k < P; k++){
                printf("result: %d %d %d || %g %gI | %g %gI | %g %gI\n", i,j,k, creal(out[((i*M+j)*P+k)*DOF]), cimag(out[((i*M+j)*P+k)*DOF]),creal(out[((i*M+j)*P+k)*DOF+1]), cimag(out[((i*M+j)*P+k)*DOF+1]),creal(out[((i*M+j)*P+k)*DOF+2]), cimag(out[((i*M+j)*P+k)*DOF+2]));
            }
        }
    }


    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);

    free(dims);
    free(howmany_dims);

    return(0);
}