xref: /petsc/src/mat/tests/ex155.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static char help[] = "This program illustrates the use of PETSc-fftw interface for parallel real DFT\n";
2 #include <petscmat.h>
3 #include <fftw3-mpi.h>
4 /*extern PetscErrorCode MatCreateVecsFFT(Mat,Vec *,Vec *,Vec *);*/
5 int main(int argc, char **args) {
6   PetscMPIInt rank, size;
7   PetscInt    N0 = 4096, N1 = 4096, N2 = 256, N3 = 10, N4 = 10, N = N0 * N1;
8   PetscRandom rdm;
9   PetscReal   enorm;
10   Vec         x, y, z, input, output;
11   Mat         A;
12   PetscInt    DIM, dim[5], vsize, row, col;
13   PetscReal   fac;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
17   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
18   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
19 
20 #if defined(PETSC_USE_COMPLEX)
21   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Example for Real DFT. Your current data type is complex!");
22 #endif
23   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
24   PetscCall(PetscRandomSetFromOptions(rdm));
25   PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
26   PetscCall(VecSetSizes(input, PETSC_DECIDE, N));
27   PetscCall(VecSetFromOptions(input));
28   PetscCall(VecSetRandom(input, rdm));
29   PetscCall(VecDuplicate(input, &output));
30 
31   DIM    = 2;
32   dim[0] = N0;
33   dim[1] = N1;
34   dim[2] = N2;
35   dim[3] = N3;
36   dim[4] = N4;
37   PetscCall(MatCreateFFT(PETSC_COMM_WORLD, DIM, dim, MATFFTW, &A));
38   PetscCall(MatGetLocalSize(A, &row, &col));
39   printf("The Matrix size  is %d and %d from process %d\n", row, col, rank);
40   PetscCall(MatCreateVecsFFTW(A, &x, &y, &z));
41 
42   PetscCall(VecGetSize(x, &vsize));
43 
44   PetscCall(VecGetSize(z, &vsize));
45   printf("The vector size of output from the main routine is %d\n", vsize);
46 
47   PetscCall(VecScatterPetscToFFTW(A, input, x));
48   /*PetscCall(VecDestroy(&input));*/
49   PetscCall(MatMult(A, x, y));
50   PetscCall(MatMultTranspose(A, y, z));
51   PetscCall(VecScatterFFTWToPetsc(A, z, output));
52   /*PetscCall(VecDestroy(&z));*/
53   fac = 1.0 / (PetscReal)N;
54   PetscCall(VecScale(output, fac));
55 
56   PetscCall(VecAssemblyBegin(input));
57   PetscCall(VecAssemblyEnd(input));
58   PetscCall(VecAssemblyBegin(output));
59   PetscCall(VecAssemblyEnd(output));
60 
61   /*  PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD));*/
62   /*  PetscCall(VecView(output,PETSC_VIEWER_STDOUT_WORLD));*/
63 
64   PetscCall(VecAXPY(output, -1.0, input));
65   PetscCall(VecNorm(output, NORM_1, &enorm));
66   if (enorm > 1.e-14) { PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Error norm of |x - z| %e\n", enorm)); }
67 
68   PetscCall(VecDestroy(&x));
69   PetscCall(VecDestroy(&y));
70   PetscCall(VecDestroy(&z));
71   PetscCall(VecDestroy(&output));
72   PetscCall(VecDestroy(&input));
73   PetscCall(MatDestroy(&A));
74   PetscCall(PetscRandomDestroy(&rdm));
75   PetscCall(PetscFinalize());
76   return 0;
77 }
78