xref: /petsc/src/mat/utils/gcreate.c (revision ae15b995b5732fffd2de5a75cf61ef7190c6fef1)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
27807a1faSBarry Smith 
3273d9f13SBarry Smith #include "src/mat/matimpl.h"       /*I "petscmat.h"  I*/
4325e03aeSBarry Smith #include "petscsys.h"
57807a1faSBarry Smith 
64a2ae208SSatish Balay #undef __FUNCT__
74a2ae208SSatish Balay #define __FUNCT__ "MatPublish_Base"
86849ba73SBarry Smith static PetscErrorCode MatPublish_Base(PetscObject obj)
935d8aa7fSBarry Smith {
1035d8aa7fSBarry Smith   PetscFunctionBegin;
1135d8aa7fSBarry Smith   PetscFunctionReturn(0);
1235d8aa7fSBarry Smith }
1335d8aa7fSBarry Smith 
1435d8aa7fSBarry Smith 
154a2ae208SSatish Balay #undef __FUNCT__
164a2ae208SSatish Balay #define __FUNCT__ "MatCreate"
1705869f15SSatish Balay /*@
1869dd0797SLois Curfman McInnes    MatCreate - Creates a matrix where the type is determined
197e5f4302SBarry Smith    from either a call to MatSetType() or from the options database
207e5f4302SBarry Smith    with a call to MatSetFromOptions(). The default matrix type is
217e5f4302SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ()
227e5f4302SBarry Smith    if you do not set a type in the options database. If you never
237e5f4302SBarry Smith    call MatSetType() or MatSetFromOptions() it will generate an
24f8ab6608SSatish Balay    error when you try to use the matrix.
2583e1b59cSLois Curfman McInnes 
26cb13003dSBarry Smith    Collective on MPI_Comm
27cb13003dSBarry Smith 
28f69a0ea3SMatthew Knepley    Input Parameter:
29f69a0ea3SMatthew Knepley .  comm - MPI communicator
307807a1faSBarry Smith 
317807a1faSBarry Smith    Output Parameter:
32dc401e71SLois Curfman McInnes .  A - the matrix
33e0b365e2SLois Curfman McInnes 
34273d9f13SBarry Smith    Options Database Keys:
35273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
36273d9f13SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
37273d9f13SBarry Smith .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
38273d9f13SBarry Smith .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
39273d9f13SBarry Smith .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
40273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
41273d9f13SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
42273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
43273d9f13SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
44e0b365e2SLois Curfman McInnes 
4583e1b59cSLois Curfman McInnes    Even More Options Database Keys:
4683e1b59cSLois Curfman McInnes    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
4783e1b59cSLois Curfman McInnes    for additional format-specific options.
48e0b365e2SLois Curfman McInnes 
49bd9ce289SLois Curfman McInnes    Notes:
50ec6e0d80SSatish Balay 
51273d9f13SBarry Smith    Level: beginner
52273d9f13SBarry Smith 
53a2fc510eSBarry Smith    User manual sections:
54a2fc510eSBarry Smith +   sec_matcreate
55a2fc510eSBarry Smith -   chapter_matrices
56a2fc510eSBarry Smith 
57273d9f13SBarry Smith .keywords: matrix, create
58273d9f13SBarry Smith 
59273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
60273d9f13SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
61273d9f13SBarry Smith           MatCreateSeqDense(), MatCreateMPIDense(),
62273d9f13SBarry Smith           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
63273d9f13SBarry Smith           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
64273d9f13SBarry Smith           MatConvert()
65273d9f13SBarry Smith @*/
66f69a0ea3SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatCreate(MPI_Comm comm,Mat *A)
67273d9f13SBarry Smith {
68273d9f13SBarry Smith   Mat            B;
69dfbe8321SBarry Smith   PetscErrorCode ierr;
70273d9f13SBarry Smith 
71273d9f13SBarry Smith   PetscFunctionBegin;
72f69a0ea3SMatthew Knepley   PetscValidPointer(A,2);
7397f1f81fSBarry Smith 
748ba1e511SMatthew Knepley   *A = PETSC_NULL;
758ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
768ba1e511SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
778ba1e511SMatthew Knepley #endif
788ba1e511SMatthew Knepley 
7952e6d16bSBarry Smith   ierr = PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);CHKERRQ(ierr);
80f69a0ea3SMatthew Knepley   B->m             = -1;
81f69a0ea3SMatthew Knepley   B->M             = -1;
82f69a0ea3SMatthew Knepley   B->n             = -1;
83f69a0ea3SMatthew Knepley   B->N             = -1;
84521d7252SBarry Smith   B->bs            = 1;
85273d9f13SBarry Smith   B->preallocated  = PETSC_FALSE;
8635d8aa7fSBarry Smith   B->bops->publish = MatPublish_Base;
87273d9f13SBarry Smith   *A               = B;
88273d9f13SBarry Smith   PetscFunctionReturn(0);
89273d9f13SBarry Smith }
90273d9f13SBarry Smith 
914a2ae208SSatish Balay #undef __FUNCT__
92f69a0ea3SMatthew Knepley #define __FUNCT__ "MatSetSizes"
93f69a0ea3SMatthew Knepley /*@
94f69a0ea3SMatthew Knepley   MatSetSizes - Sets the local and global sizes, and checks to determine compatibility
95f69a0ea3SMatthew Knepley 
96f69a0ea3SMatthew Knepley   Collective on Mat
97f69a0ea3SMatthew Knepley 
98f69a0ea3SMatthew Knepley   Input Parameters:
99f69a0ea3SMatthew Knepley +  A - the matrix
100f69a0ea3SMatthew Knepley .  m - number of local rows (or PETSC_DECIDE)
101f69a0ea3SMatthew Knepley .  n - number of local columns (or PETSC_DECIDE)
102f69a0ea3SMatthew Knepley .  M - number of global rows (or PETSC_DETERMINE)
103f69a0ea3SMatthew Knepley -  N - number of global columns (or PETSC_DETERMINE)
104f69a0ea3SMatthew Knepley 
105f69a0ea3SMatthew Knepley    Notes:
106f69a0ea3SMatthew Knepley    m (n) and M (N) cannot be both PETSC_DECIDE
107f69a0ea3SMatthew Knepley    If one processor calls this with M (N) of PETSC_DECIDE then all processors must, otherwise the program will hang.
108f69a0ea3SMatthew Knepley 
109f69a0ea3SMatthew Knepley    If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
110f69a0ea3SMatthew Knepley    user must ensure that they are chosen to be compatible with the
111f69a0ea3SMatthew Knepley    vectors. To do this, one first considers the matrix-vector product
112f69a0ea3SMatthew Knepley    'y = A x'. The 'm' that is used in the above routine must match the
113f69a0ea3SMatthew Knepley    local size used in the vector creation routine VecCreateMPI() for 'y'.
114f69a0ea3SMatthew Knepley    Likewise, the 'n' used must match that used as the local size in
115f69a0ea3SMatthew Knepley    VecCreateMPI() for 'x'.
116f69a0ea3SMatthew Knepley 
117f69a0ea3SMatthew Knepley   Level: beginner
118f69a0ea3SMatthew Knepley 
119f69a0ea3SMatthew Knepley .seealso: MatGetSize(), PetscSplitOwnership()
120f69a0ea3SMatthew Knepley @*/
121f69a0ea3SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N)
122f69a0ea3SMatthew Knepley {
123284134d9SBarry Smith   PetscErrorCode ierr;
124284134d9SBarry Smith 
125f69a0ea3SMatthew Knepley   PetscFunctionBegin;
126f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
127f69a0ea3SMatthew Knepley   if (M > 0 && m > M) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local column size %D cannot be larger than global column size %D",m,M);
128f69a0ea3SMatthew Knepley   if (N > 0 && n > N) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local row size %D cannot be larger than global row size %D",n,N);
129284134d9SBarry Smith   if (A->ops->setsizes) {
130284134d9SBarry Smith     /* Since this will not be set until the type has been set, this will NOT be called on the initial
131284134d9SBarry Smith        call of MatSetSizes() (which must be called BEFORE MatSetType() */
132284134d9SBarry Smith     ierr = (*A->ops->setsizes)(A,m,n,M,N);CHKERRQ(ierr);
133284134d9SBarry Smith   } else {
134f69a0ea3SMatthew Knepley     if ((A->m >= 0 || A->M >= 0) && (A->m != m || A->M != M)) SETERRQ4(PETSC_ERR_SUP,"Cannot change/reset row sizes to %D local %D global after previously setting them to %D local %D global",m,M,A->m,A->M);
135f69a0ea3SMatthew Knepley     if ((A->n >= 0 || A->N >= 0) && (A->n != n || A->N != N)) SETERRQ4(PETSC_ERR_SUP,"Cannot change/reset column sizes to %D local %D global after previously setting them to %D local %D global",n,N,A->n,A->N);
136284134d9SBarry Smith   }
137f69a0ea3SMatthew Knepley   A->m = m;
138f69a0ea3SMatthew Knepley   A->n = n;
139f69a0ea3SMatthew Knepley   A->M = M;
140f69a0ea3SMatthew Knepley   A->N = N;
141f69a0ea3SMatthew Knepley   PetscFunctionReturn(0);
142f69a0ea3SMatthew Knepley }
143f69a0ea3SMatthew Knepley 
144f69a0ea3SMatthew Knepley #undef __FUNCT__
1454a2ae208SSatish Balay #define __FUNCT__ "MatSetFromOptions"
14605869f15SSatish Balay /*@
147273d9f13SBarry Smith    MatSetFromOptions - Creates a matrix where the type is determined
148273d9f13SBarry Smith    from the options database. Generates a parallel MPI matrix if the
149273d9f13SBarry Smith    communicator has more than one processor.  The default matrix type is
1507e5f4302SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if
1517e5f4302SBarry Smith    you do not select a type in the options database.
152273d9f13SBarry Smith 
153273d9f13SBarry Smith    Collective on Mat
154273d9f13SBarry Smith 
155273d9f13SBarry Smith    Input Parameter:
156273d9f13SBarry Smith .  A - the matrix
157273d9f13SBarry Smith 
158273d9f13SBarry Smith    Options Database Keys:
159273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
160273d9f13SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
161273d9f13SBarry Smith .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
162273d9f13SBarry Smith .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
163273d9f13SBarry Smith .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
164273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
165273d9f13SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
166273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
167273d9f13SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
168273d9f13SBarry Smith 
169273d9f13SBarry Smith    Even More Options Database Keys:
170273d9f13SBarry Smith    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
171273d9f13SBarry Smith    for additional format-specific options.
172bd9ce289SLois Curfman McInnes 
1731d69843bSLois Curfman McInnes    Level: beginner
1741d69843bSLois Curfman McInnes 
175dc401e71SLois Curfman McInnes .keywords: matrix, create
176e0b365e2SLois Curfman McInnes 
177fafbff53SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
178fafbff53SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
17939ddd567SLois Curfman McInnes           MatCreateSeqDense(), MatCreateMPIDense(),
180a209d233SLois Curfman McInnes           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
181a209d233SLois Curfman McInnes           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
182273d9f13SBarry Smith           MatConvert()
1837807a1faSBarry Smith @*/
184be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetFromOptions(Mat B)
1857807a1faSBarry Smith {
186dfbe8321SBarry Smith   PetscErrorCode ierr;
187273d9f13SBarry Smith   char           mtype[256];
188273d9f13SBarry Smith   PetscTruth     flg;
189dbb450caSBarry Smith 
1903a40ed3dSBarry Smith   PetscFunctionBegin;
191b0a32e0cSBarry Smith   ierr = PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
192273d9f13SBarry Smith   if (flg) {
193273d9f13SBarry Smith     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
194273d9f13SBarry Smith   }
195273d9f13SBarry Smith   if (!B->type_name) {
19630735b05SKris Buschelman     ierr = MatSetType(B,MATAIJ);CHKERRQ(ierr);
197dfa27b74SSatish Balay   }
1983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1997807a1faSBarry Smith }
2007807a1faSBarry Smith 
2014a2ae208SSatish Balay #undef __FUNCT__
2024a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation"
203273d9f13SBarry Smith /*@C
204273d9f13SBarry Smith    MatSetUpPreallocation
205dae03382SLois Curfman McInnes 
206273d9f13SBarry Smith    Collective on Mat
207dae03382SLois Curfman McInnes 
208273d9f13SBarry Smith    Input Parameter:
209273d9f13SBarry Smith .  A - the matrix
210d5d45c9bSBarry Smith 
211273d9f13SBarry Smith    Level: beginner
212d5d45c9bSBarry Smith 
213273d9f13SBarry Smith .keywords: matrix, create
214273d9f13SBarry Smith 
215273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
216273d9f13SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
217273d9f13SBarry Smith           MatCreateSeqDense(), MatCreateMPIDense(),
218273d9f13SBarry Smith           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
219273d9f13SBarry Smith           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
220273d9f13SBarry Smith           MatConvert()
221273d9f13SBarry Smith @*/
222be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetUpPreallocation(Mat B)
223273d9f13SBarry Smith {
224dfbe8321SBarry Smith   PetscErrorCode ierr;
225273d9f13SBarry Smith 
226273d9f13SBarry Smith   PetscFunctionBegin;
227273d9f13SBarry Smith   if (B->ops->setuppreallocation) {
228*ae15b995SBarry Smith     ierr = PetscInfo(B,"Warning not preallocating matrix storage\n");CHKERRQ(ierr);
229273d9f13SBarry Smith     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
230273d9f13SBarry Smith     B->ops->setuppreallocation = 0;
231273d9f13SBarry Smith   }
232210f0121SBarry Smith   B->preallocated = PETSC_TRUE;
233273d9f13SBarry Smith   PetscFunctionReturn(0);
234273d9f13SBarry Smith }
235273d9f13SBarry Smith 
236273d9f13SBarry Smith /*
237273d9f13SBarry Smith         Copies from Cs header to A
238273d9f13SBarry Smith */
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "MatHeaderCopy"
241dfbe8321SBarry Smith PetscErrorCode MatHeaderCopy(Mat A,Mat C)
242273d9f13SBarry Smith {
243dfbe8321SBarry Smith   PetscErrorCode ierr;
244d44834fbSBarry Smith   PetscInt       refct;
245273d9f13SBarry Smith   PetscOps       *Abops;
246273d9f13SBarry Smith   MatOps         Aops;
247273d9f13SBarry Smith   char           *mtype,*mname;
24830735b05SKris Buschelman   void           *spptr;
249273d9f13SBarry Smith 
250273d9f13SBarry Smith   PetscFunctionBegin;
251273d9f13SBarry Smith   /* free all the interior data structures from mat */
252273d9f13SBarry Smith   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
253273d9f13SBarry Smith 
2548a124369SBarry Smith   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
2558a124369SBarry Smith   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
256273d9f13SBarry Smith 
257273d9f13SBarry Smith   /* save the parts of A we need */
258273d9f13SBarry Smith   Abops = A->bops;
259273d9f13SBarry Smith   Aops  = A->ops;
260273d9f13SBarry Smith   refct = A->refct;
261273d9f13SBarry Smith   mtype = A->type_name;
262273d9f13SBarry Smith   mname = A->name;
26330735b05SKris Buschelman   spptr = A->spptr;
26430735b05SKris Buschelman 
26530735b05SKris Buschelman   if (C->spptr) {
26630735b05SKris Buschelman     ierr = PetscFree(C->spptr);CHKERRQ(ierr);
26730735b05SKris Buschelman     C->spptr = PETSC_NULL;
26830735b05SKris Buschelman   }
269273d9f13SBarry Smith 
270273d9f13SBarry Smith   /* copy C over to A */
271273d9f13SBarry Smith   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
272273d9f13SBarry Smith 
273273d9f13SBarry Smith   /* return the parts of A we saved */
274273d9f13SBarry Smith   A->bops      = Abops;
275273d9f13SBarry Smith   A->ops       = Aops;
276273d9f13SBarry Smith   A->qlist     = 0;
277273d9f13SBarry Smith   A->refct     = refct;
278273d9f13SBarry Smith   A->type_name = mtype;
279273d9f13SBarry Smith   A->name      = mname;
28030735b05SKris Buschelman   A->spptr     = spptr;
281273d9f13SBarry Smith 
282d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(C);CHKERRQ(ierr);
283273d9f13SBarry Smith   PetscFunctionReturn(0);
284273d9f13SBarry Smith }
2858ab5b326SKris Buschelman /*
2868ab5b326SKris Buschelman         Replace A's header with that of C
2878ab5b326SKris Buschelman         This is essentially code moved from MatDestroy
2888ab5b326SKris Buschelman */
2898ab5b326SKris Buschelman #undef __FUNCT__
2908ab5b326SKris Buschelman #define __FUNCT__ "MatHeaderReplace"
2918ab5b326SKris Buschelman PetscErrorCode MatHeaderReplace(Mat A,Mat C)
2928ab5b326SKris Buschelman {
2938ab5b326SKris Buschelman   PetscErrorCode ierr;
2948ab5b326SKris Buschelman 
2958ab5b326SKris Buschelman   PetscFunctionBegin;
2968ab5b326SKris Buschelman   /* free all the interior data structures from mat */
2978ab5b326SKris Buschelman   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
298d2c95936SBarry Smith   ierr = PetscHeaderDestroy_Private((PetscObject)A);CHKERRQ(ierr);
299cb8bee85SHong Zhang   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
300cb8bee85SHong Zhang   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
3018ab5b326SKris Buschelman 
3028ab5b326SKris Buschelman   /* copy C over to A */
3038ab5b326SKris Buschelman   if (C) {
3048ab5b326SKris Buschelman     ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
3058ab5b326SKris Buschelman     ierr = PetscLogObjectDestroy((PetscObject)C);CHKERRQ(ierr);
3068ab5b326SKris Buschelman     ierr = PetscFree(C);CHKERRQ(ierr);
3078ab5b326SKris Buschelman   }
3088ab5b326SKris Buschelman   PetscFunctionReturn(0);
3098ab5b326SKris Buschelman }
310