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