Actual source code: ex157.c
petsc-3.14.6 2021-03-30
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: PetscInt main(PetscInt argc,char **args)
5: {
7: PetscMPIInt rank,size;
8: PetscInt N0=2048,N1=2048,N2=3,N3=5,N4=5,N=N0*N1;
9: PetscRandom rdm;
10: PetscReal enorm;
11: Vec x,y,z,input,output;
12: Mat A;
13: PetscInt DIM, dim[5],vsize;
14: PetscReal fac;
15: PetscScalar one=1,two=2,three=3;
17: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
18: #if defined(PETSC_USE_COMPLEX)
19: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers");
20: #endif
21: MPI_Comm_size(PETSC_COMM_WORLD, &size);
22: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
24: PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
25: PetscRandomSetFromOptions(rdm);
26: VecCreate(PETSC_COMM_WORLD,&input);
27: VecSetSizes(input,PETSC_DECIDE,N);
28: VecSetFromOptions(input);
29: /* VecSet(input,one); */
30: /* VecSetValue(input,1,two,INSERT_VALUES); */
31: /* VecSetValue(input,2,three,INSERT_VALUES); */
32: /* VecSetValue(input,3,three,INSERT_VALUES); */
33: VecSetRandom(input,rdm);
34: /* VecSetRandom(input,rdm); */
35: /* VecSetRandom(input,rdm); */
36: VecDuplicate(input,&output);
38: DIM = 2; dim[0] = N0; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;
39: MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A);
40: MatCreateVecsFFTW(A,&x,&y,&z);
41: /* MatCreateVecs(A,&x,&y); */
42: /* MatCreateVecs(A,&z,NULL); */
44: VecGetSize(x,&vsize);
45: printf("The vector size of input from the main routine is %d\n",vsize);
47: VecGetSize(z,&vsize);
48: printf("The vector size of output from the main routine is %d\n",vsize);
50: InputTransformFFT(A,input,x);
52: MatMult(A,x,y);
53: VecAssemblyBegin(y);
54: VecAssemblyEnd(y);
55: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
57: MatMultTranspose(A,y,z);
59: OutputTransformFFT(A,z,output);
60: fac = 1.0/(PetscReal)N;
61: VecScale(output,fac);
63: VecAssemblyBegin(input);
64: VecAssemblyEnd(input);
65: VecAssemblyBegin(output);
66: VecAssemblyEnd(output);
68: /* VecView(input,PETSC_VIEWER_STDOUT_WORLD); */
69: /* VecView(output,PETSC_VIEWER_STDOUT_WORLD); */
71: VecAXPY(output,-1.0,input);
72: VecNorm(output,NORM_1,&enorm);
73: /* if (enorm > 1.e-14) { */
74: PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %e\n",enorm);
75: /* } */
77: VecDestroy(&output);
78: VecDestroy(&input);
79: VecDestroy(&x);
80: VecDestroy(&y);
81: VecDestroy(&z);
82: MatDestroy(&A);
83: PetscRandomDestroy(&rdm);
84: PetscFinalize();
85: return ierr;
87: }