Actual source code: ex157.c
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: int main(int argc, char **args)
5: {
6: PetscMPIInt rank, size;
7: PetscInt N0 = 2048, N1 = 2048, N2 = 3, N3 = 5, N4 = 5, 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;
13: PetscReal fac;
14: PetscScalar one = 1, two = 2, three = 3;
16: PetscFunctionBeginUser;
17: PetscCall(PetscInitialize(&argc, &args, NULL, help));
18: PetscCheck(!PetscDefined(USE_COMPLEX), PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");
19: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
20: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
22: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
23: PetscCall(PetscRandomSetFromOptions(rdm));
24: PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
25: PetscCall(VecSetSizes(input, PETSC_DECIDE, N));
26: PetscCall(VecSetFromOptions(input));
27: /* PetscCall(VecSet(input,one)); */
28: /* PetscCall(VecSetValue(input,1,two,INSERT_VALUES)); */
29: /* PetscCall(VecSetValue(input,2,three,INSERT_VALUES)); */
30: /* PetscCall(VecSetValue(input,3,three,INSERT_VALUES)); */
31: PetscCall(VecSetRandom(input, rdm));
32: /* PetscCall(VecSetRandom(input,rdm)); */
33: /* PetscCall(VecSetRandom(input,rdm)); */
34: PetscCall(VecDuplicate(input, &output));
36: DIM = 2;
37: dim[0] = N0;
38: dim[1] = N1;
39: dim[2] = N2;
40: dim[3] = N3;
41: dim[4] = N4;
42: PetscCall(MatCreateFFT(PETSC_COMM_WORLD, DIM, dim, MATFFTW, &A));
43: PetscCall(MatCreateVecsFFTW(A, &x, &y, &z));
44: /* PetscCall(MatCreateVecs(A,&x,&y)); */
45: /* PetscCall(MatCreateVecs(A,&z,NULL)); */
47: PetscCall(VecGetSize(x, &vsize));
48: printf("The vector size of input from the main routine is %d\n", vsize);
50: PetscCall(VecGetSize(z, &vsize));
51: printf("The vector size of output from the main routine is %d\n", vsize);
53: PetscCall(InputTransformFFT(A, input, x));
55: PetscCall(MatMult(A, x, y));
56: PetscCall(VecAssemblyBegin(y));
57: PetscCall(VecAssemblyEnd(y));
58: PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD));
60: PetscCall(MatMultTranspose(A, y, z));
62: PetscCall(OutputTransformFFT(A, z, output));
63: fac = 1.0 / (PetscReal)N;
64: PetscCall(VecScale(output, fac));
66: PetscCall(VecAssemblyBegin(input));
67: PetscCall(VecAssemblyEnd(input));
68: PetscCall(VecAssemblyBegin(output));
69: PetscCall(VecAssemblyEnd(output));
71: /* PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD)); */
72: /* PetscCall(VecView(output,PETSC_VIEWER_STDOUT_WORLD)); */
74: PetscCall(VecAXPY(output, -1.0, input));
75: PetscCall(VecNorm(output, NORM_1, &enorm));
76: /* if (enorm > 1.e-14) { */
77: PetscCall(PetscPrintf(PETSC_COMM_SELF, " Error norm of |x - z| %e\n", enorm));
78: /* } */
80: PetscCall(VecDestroy(&output));
81: PetscCall(VecDestroy(&input));
82: PetscCall(VecDestroy(&x));
83: PetscCall(VecDestroy(&y));
84: PetscCall(VecDestroy(&z));
85: PetscCall(MatDestroy(&A));
86: PetscCall(PetscRandomDestroy(&rdm));
87: PetscCall(PetscFinalize());
88: return 0;
89: }