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