xref: /petsc/src/mat/utils/gcreate.c (revision 273d9f13de75c4ed17021f7f2c11eebb99d26f0d)
1 /*$Id: gcreate.c,v 1.123 2000/09/25 19:57:36 curfman Exp bsmith $*/
2 
3 #include "petscsys.h"
4 #include "src/mat/matimpl.h"       /*I "petscmat.h"  I*/
5 
6 #undef __FUNC__
7 #define __FUNC__ /*<a name=""></a>*/"MatCreate"
8 /*@C
9    MatCreate - Creates a matrix where the type is determined
10    from the options database. Generates a parallel MPI matrix if the
11    communicator has more than one processor.  The default matrix type is
12    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ().
13 
14    Collective on MPI_Comm
15 
16    Input Parameters:
17 +  m - number of local rows (or PETSC_DECIDE)
18 .  n - number of local columns (or PETSC_DECIDE)
19 .  M - number of global rows (or PETSC_DETERMINE)
20 .  N - number of global columns (or PETSC_DETERMINE)
21 -  comm - MPI communicator
22 
23    Output Parameter:
24 .  A - the matrix
25 
26    Options Database Keys:
27 +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
28 .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
29 .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
30 .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
31 .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
32 .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
33 .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
34 .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
35 -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
36 
37    Even More Options Database Keys:
38    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
39    for additional format-specific options.
40 
41    Notes:
42    If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
43    user must ensure that they are chosen to be compatible with the
44    vectors. To do this, one first considers the matrix-vector product
45    'y = A x'. The 'm' that is used in the above routine must match the
46    local size used in the vector creation routine VecCreateMPI() for 'y'.
47    Likewise, the 'n' used must match that used as the local size in
48    VecCreateMPI() for 'x'.
49 
50    Level: beginner
51 
52 .keywords: matrix, create
53 
54 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
55           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
56           MatCreateSeqDense(), MatCreateMPIDense(),
57           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
58           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
59           MatConvert()
60 @*/
61 int MatCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
62 {
63   Mat B;
64 
65   PetscFunctionBegin;
66   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
67   PLogObjectCreate(B);
68 
69   B->m = m;
70   B->n = n;
71   B->M = M;
72   B->N = N;
73 
74   B->preallocated = PETSC_FALSE;
75 
76   *A = B;
77   PetscFunctionReturn(0);
78 }
79 
80 #undef __FUNC__
81 #define __FUNC__ /*<a name=""></a>*/"MatSetFromOptions"
82 /*@C
83    MatSetFromOptions - Creates a matrix where the type is determined
84    from the options database. Generates a parallel MPI matrix if the
85    communicator has more than one processor.  The default matrix type is
86    AIJ, using the routines MatSetFromOptionsSeqAIJ() and MatSetFromOptionsMPIAIJ().
87 
88    Collective on Mat
89 
90    Input Parameter:
91 .  A - the matrix
92 
93    Options Database Keys:
94 +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
95 .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
96 .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
97 .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
98 .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
99 .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
100 .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
101 .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
102 -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
103 
104    Even More Options Database Keys:
105    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
106    for additional format-specific options.
107 
108    Level: beginner
109 
110 .keywords: matrix, create
111 
112 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
113           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
114           MatCreateSeqDense(), MatCreateMPIDense(),
115           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
116           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
117           MatConvert()
118 @*/
119 int MatSetFromOptions(Mat B)
120 {
121   int        ierr,size;
122   char       mtype[256];
123   PetscTruth flg;
124 
125   PetscFunctionBegin;
126   ierr = OptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
127   if (flg) {
128     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
129   }
130   if (!B->type_name) {
131     ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
132     if (size == 1) {
133       ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
134     } else {
135       ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
136     }
137   }
138   PetscFunctionReturn(0);
139 }
140 
141 #undef __FUNC__
142 #define __FUNC__ /*<a name=""></a>*/"MatSetUpPreallocation"
143 /*@C
144    MatSetUpPreallocation
145 
146    Collective on Mat
147 
148    Input Parameter:
149 .  A - the matrix
150 
151    Level: beginner
152 
153 .keywords: matrix, create
154 
155 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
156           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
157           MatCreateSeqDense(), MatCreateMPIDense(),
158           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
159           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
160           MatConvert()
161 @*/
162 int MatSetUpPreallocation(Mat B)
163 {
164   int        ierr;
165 
166   PetscFunctionBegin;
167   if (B->ops->setuppreallocation) {
168     PLogInfo(B,"MatSetTpPreallocation: Warning not preallocating matrix storage");
169     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
170     B->ops->setuppreallocation = 0;
171     B->preallocated            = PETSC_TRUE;
172   }
173   PetscFunctionReturn(0);
174 }
175 
176 /*
177         Copies from Cs header to A
178 */
179 #undef __FUNC__
180 #define __FUNC__ /*<a name="MatHeaderCopy"></a>*/"MatHeaderCopy"
181 int MatHeaderCopy(Mat A,Mat C)
182 {
183   int         ierr,refct;
184   PetscOps    *Abops;
185   MatOps      Aops;
186   char        *mtype,*mname;
187 
188   PetscFunctionBegin;
189   /* free all the interior data structures from mat */
190   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
191 
192   ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
193   ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
194 
195   /* save the parts of A we need */
196   Abops = A->bops;
197   Aops  = A->ops;
198   refct = A->refct;
199   mtype = A->type_name;
200   mname = A->name;
201 
202   /* copy C over to A */
203   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
204 
205   /* return the parts of A we saved */
206   A->bops      = Abops;
207   A->ops       = Aops;
208   A->qlist     = 0;
209   A->refct     = refct;
210   A->type_name = mtype;
211   A->name      = mname;
212 
213   PetscHeaderDestroy(C);
214   PetscFunctionReturn(0);
215 }
216