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