xref: /petsc/src/mat/utils/gcreate.c (revision 6849ba73f22fecb8f92ef896a42e4e8bd4cd6965) !
1 
2 #include "src/mat/matimpl.h"       /*I "petscmat.h"  I*/
3 #include "petscsys.h"
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "MatPublish_Base"
7 static PetscErrorCode MatPublish_Base(PetscObject obj)
8 {
9 #if defined(PETSC_HAVE_AMS)
10   Mat mat = (Mat)obj;
11   PetscErrorCode ierr;
12 #endif
13 
14   PetscFunctionBegin;
15 #if defined(PETSC_HAVE_AMS)
16   /* if it is already published then return */
17   if (mat->amem >=0) PetscFunctionReturn(0);
18 
19   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
20   ierr = AMS_Memory_add_field((AMS_Memory)mat->amem,"globalsizes",&mat->M,2,AMS_INT,AMS_READ,
21                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
22   ierr = AMS_Memory_add_field((AMS_Memory)mat->amem,"localsizes",&mat->m,2,AMS_INT,AMS_READ,
23                                 AMS_DISTRIBUTED,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
24   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
25 #endif
26 
27   PetscFunctionReturn(0);
28 }
29 
30 
31 #undef __FUNCT__
32 #define __FUNCT__ "MatCreate"
33 /*@C
34    MatCreate - Creates a matrix where the type is determined
35    from either a call to MatSetType() or from the options database
36    with a call to MatSetFromOptions(). The default matrix type is
37    AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ()
38    if you do not set a type in the options database. If you never
39    call MatSetType() or MatSetFromOptions() it will generate an
40    error when you try to use the matrix.
41 
42    Collective on MPI_Comm
43 
44    Input Parameters:
45 +  m - number of local rows (or PETSC_DECIDE)
46 .  n - number of local columns (or PETSC_DECIDE)
47 .  M - number of global rows (or PETSC_DETERMINE)
48 .  N - number of global columns (or PETSC_DETERMINE)
49 -  comm - MPI communicator
50 
51    Output Parameter:
52 .  A - the matrix
53 
54    Options Database Keys:
55 +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
56 .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
57 .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
58 .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
59 .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
60 .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
61 .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
62 .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
63 -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
64 
65    Even More Options Database Keys:
66    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
67    for additional format-specific options.
68 
69    Notes:
70    If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
71    user must ensure that they are chosen to be compatible with the
72    vectors. To do this, one first considers the matrix-vector product
73    'y = A x'. The 'm' that is used in the above routine must match the
74    local size used in the vector creation routine VecCreateMPI() for 'y'.
75    Likewise, the 'n' used must match that used as the local size in
76    VecCreateMPI() for 'x'.
77 
78    Level: beginner
79 
80    User manual sections:
81 +   sec_matcreate
82 -   chapter_matrices
83 
84 .keywords: matrix, create
85 
86 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
87           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
88           MatCreateSeqDense(), MatCreateMPIDense(),
89           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
90           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
91           MatConvert()
92 @*/
93 PetscErrorCode MatCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
94 {
95   Mat B;
96 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
97   PetscErrorCode ierr;
98 #endif
99 
100   PetscFunctionBegin;
101   PetscValidPointer(A,6);
102   *A = PETSC_NULL;
103 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
104   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
105 #endif
106 
107   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
108   PetscLogObjectCreate(B);
109 
110   B->m = m;
111   B->n = n;
112   B->M = M;
113   B->N = N;
114 
115   B->preallocated  = PETSC_FALSE;
116   B->bops->publish = MatPublish_Base;
117   *A = B;
118   PetscFunctionReturn(0);
119 }
120 
121 #undef __FUNCT__
122 #define __FUNCT__ "MatSetFromOptions"
123 /*@C
124    MatSetFromOptions - Creates a matrix where the type is determined
125    from the options database. Generates a parallel MPI matrix if the
126    communicator has more than one processor.  The default matrix type is
127    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if
128    you do not select a type in the options database.
129 
130    Collective on Mat
131 
132    Input Parameter:
133 .  A - the matrix
134 
135    Options Database Keys:
136 +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
137 .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
138 .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
139 .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
140 .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
141 .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
142 .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
143 .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
144 -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
145 
146    Even More Options Database Keys:
147    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
148    for additional format-specific options.
149 
150    Level: beginner
151 
152 .keywords: matrix, create
153 
154 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
155           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
156           MatCreateSeqDense(), MatCreateMPIDense(),
157           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
158           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
159           MatConvert()
160 @*/
161 PetscErrorCode MatSetFromOptions(Mat B)
162 {
163   PetscErrorCode ierr;
164   int size;
165   char       mtype[256];
166   PetscTruth flg;
167 
168   PetscFunctionBegin;
169   ierr = PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
170   if (flg) {
171     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
172   }
173   if (!B->type_name) {
174     ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
175     ierr = MatSetType(B,MATAIJ);CHKERRQ(ierr);
176   }
177 #if defined(__cplusplus) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && defined(PETSC_HAVE_CXX_NAMESPACE)
178   ierr = MatESISetFromOptions(B);CHKERRQ(ierr);
179 #endif
180   PetscFunctionReturn(0);
181 }
182 
183 #undef __FUNCT__
184 #define __FUNCT__ "MatSetUpPreallocation"
185 /*@C
186    MatSetUpPreallocation
187 
188    Collective on Mat
189 
190    Input Parameter:
191 .  A - the matrix
192 
193    Level: beginner
194 
195 .keywords: matrix, create
196 
197 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
198           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
199           MatCreateSeqDense(), MatCreateMPIDense(),
200           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
201           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
202           MatConvert()
203 @*/
204 PetscErrorCode MatSetUpPreallocation(Mat B)
205 {
206   PetscErrorCode ierr;
207 
208   PetscFunctionBegin;
209   if (B->ops->setuppreallocation) {
210     PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
211     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
212     B->ops->setuppreallocation = 0;
213     B->preallocated            = PETSC_TRUE;
214   }
215   PetscFunctionReturn(0);
216 }
217 
218 /*
219         Copies from Cs header to A
220 */
221 #undef __FUNCT__
222 #define __FUNCT__ "MatHeaderCopy"
223 PetscErrorCode MatHeaderCopy(Mat A,Mat C)
224 {
225   PetscErrorCode ierr;
226   int refct;
227   PetscOps    *Abops;
228   MatOps      Aops;
229   char        *mtype,*mname;
230 
231   PetscFunctionBegin;
232   /* free all the interior data structures from mat */
233   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
234 
235   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
236   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
237 
238   /* save the parts of A we need */
239   Abops = A->bops;
240   Aops  = A->ops;
241   refct = A->refct;
242   mtype = A->type_name;
243   mname = A->name;
244 
245   /* copy C over to A */
246   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
247 
248   /* return the parts of A we saved */
249   A->bops      = Abops;
250   A->ops       = Aops;
251   A->qlist     = 0;
252   A->refct     = refct;
253   A->type_name = mtype;
254   A->name      = mname;
255 
256   PetscLogObjectDestroy(C);
257   PetscHeaderDestroy(C);
258   PetscFunctionReturn(0);
259 }
260