Actual source code: ex28.c
1: static char help[] = "Test sequential USFFT interface on a 3-dof field over a uniform DA and compares to the result of FFTW acting on a split version of the field\n\n";
3: /*
4: Compiling the code:
5: This code uses the complex numbers version of PETSc and the FFTW package, so configure
6: must be run to enable these.
8: */
10: #define DOF 3
12: #include petscmat.h
13: #include petscda.h
16: PetscInt main(PetscInt argc,char **args)
17: {
18: typedef enum {RANDOM, CONSTANT, TANH, NUM_FUNCS} FuncType;
19: const char *funcNames[NUM_FUNCS] = {"random", "constant", "tanh"};
20: Mat A, AA;
21: PetscMPIInt size;
22: PetscInt N,i, stencil=1,dof=3;
23: PetscInt dim[3] = {10,10,10}, ndim = 3;
24: Vec coords,x,y,z,xx, yy, zz;
25: Vec xxsplit[DOF], yysplit[DOF], zzsplit[DOF];
26: PetscReal h[3];
27: PetscScalar s;
28: PetscRandom rdm;
29: PetscReal norm, enorm;
30: PetscInt func;
31: FuncType function = TANH;
32: DA da, da1, coordsda;
33: PetscTruth view_x = PETSC_FALSE, view_y = PETSC_FALSE, view_z = PETSC_FALSE;
36: PetscInitialize(&argc,&args,(char *)0,help);
37: #if !defined(PETSC_USE_COMPLEX)
38: SETERRQ(PETSC_ERR_SUP, "This example requires complex numbers");
39: #endif
40: MPI_Comm_size(PETSC_COMM_WORLD, &size);
41: if (size != 1) SETERRQ(PETSC_ERR_SUP, "This is a uniprocessor example only!");
42: PetscOptionsBegin(PETSC_COMM_WORLD, PETSC_NULL, "USFFT Options", "ex27");
43: PetscOptionsEList("-function", "Function type", "ex27", funcNames, NUM_FUNCS, funcNames[function], &func, PETSC_NULL);
44: function = (FuncType) func;
45: PetscOptionsEnd();
46: PetscOptionsGetTruth(PETSC_NULL,"-view_x",&view_x,PETSC_NULL);
47: PetscOptionsGetTruth(PETSC_NULL,"-view_y",&view_y,PETSC_NULL);
48: PetscOptionsGetTruth(PETSC_NULL,"-view_z",&view_z,PETSC_NULL);
49: PetscOptionsGetIntArray(PETSC_NULL,"-dim",dim,&ndim,PETSC_NULL);
51: // DA with the correct fiber dimension
52: DACreate3d(PETSC_COMM_SELF,DA_NONPERIODIC,DA_STENCIL_STAR,
53: dim[0], dim[1], dim[2],
54: PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE,
55: dof, stencil,
56: PETSC_NULL, PETSC_NULL, PETSC_NULL,
57: &da);
58:
59: // DA with fiber dimension 1 for split fields
60: DACreate3d(PETSC_COMM_SELF,DA_NONPERIODIC,DA_STENCIL_STAR,
61: dim[0], dim[1], dim[2],
62: PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE,
63: 1, stencil,
64: PETSC_NULL, PETSC_NULL, PETSC_NULL,
65: &da1);
66:
67:
68: // Coordinates
69: DAGetCoordinateDA(da, &coordsda);
70: DAGetGlobalVector(coordsda, &coords);
71: PetscObjectSetName((PetscObject) coords, "Grid coordinates");
72: for(i = 0, N = 1; i < 3; i++) {
73: h[i] = 1.0/dim[i];
74: PetscScalar *a;
75: VecGetArray(coords, &a);
76: PetscInt j,k,n = 0;
77: for(i = 0; i < 3; ++i) {
78: for(j = 0; j < dim[i]; ++j){
79: for(k = 0; k < 3; ++k) {
80: a[n] = j*h[i]; // coordinate along the j-th point in the i-th dimension
81: ++n;
82: }
83: }
84: }
85: VecRestoreArray(coords, &a);
87: }
88: DASetCoordinates(da, coords);
89: // Work vectors
90: DAGetGlobalVector(da, &x);
91: PetscObjectSetName((PetscObject) x, "Real space vector");
92: DAGetGlobalVector(da, &xx);
93: PetscObjectSetName((PetscObject) xx, "Real space vector");
94: DAGetGlobalVector(da, &y);
95: PetscObjectSetName((PetscObject) y, "USFFT frequency space vector");
96: DAGetGlobalVector(da, &yy);
97: PetscObjectSetName((PetscObject) yy, "FFTW frequency space vector");
98: DAGetGlobalVector(da, &z);
99: PetscObjectSetName((PetscObject) z, "USFFT reconstructed vector");
100: DAGetGlobalVector(da, &zz);
101: PetscObjectSetName((PetscObject) zz, "FFTW reconstructed vector");
102: // Split vectors for FFTW
103: for(int ii = 0; ii < 3; ++ii) {
104: DAGetGlobalVector(da1, &xxsplit[ii]);
105: PetscObjectSetName((PetscObject) xxsplit[ii], "Real space split vector");
106: DAGetGlobalVector(da1, &yysplit[ii]);
107: PetscObjectSetName((PetscObject) yysplit[ii], "FFTW frequency space split vector");
108: DAGetGlobalVector(da1, &zzsplit[ii]);
109: PetscObjectSetName((PetscObject) zzsplit[ii], "FFTW reconstructed split vector");
110: }
113: PetscPrintf(PETSC_COMM_SELF, "%3-D: USFFT on vector of ");
114: for(i = 0, N = 1; i < 3; i++) {
115: PetscPrintf(PETSC_COMM_SELF, "dim[%d] = %d ",i,dim[i]);
116: N *= dim[i];
117: }
118: PetscPrintf(PETSC_COMM_SELF, "; total size %d \n",N);
120:
121: if (function == RANDOM) {
122: PetscRandomCreate(PETSC_COMM_SELF, &rdm);
123: PetscRandomSetFromOptions(rdm);
124: VecSetRandom(x, rdm);
125: PetscRandomDestroy(rdm);
126: }
127: else if (function == CONSTANT) {
128: VecSet(x, 1.0);
129: }
130: else if (function == TANH) {
131: PetscScalar *a;
132: VecGetArray(x, &a);
133: PetscInt j,k = 0;
134: for(i = 0; i < 3; ++i) {
135: for(j = 0; j < dim[i]; ++j) {
136: a[k] = tanh((j - dim[i]/2.0)*(10.0/dim[i]));
137: ++k;
138: }
139: }
140: VecRestoreArray(x, &a);
141: }
142: if(view_x) {
143: VecView(x, PETSC_VIEWER_STDOUT_WORLD);
144: }
145: VecCopy(x,xx);
146: // Split xx
147: VecStrideGatherAll(xx,xxsplit, INSERT_VALUES); //YES! 'Gather' means 'split' (or maybe 'scatter'?)!
149: VecNorm(x,NORM_2,&norm);
150: PetscPrintf(PETSC_COMM_SELF, "|x|_2 = %g\n",norm);
151:
152: /* create USFFT object */
153: MatCreateSeqUSFFT(da,da,&A);
154: /* create FFTW object */
155: MatCreateSeqFFTW(PETSC_COMM_SELF,3,dim,&AA);
156:
157: /* apply USFFT and FFTW FORWARD "preemptively", so the fftw_plans can be reused on different vectors */
158: MatMult(A,x,z);
159: for(int ii = 0; ii < 3; ++ii) {
160: MatMult(AA,xxsplit[ii],zzsplit[ii]);
161: }
162: // Now apply USFFT and FFTW forward several (3) times
163: for (i=0; i<3; ++i){
164: MatMult(A,x,y);
165: for(int ii = 0; ii < 3; ++ii) {
166: MatMult(AA,xxsplit[ii],yysplit[ii]);
167: }
168: MatMultTranspose(A,y,z);
169: for(int ii = 0; ii < 3; ++ii) {
170: MatMult(AA,yysplit[ii],zzsplit[ii]);
171: }
172: }
173: // Unsplit yy
174: VecStrideScatterAll(yysplit, yy, INSERT_VALUES); //YES! 'Scatter' means 'collect' (or maybe 'gather'?)!
175: // Unsplit zz
176: VecStrideScatterAll(zzsplit, zz, INSERT_VALUES); //YES! 'Scatter' means 'collect' (or maybe 'gather'?)!
178: if(view_y) {
179: PetscPrintf(PETSC_COMM_WORLD, "y = \n");
180: VecView(y, PETSC_VIEWER_STDOUT_WORLD);
181: PetscPrintf(PETSC_COMM_WORLD, "yy = \n");
182: VecView(yy, PETSC_VIEWER_STDOUT_WORLD);
183: }
184:
185: if(view_z) {
186: PetscPrintf(PETSC_COMM_WORLD, "z = \n");
187: VecView(z, PETSC_VIEWER_STDOUT_WORLD);
188: PetscPrintf(PETSC_COMM_WORLD, "zz = \n");
189: VecView(zz, PETSC_VIEWER_STDOUT_WORLD);
190: }
191:
192: /* compare x and z. USFFT computes an unnormalized DFT, thus z = N*x */
193: s = 1.0/(PetscReal)N;
194: VecScale(z,s);
195: VecAXPY(x,-1.0,z);
196: VecNorm(x,NORM_1,&enorm);
197: PetscPrintf(PETSC_COMM_SELF, "|x-z| = %g\n",enorm);
199: /* compare xx and zz. FFTW computes an unnormalized DFT, thus zz = N*x */
200: s = 1.0/(PetscReal)N;
201: VecScale(zz,s);
202: VecAXPY(xx,-1.0,zz);
203: VecNorm(xx,NORM_1,&enorm);
204: PetscPrintf(PETSC_COMM_SELF, "|xx-zz| = %g\n",enorm);
206: /* compare y and yy: USFFT and FFTW results*/
207: VecNorm(y,NORM_2,&norm);
208: VecAXPY(y,-1.0,yy);
209: VecNorm(y,NORM_1,&enorm);
210: PetscPrintf(PETSC_COMM_SELF, "|y|_2 = %g\n",norm);
211: PetscPrintf(PETSC_COMM_SELF, "|y-yy| = %g\n",enorm);
212:
213: /* compare z and zz: USFFT and FFTW results*/
214: VecNorm(z,NORM_2,&norm);
215: VecAXPY(z,-1.0,zz);
216: VecNorm(z,NORM_1,&enorm);
217: PetscPrintf(PETSC_COMM_SELF, "|z|_2 = %g\n",norm);
218: PetscPrintf(PETSC_COMM_SELF, "|z-zz| = %g\n",enorm);
219:
221: /* free spaces */
222: DARestoreGlobalVector(da,&x);
223: DARestoreGlobalVector(da,&xx);
224: DARestoreGlobalVector(da,&y);
225: DARestoreGlobalVector(da,&yy);
226: DARestoreGlobalVector(da,&z);
227: DARestoreGlobalVector(da,&zz);
229: PetscFinalize();
230: return 0;
231: }