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