Actual source code: matusfft.c


  2: /*
  3:     Provides an implementation of the Unevenly Sampled FFT algorithm as a Mat.
  4:     Testing examples can be found in ~/src/mat/tests FIX: should these be moved to dm/da/tests?
  5: */

  7: #include <petsc/private/matimpl.h>
  8: #include <petscdmda.h>
  9: #include <fftw3.h>

 11: typedef struct {
 12:   PetscInt  dim;
 13:   Vec       sampleCoords;
 14:   PetscInt  dof;
 15:   DM        freqDA;            /* frequency DMDA */
 16:   PetscInt  *freqSizes;        /* sizes of the frequency DMDA, one per each dim */
 17:   DM        resampleDa;        /* the Battle-Lemarie interpolant DMDA */
 18:   Vec       resample;          /* Vec of samples, one per dof per sample point */
 19:   fftw_plan p_forward,p_backward;
 20:   unsigned  p_flag;      /* planner flags, FFTW_ESTIMATE,FFTW_MEASURE, FFTW_PATIENT, FFTW_EXHAUSTIVE */
 21: } Mat_USFFT;

 23: PetscErrorCode MatApply_USFFT_Private(Mat A, fftw_plan *plan, int direction, Vec x,Vec y)
 24: {
 25: #if 0
 27:   PetscScalar    *r_array, *y_array;
 28:   Mat_USFFT* = (Mat_USFFT*)(A->data);
 29: #endif

 32: #if 0
 33:   /* resample x to usfft->resample */
 34:   MatResample_USFFT_Private(A, x);

 36:   /* NB: for now we use outdim for both x and y; this will change once a full USFFT is implemented */
 37:   VecGetArray(usfft->resample,&r_array);
 38:   VecGetArray(y,&y_array);
 39:   if (!*plan) { /* create a plan then execute it*/
 40:     if (usfft->dof == 1) {
 41: #if defined(PETSC_DEBUG_USFFT)
 42:       PetscPrintf(PetscObjectComm((PetscObject)A), "direction = %d, usfft->ndim = %d\n", direction, usfft->ndim);
 43:       for (int ii = 0; ii < usfft->ndim; ++ii) {
 44:         PetscPrintf(PetscObjectComm((PetscObject)A), "usfft->outdim[%d] = %d\n", ii, usfft->outdim[ii]);
 45:       }
 46: #endif

 48:       switch (usfft->dim) {
 49:       case 1:
 50:         *plan = fftw_plan_dft_1d(usfft->outdim[0],(fftw_complex*)x_array,(fftw_complex*)y_array,direction,usfft->p_flag);
 51:         break;
 52:       case 2:
 53:         *plan = fftw_plan_dft_2d(usfft->outdim[0],usfft->outdim[1],(fftw_complex*)x_array,(fftw_complex*)y_array,direction,usfft->p_flag);
 54:         break;
 55:       case 3:
 56:         *plan = fftw_plan_dft_3d(usfft->outdim[0],usfft->outdim[1],usfft->outdim[2],(fftw_complex*)x_array,(fftw_complex*)y_array,direction,usfft->p_flag);
 57:         break;
 58:       default:
 59:         *plan = fftw_plan_dft(usfft->ndim,usfft->outdim,(fftw_complex*)x_array,(fftw_complex*)y_array,direction,usfft->p_flag);
 60:         break;
 61:       }
 62:       fftw_execute(*plan);
 63:     } /* if (dof == 1) */
 64:     else { /* if (dof > 1) */
 65:       *plan = fftw_plan_many_dft(/*rank*/usfft->ndim, /*n*/usfft->outdim, /*howmany*/usfft->dof,
 66:                                  (fftw_complex*)x_array, /*nembed*/usfft->outdim, /*stride*/usfft->dof, /*dist*/1,
 67:                                  (fftw_complex*)y_array, /*nembed*/usfft->outdim, /*stride*/usfft->dof, /*dist*/1,
 68:                                  /*sign*/direction, /*flags*/usfft->p_flag);
 69:       fftw_execute(*plan);
 70:     } /* if (dof > 1) */
 71:   } /* if (!*plan) */
 72:   else {  /* if (*plan) */
 73:     /* use existing plan */
 74:     fftw_execute_dft(*plan,(fftw_complex*)x_array,(fftw_complex*)y_array);
 75:   }
 76:   VecRestoreArray(y,&y_array);
 77:   VecRestoreArray(x,&x_array);
 78: #endif
 79:   return(0);
 80: } /* MatApply_USFFT_Private() */

 82: #if 0
 83: PetscErrorCode MatUSFFT_ProjectOnBattleLemarie_Private(Vec x,double *r)
 84: /* Project onto the Battle-Lemarie function centered around r */
 85: {
 87:   PetscScalar    *x_array, *y_array;

 90:   return(0);
 91: } /* MatUSFFT_ProjectOnBattleLemarie_Private() */

 93: PetscErrorCode MatInterpolate_USFFT_Private(Vec x,Vec y)
 94: {
 96:   PetscScalar    *x_array, *y_array;

 99:   return(0);
100: } /* MatInterpolate_USFFT_Private() */

