xref: /petsc/src/mat/utils/gcreate.c (revision 4ee90b5ea4b1ea789a057ce2e6ee891e15dba83a)
1 /*$Id: gcreate.c,v 1.131 2001/07/20 21:22:13 bsmith Exp $*/
2 
3 #include "petscsys.h"
4 #include "src/mat/matimpl.h"       /*I "petscmat.h"  I*/
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    eorror 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 .keywords: matrix, create
82 
83 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
84           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
85           MatCreateSeqDense(), MatCreateMPIDense(),
86           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
87           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
88           MatConvert()
89 @*/
90 int MatCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
91 {
92   Mat B;
93 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
94   int ierr;
95 #endif
96 
97   PetscFunctionBegin;
98   PetscValidPointer(A);
99   *A = PETSC_NULL;
100 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
101   ierr = MatInitializePackage(PETSC_NULL);                                                                CHKERRQ(ierr);
102 #endif
103 
104   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
105   PetscLogObjectCreate(B);
106 
107   B->m = m;
108   B->n = n;
109   B->M = M;
110   B->N = N;
111 
112   B->preallocated  = PETSC_FALSE;
113   B->bops->publish = MatPublish_Base;
114   *A = B;
115   PetscFunctionReturn(0);
116 }
117 
118 #undef __FUNCT__
119 #define __FUNCT__ "MatSetFromOptions"
120 /*@C
121    MatSetFromOptions - Creates a matrix where the type is determined
122    from the options database. Generates a parallel MPI matrix if the
123    communicator has more than one processor.  The default matrix type is
124    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if
125    you do not select a type in the options database.
126 
127    Collective on Mat
128 
129    Input Parameter:
130 .  A - the matrix
131 
132    Options Database Keys:
133 +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
134 .    -mat_type mpiaij   - AIJ type, uses MatCreateMPIAIJ()
135 .    -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
136 .    -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
137 .    -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
138 .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
139 .    -mat_type mpidense - dense type, uses MatCreateMPIDense()
140 .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
141 -    -mat_type mpibaij  - block AIJ type, uses MatCreateMPIBAIJ()
142 
143    Even More Options Database Keys:
144    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
145    for additional format-specific options.
146 
147    Level: beginner
148 
149 .keywords: matrix, create
150 
151 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
152           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
153           MatCreateSeqDense(), MatCreateMPIDense(),
154           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
155           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
156           MatConvert()
157 @*/
158 int MatSetFromOptions(Mat B)
159 {
160   int        ierr,size;
161   char       mtype[256];
162   PetscTruth flg;
163 
164   PetscFunctionBegin;
165   ierr = PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
166   if (flg) {
167     ierr = MatSetType(B,mtype);CHKERRQ(ierr);
168   }
169   if (!B->type_name) {
170     ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
171     if (size == 1) {
172       ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
173     } else {
174       ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
175     }
176   }
177   PetscFunctionReturn(0);
178 }
179 
180 #undef __FUNCT__
181 #define __FUNCT__ "MatSetUpPreallocation"
182 /*@C
183    MatSetUpPreallocation
184 
185    Collective on Mat
186 
187    Input Parameter:
188 .  A - the matrix
189 
190    Level: beginner
191 
192 .keywords: matrix, create
193 
194 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
195           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
196           MatCreateSeqDense(), MatCreateMPIDense(),
197           MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
198           MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
199           MatConvert()
200 @*/
201 int MatSetUpPreallocation(Mat B)
202 {
203   int        ierr;
204 
205   PetscFunctionBegin;
206   if (B->ops->setuppreallocation) {
207     PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
208     ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
209     B->ops->setuppreallocation = 0;
210     B->preallocated            = PETSC_TRUE;
211   }
212   PetscFunctionReturn(0);
213 }
214 
215 /*
216         Copies from Cs header to A
217 */
218 #undef __FUNCT__
219 #define __FUNCT__ "MatHeaderCopy"
220 int MatHeaderCopy(Mat A,Mat C)
221 {
222   int         ierr,refct;
223   PetscOps    *Abops;
224   MatOps      Aops;
225   char        *mtype,*mname;
226 
227   PetscFunctionBegin;
228   /* free all the interior data structures from mat */
229   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
230 
231   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
232   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
233 
234   /* save the parts of A we need */
235   Abops = A->bops;
236   Aops  = A->ops;
237   refct = A->refct;
238   mtype = A->type_name;
239   mname = A->name;
240 
241   /* copy C over to A */
242   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
243 
244   /* return the parts of A we saved */
245   A->bops      = Abops;
246   A->ops       = Aops;
247   A->qlist     = 0;
248   A->refct     = refct;
249   A->type_name = mtype;
250   A->name      = mname;
251 
252   PetscLogObjectDestroy(C);
253   PetscHeaderDestroy(C);
254   PetscFunctionReturn(0);
255 }
256