xref: /petsc/src/mat/utils/gcreate.c (revision 58562591ed05f0d38367c4dd37eaee40e9027a05)
1 
2 #ifndef lint
3 static char vcid[] = "$Id: gcreate.c,v 1.100 1996/12/16 22:25:17 balay Exp balay $";
4 #endif
5 
6 #include "sys.h"
7 #include "mat.h"       /*I "mat.h"  I*/
8 
9 #undef __FUNC__
10 #define __FUNC__ "MatGetTypeFromOptions"
11 /*@C
12    MatGetTypeFromOptions - Determines from the options database what matrix
13    format the user has specified.
14 
15    Input Parameter:
16 .  comm - the MPI communicator
17 .  type - the type of matrix desired, for example MATSEQAIJ, MATMPIAIJ
18 .  pre - optional string to prepend to the name
19 
20    Output Parameters:
21 .  set - flag indicating whether user set matrix type option.
22 
23    Basic Options Database Keys:
24    These options return MATSEQxxx or MATMPIxxx,
25    depending on the communicator, comm.
26 $    -mat_aij      : AIJ type
27 $    -mat_baij     : block AIJ type
28 $    -mat_dense    : dense type
29 $    -mat_bdiag    : block diagonal type
30 
31    More Options Database Keys:
32 $    -mat_seqaij   : MATSEQAIJ
33 $    -mat_mpiaij   : MATMPIAIJ
34 $    -mat_seqbaij  : MATSEQBAIJ
35 $    -mat_mpibaij  : MATMPIBAIJ
36 $    -mat_seqbdiag : MATSEQBDAIG
37 $    -mat_mpibdiag : MATMPIBDIAG
38 $    -mat_mpirowbs : MATMPIROWBS
39 $    -mat_seqdense : MATSEQDENSE
40 $    -mat_mpidense : MATMPIDENSE
41 
42    Note:
43    This routine is automatically called within MatCreate() and MatLoad().
44 
45 .keywords: matrix, get, format, from, options
46 
47 .seealso: MatCreate(), MatLoad()
48 @*/
49 
50 int MatGetTypeFromOptions(MPI_Comm comm,char *pre,MatType *type,int *set)
51 {
52   int  size,flg1,flg2,flg3,flg4,flg5,flg8,flg9,flg10,flg12,flg13,ierr,flg11,flg14,flg15;
53   char p[64];
54 
55   PetscValidIntPointer(type); PetscValidIntPointer(set);
56 
57   PetscStrcpy(p,"-");
58   if (pre) PetscStrcat(p,pre);
59 
60   MPI_Comm_size(comm,&size);
61   ierr = OptionsHasName(PETSC_NULL,"-help",&flg1); CHKERRQ(ierr);
62   if (flg1) {
63     PetscPrintf(comm,"Matrix format options:\n");
64     PetscPrintf(comm,"  %smat_aij, %smat_seqaij, %smat_mpiaij\n",p,p,p);
65     PetscPrintf(comm,"  %smat_baij, %smat_seqbaij, %smat_mpibaij\n",p,p,p);
66     PetscPrintf(comm,"  %smat_dense, %smat_seqdense, %smat_mpidense\n",p,p,p);
67     PetscPrintf(comm,"  %smat_mpirowbs, %smat_bdiag, %smat_seqbdiag, %smat_mpibdiag\n",p,p,p,p);
68   }
69   ierr = OptionsHasName(pre,"-mat_seqdense",&flg1); CHKERRQ(ierr);
70   ierr = OptionsHasName(pre,"-mat_mpidense",&flg2); CHKERRQ(ierr);
71   ierr = OptionsHasName(pre,"-mat_seqbdiag",&flg3); CHKERRQ(ierr);
72   ierr = OptionsHasName(pre,"-mat_mpibdiag",&flg4); CHKERRQ(ierr);
73   ierr = OptionsHasName(pre,"-mat_mpirowbs",&flg5); CHKERRQ(ierr);
74   ierr = OptionsHasName(pre,"-mat_seqbaij",&flg11); CHKERRQ(ierr);
75   ierr = OptionsHasName(pre,"-mat_mpibaij",&flg14); CHKERRQ(ierr);
76   ierr = OptionsHasName(pre,"-mat_mpiaij",&flg8); CHKERRQ(ierr);
77   ierr = OptionsHasName(pre,"-mat_seqaij",&flg9); CHKERRQ(ierr);
78   ierr = OptionsHasName(pre,"-mat_aij",&flg10); CHKERRQ(ierr);
79   ierr = OptionsHasName(pre,"-mat_bdiag",&flg12); CHKERRQ(ierr);
80   ierr = OptionsHasName(pre,"-mat_dense",&flg13); CHKERRQ(ierr);
81   ierr = OptionsHasName(pre,"-mat_baij",&flg15); CHKERRQ(ierr);
82   if (flg1) {
83     *type = MATSEQDENSE;
84     *set = 1;
85   }
86   else if (flg2) {
87     *type = MATMPIDENSE;
88     *set = 1;
89   }
90   else if (flg3) {
91     *type = MATSEQBDIAG;
92     *set = 1;
93   }
94   else if (flg4) {
95     *type = MATMPIBDIAG;
96     *set = 1;
97   }
98   else if (flg5) {
99     *type = MATMPIROWBS;
100     *set = 1;
101   }
102   else if (flg8) {
103     *type = MATMPIAIJ;
104     *set = 1;
105   }
106   else if (flg9){
107     *type = MATSEQAIJ;
108     *set = 1;
109   }
110   else if (flg10) {
111     if (size == 1) *type = MATSEQAIJ;
112     else *type = MATMPIAIJ;
113     *set = 1;
114   }
115   else if (flg11) {
116     *type = MATSEQBAIJ;
117     *set = 1;
118   }
119   else if (flg12) {
120     if (size == 1) *type = MATSEQBDIAG;
121     else *type = MATMPIBDIAG;
122     *set = 1;
123   }
124   else if (flg13) {
125     if (size == 1) *type = MATSEQDENSE;
126     else *type = MATMPIDENSE;
127     *set = 1;
128   }
129   else if (flg14) {
130     *type = MATMPIBAIJ;
131     *set = 1;
132   }
133   else if (flg15) {
134     if (size == 1) *type = MATSEQBAIJ;
135     else *type = MATMPIBAIJ;
136     *set = 1;
137   }
138   else {
139     if (size == 1) *type = MATSEQAIJ;
140     else *type = MATMPIAIJ;
141     *set = 0;
142   }
143   return 0;
144 }
145 
146 #undef __FUNC__
147 #define __FUNC__ "MatCreate"
148 /*@C
149    MatCreate - Creates a matrix where the type is determined
150    from the options database. Generates a parallel MPI matrix if the
151    communicator has more than one processor.  The default matrix type is
152    AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ().
153 
154    Input Parameters:
155 .  m - number of global rows
156 .  n - number of global columns
157 .  comm - MPI communicator
158 
159    Output Parameter:
160 .  A - the matrix
161 
162    Basic Options Database Keys:
163    These options use MatCreateSeqXXX or MatCreateMPIXXX,
164    depending on the communicator, comm.
165 $    -mat_aij      : AIJ type
166 $    -mat_baij     : block AIJ type
167 $    -mat_dense    : dense type
168 $    -mat_bdiag    : block diagonal type
169 
170    More Options Database Keys:
171 $    -mat_seqaij   : AIJ type, uses MatCreateSeqAIJ()
172 $    -mat_mpiaij   : AIJ type, uses MatCreateMPIAIJ()
173 $    -mat_seqbdiag : block diagonal type, uses
174 $                    MatCreateSeqBDiag()
175 $    -mat_mpibdiag : block diagonal type, uses
176 $                    MatCreateMPIBDiag()
177 $    -mat_mpirowbs : rowbs type, uses MatCreateMPIRowbs()
178 $    -mat_seqdense : dense type, uses MatCreateSeqDense()
179 $    -mat_mpidense : dense type, uses MatCreateMPIDense()
180 $    -mat_seqbaij  : block AIJ type, uses MatCreateSeqBAIJ()
181 $    -mat_mpibaij  : block AIJ type, uses MatCreateMPIBAIJ()
182 
183    Even More Options Database Keys:
184    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
185    for additional format-specific options.
186 
187    Notes:
188    This routine calls MatGetTypeFromOptions() to determind the matrix type.
189 
190 .keywords: matrix, create
191 
192 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
193           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
194           MatCreateSeqDense(), MatCreateMPIDense(),
195           MatCreateMPIRowbs(), MatCreateSeqBAIJ,
196           MatConvert(), MatGetTypeFromOptions()
197  @*/
198 int MatCreate(MPI_Comm comm,int m,int n,Mat *A)
199 {
200   MatType type;
201   int     set, ierr, bs=1, flg;
202 
203   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
204   switch (type) {
205   case MATSEQDENSE:
206     ierr = MatCreateSeqDense(comm,m,n,PETSC_NULL,A); CHKERRQ(ierr);
207     break;
208   case MATMPIBDIAG:
209     ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
210            PETSC_NULL,PETSC_NULL,A); CHKERRQ(ierr);
211     break;
212   case MATSEQBDIAG:
213     ierr = MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
214            PETSC_NULL,A); CHKERRQ(ierr);
215     break;
216   case MATMPIROWBS:
217     ierr = MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,
218            PETSC_NULL,A); CHKERRQ(ierr);
219     break;
220   case MATMPIDENSE:
221     ierr = MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,A); CHKERRQ(ierr);
222     break;
223   case MATMPIAIJ:
224     ierr = MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
225            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
226     break;
227   case MATSEQBAIJ:
228     ierr = OptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,&flg); CHKERRQ(ierr);
229     ierr = MatCreateSeqBAIJ(comm,bs,m,n,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
230     break;
231   case MATMPIBAIJ:
232     ierr = OptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,&flg); CHKERRQ(ierr);
233     ierr = MatCreateMPIBAIJ(comm,bs,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
234     break;
235   default:
236     ierr = MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
237     break;
238   }
239   return 0;
240 }
241 
242 #include "src/mat/matimpl.h"
243 #undef __FUNC__
244 #define __FUNC__ "MatGetType"
245 /*@C
246    MatGetType - Gets the matrix type and name (as a string) from the matrix.
247 
248    Input Parameter:
249 .  mat - the matrix
250 
251    Output Parameter:
252 .  type - the matrix type (or use PETSC_NULL)
253 .  name - name of matrix type (or use PETSC_NULL)
254 
255 .keywords: matrix, get, type, name
256 @*/
257 int MatGetType(Mat mat,MatType *type,char **name)
258 {
259   int  itype = (int)mat->type;
260   char *matname[10];
261 
262   PetscValidHeaderSpecific(mat,MAT_COOKIE);
263 
264   if (type) *type = (MatType) mat->type;
265   if (name) {
266     /* Note:  Be sure that this list corresponds to the enum in mat.h */
267     matname[0] = "MATSEQDENSE";
268     matname[1] = "MATSEQAIJ";
269     matname[2] = "MATMPIAIJ";
270     matname[3] = "MATSHELL";
271     matname[4] = "MATMPIROWBS";
272     matname[5] = "MATSEQBDIAG";
273     matname[6] = "MATMPIBDIAG";
274     matname[7] = "MATMPIDENSE";
275     matname[8] = "MATSEQBAIJ";
276     matname[9] = "MATMPIBAIJ";
277 
278     if (itype < 0 || itype > 9) *name = "Unknown matrix type";
279     else                        *name = matname[itype];
280   }
281   return 0;
282 }
283 
284 
285 
286