xref: /petsc/src/mat/impls/fft/fft.c (revision 7d0a6c19129e7069c8a40e210b34ed62989173db)
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(fft);CHKERRQ(ierr);
21   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
22   PetscFunctionReturn(0);
23 }
24 
25 #undef __FUNCT__
26 #define __FUNCT__ "MatCreateFFT"
27 /*@
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[],const 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,Mat_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 = PetscMalloc(ndim*sizeof(PetscInt),&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 = PETSC_NULL;
75 
76   ierr = MatSetType(FFT,mattype);CHKERRQ(ierr);
77   FFT->ops->destroy = MatDestroy_FFT;
78 
79   /* get runtime options */
80   ierr = PetscOptionsBegin(((PetscObject)FFT)->comm,((PetscObject)FFT)->prefix,"FFT Options","Mat");CHKERRQ(ierr);
81   PetscOptionsEnd();
82 
83   *A = FFT;
84   PetscFunctionReturn(0);
85 }
86