xref: /petsc/src/mat/utils/gcreate.c (revision d5058c763de0077e1c2d63aecbdb635e3d260d53)
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 int MatPublish_Base(PetscObject obj)
8 {
9 #if defined(PETSC_HAVE_AMS)
10   Mat mat = (Mat)obj;
11   int 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 int 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   int 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 int MatSetFromOptions(Mat B)
162 {
163   int        ierr,size;
164   char       mtype[256];
165   PetscTruth flg;
166 
167   PetscFunctionBegin;
168   ierr = PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
169   if (flg) {
170     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
171   }
172   if (!B->type_name) {
173     ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
174     ierr = MatSetType(B,MATAIJ);CHKERRQ(ierr);
175   }
176 #if defined(__cplusplus) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && defined(PETSC_HAVE_CXX_NAMESPACE)
177   ierr = MatESISetFromOptions(B);CHKERRQ(ierr);
178 #endif
179   PetscFunctionReturn(0);
180 }
181 
182 #undef __FUNCT__
183 #define __FUNCT__ "MatSetUpPreallocation"
184 /*@C
185    MatSetUpPreallocation
186 
187    Collective on Mat
188 
189    Input Parameter:
190 .  A - the matrix
191 
192    Level: beginner
193 
194 .keywords: matrix, create
195 
196 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
197           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
198           MatCreateSeqDense(), MatCreateMPIDense(),
199           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
200           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
201           MatConvert()
202 @*/
203 int MatSetUpPreallocation(Mat B)
204 {
205   int        ierr;
206 
207   PetscFunctionBegin;
208   if (B->ops->setuppreallocation) {
209     PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
210     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
211     B->ops->setuppreallocation = 0;
212     B->preallocated            = PETSC_TRUE;
213   }
214   PetscFunctionReturn(0);
215 }
216 
217 /*
218         Copies from Cs header to A
219 */
220 #undef __FUNCT__
221 #define __FUNCT__ "MatHeaderCopy"
222 int MatHeaderCopy(Mat A,Mat C)
223 {
224   int         ierr,refct;
225   PetscOps    *Abops;
226   MatOps      Aops;
227   char        *mtype,*mname;
228 
229   PetscFunctionBegin;
230   /* free all the interior data structures from mat */
231   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
232 
233   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
234   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
235 
236   /* save the parts of A we need */
237   Abops = A->bops;
238   Aops  = A->ops;
239   refct = A->refct;
240   mtype = A->type_name;
241   mname = A->name;
242 
243   /* copy C over to A */
244   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
245 
246   /* return the parts of A we saved */
247   A->bops      = Abops;
248   A->ops       = Aops;
249   A->qlist     = 0;
250   A->refct     = refct;
251   A->type_name = mtype;
252   A->name      = mname;
253 
254   PetscLogObjectDestroy(C);
255   PetscHeaderDestroy(C);
256   PetscFunctionReturn(0);
257 }
258