102: PetscErrorCode MatMult_SeqUSFFT(Mat A,Vec x,Vec y)
103: {
105:   Mat_USFFT      *usfft = (Mat_USFFT*)A->data;

108:   /* NB: for now we use outdim for both x and y; this will change once a full USFFT is implemented */
109:   MatApply_USFFT_Private(A, &usfft->p_forward, FFTW_FORWARD, x,y);
110:   return(0);
111: }

113: PetscErrorCode MatMultTranspose_SeqUSFFT(Mat A,Vec x,Vec y)
114: {
116:   Mat_USFFT      *usfft = (Mat_USFFT*)A->data;

119:   /* NB: for now we use outdim for both x and y; this will change once a full USFFT is implemented */
120:   MatApply_USFFT_Private(usfft, &usfft->p_backward, FFTW_BACKWARD, x,y);
121:   return(0);
122: }

124: PetscErrorCode MatDestroy_SeqUSFFT(Mat A)
125: {
126:   Mat_USFFT      *usfft = (Mat_USFFT*)A->data;

130:   fftw_destroy_plan(usfft->p_forward);
131:   fftw_destroy_plan(usfft->p_backward);
132:   PetscFree(usfft->indim);
133:   PetscFree(usfft->outdim);
134:   PetscFree(usfft);
135:   PetscObjectChangeTypeName((PetscObject)A,0);
136:   return(0);
137: } /* MatDestroy_SeqUSFFT() */

139: /*@C
140:       MatCreateSeqUSFFT - Creates a matrix object that provides sequential USFFT
141:   via the external package FFTW

143:    Collective

145:    Input Parameter:
146: .   da - geometry of the domain encoded by a DMDA

148:    Output Parameter:
149: .   A  - the matrix

151:   Options Database Keys:
152: . -mat_usfft_plannerflags - set the FFTW planner flags

154:    Level: intermediate

156: @*/
157: PetscErrorCode  MatCreateSeqUSFFT(Vec sampleCoords, DMDA freqDA, Mat *A)
158: {
160:   Mat_USFFT      *usfft;
161:   PetscInt       m,n,M,N,i;
162:   const char     *p_flags[]={"FFTW_ESTIMATE","FFTW_MEASURE","FFTW_PATIENT","FFTW_EXHAUSTIVE"};
163:   PetscBool      flg;
164:   PetscInt       p_flag;
165:   PetscInt       dof, dim, freqSizes[3];
166:   MPI_Comm       comm;
167:   PetscInt       size;

170:   PetscObjectGetComm((PetscObject)inda, &comm);
171:   MPI_Comm_size(comm, &size);
172:   if (size > 1) SETERRQ(comm,PETSC_ERR_USER, "Parallel DMDA (in) not yet supported by USFFT");
173:   PetscObjectGetComm((PetscObject)outda, &comm);
174:   MPI_Comm_size(comm, &size);
175:   if (size > 1) SETERRQ(comm,PETSC_ERR_USER, "Parallel DMDA (out) not yet supported by USFFT");
176:   MatCreate(comm,A);
177:   PetscNewLog(*A,&usfft);
178:   (*A)->data   = (void*)usfft;
179:   usfft->inda  = inda;
180:   usfft->outda = outda;
181:   /* inda */
182:   DMDAGetInfo(usfft->inda, &ndim, dim+0, dim+1, dim+2, NULL, NULL, NULL, &dof, NULL, NULL, NULL);
183:   if (ndim <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"ndim %d must be > 0",ndim);
184:   if (dof <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"dof %d must be > 0",dof);
185:   usfft->ndim   = ndim;
186:   usfft->dof    = dof;
187:   usfft->freqDA = freqDA;
188:   /* NB: we reverse the freq and resample DMDA sizes, since the DMDA ordering (natural on x-y-z, with x varying the fastest)
189:      is the order opposite of that assumed by FFTW: z varying the fastest */
190:   PetscMalloc1(usfft->ndim+1,&usfft->indim);
191:   for (i = usfft->ndim; i > 0; --i) usfft->indim[usfft->ndim-i] = dim[i-1];

193:   /* outda */
194:   DMDAGetInfo(usfft->outda, &ndim, dim+0, dim+1, dim+2, NULL, NULL, NULL, &dof, NULL, NULL, NULL);
195:   if (ndim != usfft->ndim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"in and out DMDA dimensions must match: %d != %d",usfft->ndim, ndim);
196:   if (dof != usfft->dof) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"in and out DMDA dof must match: %d != %d",usfft->dof, dof);
197:   /* Store output dimensions */
198:   /* NB: we reverse the DMDA dimensions, since the DMDA ordering (natural on x-y-z, with x varying the fastest)
199:      is the order opposite of that assumed by FFTW: z varying the fastest */
200:   PetscMalloc1(usfft->ndim+1,&usfft->outdim);
201:   for (i = usfft->ndim; i > 0; --i) usfft->outdim[usfft->ndim-i] = dim[i-1];

