xref: /petsc/src/mat/tests/ex147.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 /* This program illustrates use of parallel real FFT */
2 static char help[] = "This program illustrates the use of parallel real multi-dimensional fftw (without PETSc interface)";
3 #include <petscmat.h>
4 #include <fftw3.h>
5 #include <fftw3-mpi.h>
6 
7 int main(int argc, char **args) {
8   ptrdiff_t     N0 = 2, N1 = 2, N2 = 2, N3 = 2, dim[4], N, D;
9   fftw_plan     bplan, fplan;
10   fftw_complex *out;
11   double       *in1, *in2;
12   ptrdiff_t     alloc_local, local_n0, local_0_start;
13   ptrdiff_t     local_n1, local_1_start;
14   PetscInt      i, j, indx[100], n1;
15   PetscInt      size, rank, n, *in, N_factor;
16   PetscScalar  *data_fin, value1, one = 1.0, zero = 0.0;
17   PetscScalar   a, *x_arr, *y_arr, *z_arr, enorm;
18   Vec           fin, fout, fout1, x, y;
19   PetscRandom   rnd;
20 
21   PetscFunctionBeginUser;
22   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
23 #if defined(PETSC_USE_COMPLEX)
24   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers. Your current scalar type is complex");
25 #endif
26   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
27   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
28 
29   PetscRandomCreate(PETSC_COMM_WORLD, &rnd);
30   D      = 4;
31   dim[0] = N0;
32   dim[1] = N1;
33   dim[2] = N2;
34   dim[3] = N3 / 2 + 1;
35 
36   alloc_local = fftw_mpi_local_size_transposed(D, dim, PETSC_COMM_WORLD, &local_n0, &local_0_start, &local_n1, &local_1_start);
37 
38   printf("The value alloc_local is %ld from process %d\n", alloc_local, rank);
39   printf("The value local_n0 is %ld from process %d\n", local_n0, rank);
40   printf("The value local_0_start is  %ld from process %d\n", local_0_start, rank);
41   printf("The value local_n1 is  %ld from process %d\n", local_n1, rank);
42   printf("The value local_1_start is  %ld from process %d\n", local_1_start, rank);
43 
44   /* Allocate space for input and output arrays  */
45 
46   in1 = (double *)fftw_malloc(sizeof(double) * alloc_local * 2);
47   in2 = (double *)fftw_malloc(sizeof(double) * alloc_local * 2);
48   out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * alloc_local);
49 
50   N        = 2 * N0 * N1 * N2 * (N3 / 2 + 1);
51   N_factor = N0 * N1 * N2 * N3;
52   n        = 2 * local_n0 * N1 * N2 * (N3 / 2 + 1);
53   n1       = local_n1 * N0 * 2 * N1 * N2;
54 
55   /*    printf("The value N is  %d from process %d\n",N,rank); */
56   /*    printf("The value n is  %d from process %d\n",n,rank); */
57   /*    printf("The value n1 is  %d from process %d\n",n1,rank); */
58   /* Creating data vector and accompanying array with VeccreateMPIWithArray */
59   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, n, N, (PetscScalar *)in1, &fin));
60   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, n, N, (PetscScalar *)out, &fout));
61   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, n, N, (PetscScalar *)in2, &fout1));
62 
63   /*    VecGetSize(fin,&size); */
64   /*    printf("The size is %d\n",size); */
65 
66   VecSet(fin, one);
67   /*    VecAssemblyBegin(fin); */
68   /*    VecAssemblyEnd(fin); */
69   /*    VecView(fin,PETSC_VIEWER_STDOUT_WORLD); */
70 
71   VecGetArray(fin, &x_arr);
72   VecGetArray(fout1, &z_arr);
73   VecGetArray(fout, &y_arr);
74 
75   dim[3] = N3;
76 
77   fplan = fftw_mpi_plan_dft_r2c(D, dim, (double *)x_arr, (fftw_complex *)y_arr, PETSC_COMM_WORLD, FFTW_ESTIMATE);
78   bplan = fftw_mpi_plan_dft_c2r(D, dim, (fftw_complex *)y_arr, (double *)z_arr, PETSC_COMM_WORLD, FFTW_ESTIMATE);
79 
80   fftw_execute(fplan);
81   fftw_execute(bplan);
82 
83   VecRestoreArray(fin, &x_arr);
84   VecRestoreArray(fout1, &z_arr);
85   VecRestoreArray(fout, &y_arr);
86 
87   /*    a = 1.0/(PetscReal)N_factor; */
88   /*    PetscCall(VecScale(fout1,a)); */
89 
90   VecAssemblyBegin(fout1);
91   VecAssemblyEnd(fout1);
92 
93   VecView(fout1, PETSC_VIEWER_STDOUT_WORLD);
94 
95   fftw_destroy_plan(fplan);
96   fftw_destroy_plan(bplan);
97   fftw_free(in1);
98   PetscCall(VecDestroy(&fin));
99   fftw_free(out);
100   PetscCall(VecDestroy(&fout));
101   fftw_free(in2);
102   PetscCall(VecDestroy(&fout1));
103 
104   PetscCall(PetscFinalize());
105   return 0;
106 }
107