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