203:   /* TODO: Use the new form of DMDACreate() */
204: #if 0
205:   DMDACreate(comm,usfft->dim, DMDA_NONPERIODIC, DMDA_STENCIL_STAR, usfft->freqSizes[0], usfft->freqSizes[1], usfft->freqSizes[2],
206:                     PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, dof, 0, NULL, NULL, NULL,  0, &(usfft->resampleDA));
207: #endif
208:   DMDAGetVec(usfft->resampleDA, usfft->resample);

210:   /* CONTINUE: Need to build the connectivity "Sieve" attaching sample points to the resample points they are close to */

212:   /* CONTINUE: recalculate matrix sizes based on the connectivity "Sieve" */
213:   /* mat sizes */
214:   m = 1; n = 1;
215:   for (i=0; i<usfft->ndim; i++) {
216:     if (usfft->indim[i] <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"indim[%d]=%d must be > 0",i,usfft->indim[i]);
217:     if (usfft->outdim[i] <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"outdim[%d]=%d must be > 0",i,usfft->outdim[i]);
218:     n *= usfft->indim[i];
219:     m *= usfft->outdim[i];
220:   }
221:   N        = n*usfft->dof;
222:   M        = m*usfft->dof;
223:   MatSetSizes(*A,M,N,M,N); /* "in size" is the number of columns, "out size" is the number of rows" */
224:   PetscObjectChangeTypeName((PetscObject)*A,MATSEQUSFFT);
225:   usfft->m = m; usfft->n = n; usfft->M = M; usfft->N = N;
226:   /* FFTW */
227:   usfft->p_forward  = 0;
228:   usfft->p_backward = 0;
229:   usfft->p_flag     = FFTW_ESTIMATE;
230:   /* set Mat ops */
231:   (*A)->ops->mult          = MatMult_SeqUSFFT;
232:   (*A)->ops->multtranspose = MatMultTranspose_SeqUSFFT;
233:   (*A)->assembled          = PETSC_TRUE;
234:   (*A)->ops->destroy       = MatDestroy_SeqUSFFT;
235:   /* get runtime options */
236:   PetscOptionsBegin(((PetscObject)(*A))->comm,((PetscObject)(*A))->prefix,"USFFT Options","Mat");
237:   PetscOptionsEList("-mat_usfft_fftw_plannerflags","Planner Flags","None",p_flags,4,p_flags[0],&p_flag,&flg);
238:   if (flg) usfft->p_flag = (unsigned)p_flag;
239:   PetscOptionsEnd();
240:   return(0);
241: } /* MatCreateSeqUSFFT() */

243: #endif