xref: /petsc/src/mat/utils/gcreate.c (revision 38baddfdda9fcd85a080f141519fb97fb01bc0a4)
17807a1faSBarry Smith 
2273d9f13SBarry Smith #include "src/mat/matimpl.h"       /*I "petscmat.h"  I*/
3325e03aeSBarry Smith #include "petscsys.h"
47807a1faSBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
64a2ae208SSatish Balay #define __FUNCT__ "MatPublish_Base"
76849ba73SBarry Smith static PetscErrorCode MatPublish_Base(PetscObject obj)
835d8aa7fSBarry Smith {
935d8aa7fSBarry Smith   PetscFunctionBegin;
1035d8aa7fSBarry Smith   PetscFunctionReturn(0);
1135d8aa7fSBarry Smith }
1235d8aa7fSBarry Smith 
1335d8aa7fSBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
154a2ae208SSatish Balay #define __FUNCT__ "MatCreate"
16325ab940SBarry Smith /*@C
1769dd0797SLois Curfman McInnes    MatCreate - Creates a matrix where the type is determined
187e5f4302SBarry Smith    from either a call to MatSetType() or from the options database
197e5f4302SBarry Smith    with a call to MatSetFromOptions(). The default matrix type is
207e5f4302SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ()
217e5f4302SBarry Smith    if you do not set a type in the options database. If you never
227e5f4302SBarry Smith    call MatSetType() or MatSetFromOptions() it will generate an
23f8ab6608SSatish Balay    error when you try to use the matrix.
2483e1b59cSLois Curfman McInnes 
25cb13003dSBarry Smith    Collective on MPI_Comm
26cb13003dSBarry Smith 
277807a1faSBarry Smith    Input Parameters:
2882b900a8SBarry Smith +  m - number of local rows (or PETSC_DECIDE)
2982b900a8SBarry Smith .  n - number of local columns (or PETSC_DECIDE)
3082b900a8SBarry Smith .  M - number of global rows (or PETSC_DETERMINE)
3182b900a8SBarry Smith .  N - number of global columns (or PETSC_DETERMINE)
32cb13003dSBarry Smith -  comm - MPI communicator
337807a1faSBarry Smith 
347807a1faSBarry Smith    Output Parameter:
35dc401e71SLois Curfman McInnes .  A - the matrix
36e0b365e2SLois Curfman McInnes 
37273d9f13SBarry Smith    Options Database Keys:
38273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
39273d9f13SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
40273d9f13SBarry Smith .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
41273d9f13SBarry Smith .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
42273d9f13SBarry Smith .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
43273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
44273d9f13SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
45273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
46273d9f13SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
47e0b365e2SLois Curfman McInnes 
4883e1b59cSLois Curfman McInnes    Even More Options Database Keys:
4983e1b59cSLois Curfman McInnes    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
5083e1b59cSLois Curfman McInnes    for additional format-specific options.
51e0b365e2SLois Curfman McInnes 
52bd9ce289SLois Curfman McInnes    Notes:
53ec6e0d80SSatish Balay    If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
54ec6e0d80SSatish Balay    user must ensure that they are chosen to be compatible with the
55ec6e0d80SSatish Balay    vectors. To do this, one first considers the matrix-vector product
56ec6e0d80SSatish Balay    'y = A x'. The 'm' that is used in the above routine must match the
57ec6e0d80SSatish Balay    local size used in the vector creation routine VecCreateMPI() for 'y'.
58ec6e0d80SSatish Balay    Likewise, the 'n' used must match that used as the local size in
59ec6e0d80SSatish Balay    VecCreateMPI() for 'x'.
60ec6e0d80SSatish Balay 
61273d9f13SBarry Smith    Level: beginner
62273d9f13SBarry Smith 
63a2fc510eSBarry Smith    User manual sections:
64a2fc510eSBarry Smith +   sec_matcreate
65a2fc510eSBarry Smith -   chapter_matrices
66a2fc510eSBarry Smith 
67273d9f13SBarry Smith .keywords: matrix, create
68273d9f13SBarry Smith 
69273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
70273d9f13SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
71273d9f13SBarry Smith           MatCreateSeqDense(), MatCreateMPIDense(),
72273d9f13SBarry Smith           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
73273d9f13SBarry Smith           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
74273d9f13SBarry Smith           MatConvert()
75273d9f13SBarry Smith @*/
76*38baddfdSBarry Smith PetscErrorCode MatCreate(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,Mat *A)
77273d9f13SBarry Smith {
78273d9f13SBarry Smith   Mat B;
798ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
80dfbe8321SBarry Smith   PetscErrorCode ierr;
818ba1e511SMatthew Knepley #endif
82273d9f13SBarry Smith 
83273d9f13SBarry Smith   PetscFunctionBegin;
844482741eSBarry Smith   PetscValidPointer(A,6);
85*38baddfdSBarry Smith   if (M > 0 && m > M) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local column size %d cannot be larger than global column size %d",(int)m,(int)M);
86*38baddfdSBarry Smith   if (N > 0 && n > N) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local row size %d cannot be larger than global row size %d",(int)n,(int)N);
8797f1f81fSBarry Smith 
888ba1e511SMatthew Knepley   *A = PETSC_NULL;
898ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
908ba1e511SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
918ba1e511SMatthew Knepley #endif
928ba1e511SMatthew Knepley 
93273d9f13SBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
94b0a32e0cSBarry Smith   PetscLogObjectCreate(B);
95273d9f13SBarry Smith 
96273d9f13SBarry Smith   B->m = m;
97273d9f13SBarry Smith   B->n = n;
98273d9f13SBarry Smith   B->M = M;
99273d9f13SBarry Smith   B->N = N;
100273d9f13SBarry Smith 
101273d9f13SBarry Smith   B->preallocated  = PETSC_FALSE;
10235d8aa7fSBarry Smith   B->bops->publish = MatPublish_Base;
103273d9f13SBarry Smith   *A = B;
104273d9f13SBarry Smith   PetscFunctionReturn(0);
105273d9f13SBarry Smith }
106273d9f13SBarry Smith 
1074a2ae208SSatish Balay #undef __FUNCT__
1084a2ae208SSatish Balay #define __FUNCT__ "MatSetFromOptions"
109273d9f13SBarry Smith /*@C
110273d9f13SBarry Smith    MatSetFromOptions - Creates a matrix where the type is determined
111273d9f13SBarry Smith    from the options database. Generates a parallel MPI matrix if the
112273d9f13SBarry Smith    communicator has more than one processor.  The default matrix type is
1137e5f4302SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if
1147e5f4302SBarry Smith    you do not select a type in the options database.
115273d9f13SBarry Smith 
116273d9f13SBarry Smith    Collective on Mat
117273d9f13SBarry Smith 
118273d9f13SBarry Smith    Input Parameter:
119273d9f13SBarry Smith .  A - the matrix
120273d9f13SBarry Smith 
121273d9f13SBarry Smith    Options Database Keys:
122273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
123273d9f13SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
124273d9f13SBarry Smith .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
125273d9f13SBarry Smith .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
126273d9f13SBarry Smith .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
127273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
128273d9f13SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
129273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
130273d9f13SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
131273d9f13SBarry Smith 
132273d9f13SBarry Smith    Even More Options Database Keys:
133273d9f13SBarry Smith    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
134273d9f13SBarry Smith    for additional format-specific options.
135bd9ce289SLois Curfman McInnes 
1361d69843bSLois Curfman McInnes    Level: beginner
1371d69843bSLois Curfman McInnes 
138dc401e71SLois Curfman McInnes .keywords: matrix, create
139e0b365e2SLois Curfman McInnes 
140fafbff53SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
141fafbff53SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
14239ddd567SLois Curfman McInnes           MatCreateSeqDense(), MatCreateMPIDense(),
143a209d233SLois Curfman McInnes           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
144a209d233SLois Curfman McInnes           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
145273d9f13SBarry Smith           MatConvert()
1467807a1faSBarry Smith @*/
147dfbe8321SBarry Smith PetscErrorCode MatSetFromOptions(Mat B)
1487807a1faSBarry Smith {
149dfbe8321SBarry Smith   PetscErrorCode ierr;
150dfbe8321SBarry Smith   int size;
151273d9f13SBarry Smith   char       mtype[256];
152273d9f13SBarry Smith   PetscTruth flg;
153dbb450caSBarry Smith 
1543a40ed3dSBarry Smith   PetscFunctionBegin;
155b0a32e0cSBarry Smith   ierr = PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
156273d9f13SBarry Smith   if (flg) {
157273d9f13SBarry Smith     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
158273d9f13SBarry Smith   }
159273d9f13SBarry Smith   if (!B->type_name) {
160273d9f13SBarry Smith     ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
1611d9a38edSKris Buschelman     ierr = MatSetType(B,MATAIJ);CHKERRQ(ierr);
162dfa27b74SSatish Balay   }
163c7cd70f7SSatish Balay #if defined(__cplusplus) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && defined(PETSC_HAVE_CXX_NAMESPACE)
16424a595ddSBarry Smith   ierr = MatESISetFromOptions(B);CHKERRQ(ierr);
16524a595ddSBarry Smith #endif
1663a40ed3dSBarry Smith   PetscFunctionReturn(0);
1677807a1faSBarry Smith }
1687807a1faSBarry Smith 
1694a2ae208SSatish Balay #undef __FUNCT__
1704a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation"
171273d9f13SBarry Smith /*@C
172273d9f13SBarry Smith    MatSetUpPreallocation
173dae03382SLois Curfman McInnes 
174273d9f13SBarry Smith    Collective on Mat
175dae03382SLois Curfman McInnes 
176273d9f13SBarry Smith    Input Parameter:
177273d9f13SBarry Smith .  A - the matrix
178d5d45c9bSBarry Smith 
179273d9f13SBarry Smith    Level: beginner
180d5d45c9bSBarry Smith 
181273d9f13SBarry Smith .keywords: matrix, create
182273d9f13SBarry Smith 
183273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
184273d9f13SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
185273d9f13SBarry Smith           MatCreateSeqDense(), MatCreateMPIDense(),
186273d9f13SBarry Smith           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
187273d9f13SBarry Smith           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
188273d9f13SBarry Smith           MatConvert()
189273d9f13SBarry Smith @*/
190dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation(Mat B)
191273d9f13SBarry Smith {
192dfbe8321SBarry Smith   PetscErrorCode ierr;
193273d9f13SBarry Smith 
194273d9f13SBarry Smith   PetscFunctionBegin;
195273d9f13SBarry Smith   if (B->ops->setuppreallocation) {
1968ba1e511SMatthew Knepley     PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
197273d9f13SBarry Smith     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
198273d9f13SBarry Smith     B->ops->setuppreallocation = 0;
199273d9f13SBarry Smith     B->preallocated            = PETSC_TRUE;
200273d9f13SBarry Smith   }
201273d9f13SBarry Smith   PetscFunctionReturn(0);
202273d9f13SBarry Smith }
203273d9f13SBarry Smith 
204273d9f13SBarry Smith /*
205273d9f13SBarry Smith         Copies from Cs header to A
206273d9f13SBarry Smith */
2074a2ae208SSatish Balay #undef __FUNCT__
2084a2ae208SSatish Balay #define __FUNCT__ "MatHeaderCopy"
209dfbe8321SBarry Smith PetscErrorCode MatHeaderCopy(Mat A,Mat C)
210273d9f13SBarry Smith {
211dfbe8321SBarry Smith   PetscErrorCode ierr;
212dfbe8321SBarry Smith   int refct;
213273d9f13SBarry Smith   PetscOps    *Abops;
214273d9f13SBarry Smith   MatOps      Aops;
215273d9f13SBarry Smith   char        *mtype,*mname;
216273d9f13SBarry Smith 
217273d9f13SBarry Smith   PetscFunctionBegin;
218273d9f13SBarry Smith   /* free all the interior data structures from mat */
219273d9f13SBarry Smith   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
220273d9f13SBarry Smith 
2218a124369SBarry Smith   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
2228a124369SBarry Smith   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
223273d9f13SBarry Smith 
224273d9f13SBarry Smith   /* save the parts of A we need */
225273d9f13SBarry Smith   Abops = A->bops;
226273d9f13SBarry Smith   Aops  = A->ops;
227273d9f13SBarry Smith   refct = A->refct;
228273d9f13SBarry Smith   mtype = A->type_name;
229273d9f13SBarry Smith   mname = A->name;
230273d9f13SBarry Smith 
231273d9f13SBarry Smith   /* copy C over to A */
232273d9f13SBarry Smith   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
233273d9f13SBarry Smith 
234273d9f13SBarry Smith   /* return the parts of A we saved */
235273d9f13SBarry Smith   A->bops      = Abops;
236273d9f13SBarry Smith   A->ops       = Aops;
237273d9f13SBarry Smith   A->qlist     = 0;
238273d9f13SBarry Smith   A->refct     = refct;
239273d9f13SBarry Smith   A->type_name = mtype;
240273d9f13SBarry Smith   A->name      = mname;
241273d9f13SBarry Smith 
2420cc1bc1eSSatish Balay   PetscLogObjectDestroy(C);
243273d9f13SBarry Smith   PetscHeaderDestroy(C);
244273d9f13SBarry Smith   PetscFunctionReturn(0);
245273d9f13SBarry Smith }
246