xref: /petsc/src/mat/impls/fft/fft.c (revision 2f6eced2a19e978d64f27de66fbc6c26cc5c7934)
1 
2 /*
3     Provides an interface to the FFT packages.
4 */
5 
6 #include <../src/mat/impls/fft/fft.h>   /*I "petscmat.h" I*/
7 
8 PetscErrorCode MatDestroy_FFT(Mat A)
9 {
10   PetscErrorCode ierr;
11   Mat_FFT        *fft = (Mat_FFT*)A->data;
12 
13   PetscFunctionBegin;
14   if (fft->matdestroy) {
15     ierr = (fft->matdestroy)(A);CHKERRQ(ierr);
16   }
17   ierr = PetscFree(fft->dim);CHKERRQ(ierr);
18   ierr = PetscFree(A->data);CHKERRQ(ierr);
19   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
20   PetscFunctionReturn(0);
21 }
22 
23 /*@C
24       MatCreateFFT - Creates a matrix object that provides FFT via an external package
25 
26    Collective on MPI_Comm
27 
28    Input Parameter:
29 +   comm - MPI communicator
30 .   ndim - the ndim-dimensional transform
31 .   dim - array of size ndim, dim[i] contains the vector length in the i-dimension
32 -   type - package type, e.g., FFTW or FFTCU
33 
34    Output Parameter:
35 .   A  - the matrix
36 
37   Options Database Keys:
38 + -mat_fft_type - set FFT type
39 
40    Level: intermediate
41 
42 @*/
43 PetscErrorCode MatCreateFFT(MPI_Comm comm,PetscInt ndim,const PetscInt dim[],MatType mattype,Mat *A)
44 {
45   PetscErrorCode ierr;
46   PetscMPIInt    size;
47   Mat            FFT;
48   PetscInt       N,i;
49   Mat_FFT        *fft;
50 
51   PetscFunctionBegin;
52   if (ndim < 1) SETERRQ1(comm,PETSC_ERR_USER,"ndim %d must be > 0",ndim);
53   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
54 
55   ierr      = MatCreate(comm,&FFT);CHKERRQ(ierr);
56   ierr      = PetscNewLog(FFT,&fft);CHKERRQ(ierr);
57   FFT->data = (void*)fft;
58   N         = 1;
59   for (i=0; i<ndim; i++) {
60     if (dim[i] < 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"dim[%d]=%d must be > 0",i,dim[i]);
61     N *= dim[i];
62   }
63 
64   ierr = PetscMalloc1(ndim,&fft->dim);CHKERRQ(ierr);
65   ierr = PetscMemcpy(fft->dim,dim,ndim*sizeof(PetscInt));CHKERRQ(ierr);
66 
67   fft->ndim = ndim;
68   fft->n    = PETSC_DECIDE;
69   fft->N    = N;
70   fft->data = NULL;
71 
72   ierr = MatSetType(FFT,mattype);CHKERRQ(ierr);
73 
74   FFT->ops->destroy = MatDestroy_FFT;
75 
76   /* get runtime options */
77   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)FFT),((PetscObject)FFT)->prefix,"FFT Options","Mat");CHKERRQ(ierr);
78   PetscOptionsEnd();
79 
80   *A = FFT;
81   PetscFunctionReturn(0);
82 }
83