xref: /petsc/src/mat/utils/gcreate.c (revision ee0de897fec99be7c10c7637bbd068d61b2ce625)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.66 1996/01/12 03:54:11 bsmith Exp bsmith $";
3 #endif
4 
5 #include "sys.h"
6 #include "mat.h"       /*I "mat.h"  I*/
7 
8 /*@C
9    MatGetFormatFromOptions - 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 MatGetFormatFromOptions(MPI_Comm comm,char *pre,MatType *type,int *set)
29 {
30   int  size,flg1,flg2,flg3,flg4,flg5,flg6,flg7,flg8,flg9,flg10,flg11,flg12,flg13,ierr;
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     MPIU_printf(comm,"Matrix format options:\n");
40     MPIU_printf(comm,"  %smat_aij, %smat_seqaij, %smat_mpiaij\n",p,p,p);
41     MPIU_printf(comm,"  %smat_row, %smat_seqrow, %smat_mpirow\n",p,p,p);
42     MPIU_printf(comm,"  %smat_dense, %smat_seqdense, %smat_mpidense\n",p,p,p);
43     MPIU_printf(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_mpirow",&flg6); CHKERRQ(ierr);
51   ierr = OptionsHasName(pre,"-mat_seqrow",&flg7); 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_row",&flg11); CHKERRQ(ierr);
56   ierr = OptionsHasName(pre,"-mat_bdiag",&flg12); CHKERRQ(ierr);
57   ierr = OptionsHasName(pre,"-mat_dense",&flg13); 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 (flg6) {
79     *type = MATMPIROW;
80     *set = 1;
81   }
82   else if (flg7){
83     *type = MATSEQROW;
84     *set = 1;
85   }
86   else if (flg8) {
87     *type = MATMPIAIJ;
88     *set = 1;
89   }
90   else if (flg9){
91     *type = MATSEQAIJ;
92     *set = 1;
93   }
94   else if (flg10){
95     if (size == 1) *type = MATSEQAIJ;
96     else *type = MATMPIAIJ;
97     *set = 1;
98   }
99   else if (flg11){
100     if (size == 1) *type = MATSEQROW;
101     else *type = MATMPIROW;
102     *set = 1;
103   }
104   else if (flg12){
105     if (size == 1) *type = MATSEQBDIAG;
106     else *type = MATMPIBDIAG;
107     *set = 1;
108   }
109   else if (flg13){
110     if (size == 1) *type = MATSEQDENSE;
111     else *type = MATMPIDENSE;
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.
126 
127    Input Parameters:
128 .  m - number of global rows
129 .  n - number of global columns
130 .  comm - MPI communicator
131 
132    Output Parameter:
133 .  V - location to stash resulting matrix
134 
135    Options Database Keywords:
136 $  -mat_seqaij   : AIJ type, uses MatCreateSeqAIJ
137 $  -mat_mpiaij   : AIJ type, uses MatCreateMPIAIJ
138 $  -mat_aij      : AIJ type, (Seq or MPI depending on comm)
139 $  -mat_seqrow   : row type, uses MatCreateSeqRow()
140 $  -mat_mpirow   : MatCreateMPIRow()
141 $  -mat_row      : row type, (Seq or MPI depending on comm)
142 $  -mat_seqbdiag : block diagonal type, uses
143 $                  MatCreateSeqBDiag()
144 $  -mat_mpibdiag : block diagonal type, uses
145 $                  MatCreateMPIBDiag()
146 $  -mat_bdiag    : block diagonal type,
147 $                  (Seq or MPI depending on comm)
148 $  -mat_mpirowbs : rowbs type, uses MatCreateMPIRowbs()
149 $  -mat_dense    : dense type, (Seq or MPI depending on comm)
150 $  -mat_mpidense : dense type, uses MatCreateSeqDense()
151 $  -mat_mpidense : dense type, uses MatCreateMPIDense()
152 
153    Notes:
154    The default matrix type is AIJ, using MatCreateSeqAIJ() and
155    MatCreateMPIAIJ().
156 
157 .keywords: matrix, create, initial
158 
159 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
160           MatCreateSeqRow(), MatCreateMPIRow(),
161           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
162           MatCreateSeqDense(), MatCreateMPIDense(),
163           MatCreateMPIRowbs(), MatConvert()
164           MatGetFormatFromOptions()
165  @*/
166 int MatCreate(MPI_Comm comm,int m,int n,Mat *V)
167 {
168   MatType type;
169   int     set,ierr;
170 
171   ierr = MatGetFormatFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
172   if (type == MATSEQDENSE)
173     return MatCreateSeqDense(comm,m,n,PETSC_NULL,V);
174   if (type == MATMPIBDIAG)
175     return MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
176            PETSC_NULL,PETSC_NULL,V);
177   if (type == MATSEQBDIAG)
178     return MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
179            PETSC_NULL,V);
180   if (type == MATMPIROWBS)
181     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,PETSC_NULL,V);
182   if (type == MATMPIROW)
183     return MatCreateMPIRow(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
184            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V);
185   if (type == MATSEQROW)
186     return MatCreateSeqRow(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V);
187   if (type == MATMPIDENSE)
188     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V);
189   if (type == MATMPIAIJ)
190     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
191            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V);
192   return MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V);
193 }
194 
195 #include "matimpl.h"
196 /*@C
197    MatGetType - Gets the matrix type and name (as a string) from the matrix.
198 
199    Input Parameter:
200 .  mat - the matrix
201 
202    Output Parameter:
203 .  type - the matrix type (or use PETSC_NULL)
204 .  name - name of matrix type (or use PETSC_NULL)
205 
206 .keywords: matrix, get, name
207 @*/
208 int MatGetType(Mat mat,MatType *type,char **name)
209 {
210   int  itype = (int)mat->type;
211   char *matname[10];
212 
213   if (type) *type = (MatType) mat->type;
214   if (name) {
215     /* Note:  Be sure that this list corresponds to the enum in mat.h */
216     matname[0] = "MATSEQDENSE";
217     matname[1] = "MATSEQAIJ";
218     matname[2] = "MATMPIAIJ";
219     matname[3] = "MATSHELL";
220     matname[4] = "MATSEQROW";
221     matname[5] = "MATMPIROW";
222     matname[6] = "MATMPIROWBS";
223     matname[7] = "MATSEQBDIAG";
224     matname[8] = "MATMPIBDIAG";
225     matname[9] = "MATMPIDENSE";
226     if (itype < 0 || itype > 9) *name = "Unknown matrix type";
227     else                        *name = matname[itype];
228   }
229   return 0;
230 }
231 
232 
233 
234