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