xref: /petsc/src/mat/utils/gcreate.c (revision d44834fb5aa6ab55eaa2e18f09dfd00e40aa4639)
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 @*/
7638baddfdSBarry 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);
8577431f27SBarry Smith   if (M > 0 && m > M) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local column size %D cannot be larger than global column size %D",m,M);
8677431f27SBarry Smith   if (N > 0 && n > N) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local row size %D cannot be larger than global row size %D",n,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;
100521d7252SBarry Smith   B->bs            = 1;
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;
1502d156eb4SBarry Smith   PetscMPIInt    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   }
1633a40ed3dSBarry Smith   PetscFunctionReturn(0);
1647807a1faSBarry Smith }
1657807a1faSBarry Smith 
1664a2ae208SSatish Balay #undef __FUNCT__
1674a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation"
168273d9f13SBarry Smith /*@C
169273d9f13SBarry Smith    MatSetUpPreallocation
170dae03382SLois Curfman McInnes 
171273d9f13SBarry Smith    Collective on Mat
172dae03382SLois Curfman McInnes 
173273d9f13SBarry Smith    Input Parameter:
174273d9f13SBarry Smith .  A - the matrix
175d5d45c9bSBarry Smith 
176273d9f13SBarry Smith    Level: beginner
177d5d45c9bSBarry Smith 
178273d9f13SBarry Smith .keywords: matrix, create
179273d9f13SBarry Smith 
180273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
181273d9f13SBarry Smith           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
182273d9f13SBarry Smith           MatCreateSeqDense(), MatCreateMPIDense(),
183273d9f13SBarry Smith           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
184273d9f13SBarry Smith           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
185273d9f13SBarry Smith           MatConvert()
186273d9f13SBarry Smith @*/
187dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation(Mat B)
188273d9f13SBarry Smith {
189dfbe8321SBarry Smith   PetscErrorCode ierr;
190273d9f13SBarry Smith 
191273d9f13SBarry Smith   PetscFunctionBegin;
192273d9f13SBarry Smith   if (B->ops->setuppreallocation) {
1938ba1e511SMatthew Knepley     PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
194273d9f13SBarry Smith     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
195273d9f13SBarry Smith     B->ops->setuppreallocation = 0;
196273d9f13SBarry Smith     B->preallocated            = PETSC_TRUE;
197273d9f13SBarry Smith   }
198273d9f13SBarry Smith   PetscFunctionReturn(0);
199273d9f13SBarry Smith }
200273d9f13SBarry Smith 
201273d9f13SBarry Smith /*
202273d9f13SBarry Smith         Copies from Cs header to A
203273d9f13SBarry Smith */
2044a2ae208SSatish Balay #undef __FUNCT__
2054a2ae208SSatish Balay #define __FUNCT__ "MatHeaderCopy"
206dfbe8321SBarry Smith PetscErrorCode MatHeaderCopy(Mat A,Mat C)
207273d9f13SBarry Smith {
208dfbe8321SBarry Smith   PetscErrorCode ierr;
209*d44834fbSBarry Smith   PetscInt       refct;
210273d9f13SBarry Smith   PetscOps       *Abops;
211273d9f13SBarry Smith   MatOps         Aops;
212273d9f13SBarry Smith   char           *mtype,*mname;
213273d9f13SBarry Smith 
214273d9f13SBarry Smith   PetscFunctionBegin;
215273d9f13SBarry Smith   /* free all the interior data structures from mat */
216273d9f13SBarry Smith   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
217273d9f13SBarry Smith 
2188a124369SBarry Smith   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
2198a124369SBarry Smith   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
220273d9f13SBarry Smith 
221273d9f13SBarry Smith   /* save the parts of A we need */
222273d9f13SBarry Smith   Abops = A->bops;
223273d9f13SBarry Smith   Aops  = A->ops;
224273d9f13SBarry Smith   refct = A->refct;
225273d9f13SBarry Smith   mtype = A->type_name;
226273d9f13SBarry Smith   mname = A->name;
227273d9f13SBarry Smith 
228273d9f13SBarry Smith   /* copy C over to A */
229273d9f13SBarry Smith   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
230273d9f13SBarry Smith 
231273d9f13SBarry Smith   /* return the parts of A we saved */
232273d9f13SBarry Smith   A->bops      = Abops;
233273d9f13SBarry Smith   A->ops       = Aops;
234273d9f13SBarry Smith   A->qlist     = 0;
235273d9f13SBarry Smith   A->refct     = refct;
236273d9f13SBarry Smith   A->type_name = mtype;
237273d9f13SBarry Smith   A->name      = mname;
238273d9f13SBarry Smith 
2390cc1bc1eSSatish Balay   PetscLogObjectDestroy(C);
240273d9f13SBarry Smith   PetscHeaderDestroy(C);
241273d9f13SBarry Smith   PetscFunctionReturn(0);
242273d9f13SBarry Smith }
243