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