xref: /petsc/src/mat/utils/gcreate.c (revision 2f8a729c95982efb3e50c82c45a5cc0e4d951579)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.58 1995/11/21 03:09:33 curfman 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;
31   char *p = "-";
32 
33   if (pre) p = pre;
34   MPI_Comm_size(comm,&size);
35   if (OptionsHasName(PetscNull,"-help")) {
36     MPIU_printf(comm,"Matrix format options:\n");
37     MPIU_printf(comm,"  %smat_aij, %smat_seqaij, %smat_mpiaij\n",p,p,p);
38     MPIU_printf(comm,"  %smat_row, %smat_seqrow, %smat_mpirow\n",p,p,p);
39     MPIU_printf(comm,"  %smat_dense, %smat_seqdense, %smat_mpidense\n",p,p,p);
40     MPIU_printf(comm,"  %smat_mpirowbs, %smat_bdiag, %smat_seqbdiag, %smat_mpibdiag\n",p,p,p,p);
41     /* We need to move the following to MatPrintHelp or some analogous routine */
42     MPIU_printf(comm,"More matrix options:\n");
43     MPIU_printf(comm,"  %smat_view_info : view basic matrix info during MatAssemblyEnd()\n",p);
44     MPIU_printf(comm,"  %smat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n",p);
45     MPIU_printf(comm,"  %smat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n",p);
46     MPIU_printf(comm,"      -pause <sec> : set seconds of display pause\n");
47     MPIU_printf(comm,"      -display <name> : set alternate display\n");
48   }
49   if (OptionsHasName(pre,"-mat_seqdense")) {
50     *type = MATSEQDENSE;
51     *set = 1;
52   }
53   else if (OptionsHasName(pre,"-mat_mpidense")) {
54     *type = MATMPIDENSE;
55     *set = 1;
56   }
57   else if (OptionsHasName(pre,"-mat_seqbdiag")) {
58     *type = MATSEQBDIAG;
59     *set = 1;
60   }
61   else if (OptionsHasName(pre,"-mat_mpibdiag")) {
62     *type = MATMPIBDIAG;
63     *set = 1;
64   }
65   else if (OptionsHasName(pre,"-mat_mpirowbs")) {
66     *type = MATMPIROWBS;
67     *set = 1;
68   }
69   else if (OptionsHasName(pre,"-mat_mpirow")) {
70     *type = MATMPIROW;
71     *set = 1;
72   }
73   else if (OptionsHasName(pre,"-mat_seqrow")){
74     *type = MATSEQROW;
75     *set = 1;
76   }
77   else if (OptionsHasName(pre,"-mat_mpiaij")) {
78     *type = MATMPIAIJ;
79     *set = 1;
80   }
81   else if (OptionsHasName(pre,"-mat_seqaij")){
82     *type = MATSEQAIJ;
83     *set = 1;
84   }
85   else if (OptionsHasName(pre,"-mat_aij")){
86     if (size == 1) *type = MATSEQAIJ;
87     else *type = MATMPIAIJ;
88     *set = 1;
89   }
90   else if (OptionsHasName(pre,"-mat_row")){
91     if (size == 1) *type = MATSEQROW;
92     else *type = MATMPIROW;
93     *set = 1;
94   }
95   else if (OptionsHasName(pre,"-mat_bdiag")){
96     if (size == 1) *type = MATSEQBDIAG;
97     else *type = MATMPIBDIAG;
98     *set = 1;
99   }
100   else if (OptionsHasName(pre,"-mat_dense")){
101     if (size == 1) *type = MATSEQDENSE;
102     else *type = MATMPIDENSE;
103     *set = 1;
104   }
105   else {
106     if (size == 1) *type = MATSEQAIJ;
107     else *type = MATMPIAIJ;
108     *set = 0;
109   }
110   return 0;
111 }
112 
113 /*@C
114    MatCreate - Creates a matrix, where the type is determined
115    from the options database. Generates a parallel MPI matrix if the
116    communicator has more than one processor.
117 
118    Input Parameters:
119 .  m - number of global rows
120 .  n - number of global columns
121 .  comm - MPI communicator
122 
123    Output Parameter:
124 .  V - location to stash resulting matrix
125 
126    Options Database Keywords:
127 $  -mat_seqaij   : AIJ type, uses MatCreateSeqAIJ
128 $  -mat_mpiaij   : AIJ type, uses MatCreateMPIAIJ
129 $  -mat_aij      : AIJ type, (Seq or MPI depending on comm)
130 $  -mat_seqrow   : row type, uses MatCreateSeqRow()
131 $  -mat_mpirow   : MatCreateMPIRow()
132 $  -mat_row      : row type, (Seq or MPI depending on comm)
133 $  -mat_seqbdiag : block diagonal type, uses
134 $                  MatCreateSeqBDiag()
135 $  -mat_mpibdiag : block diagonal type, uses
136 $                  MatCreateMPIBDiag()
137 $  -mat_bdiag    : block diagonal type,
138 $                  (Seq or MPI depending on comm)
139 $  -mat_mpirowbs : rowbs type, uses MatCreateMPIRowbs()
140 $  -mat_dense    : dense type, (Seq or MPI depending on comm)
141 $  -mat_mpidense : dense type, uses MatCreateSeqDense()
142 $  -mat_mpidense : dense type, uses MatCreateMPIDense()
143 
144    Notes:
145    The default matrix type is AIJ, using MatCreateSeqAIJ() and
146    MatCreateMPIAIJ().
147 
148 .keywords: matrix, create, initial
149 
150 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
151           MatCreateSeqRow(), MatCreateMPIRow(),
152           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
153           MatCreateSeqDense(), MatCreateMPIDense(),
154           MatCreateMPIRowbs(), MatConvert()
155           MatGetFormatFromOptions()
156  @*/
157 int MatCreate(MPI_Comm comm,int m,int n,Mat *V)
158 {
159   MatType type;
160   int     set,ierr;
161 
162   ierr = MatGetFormatFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
163   if (type == MATSEQDENSE) {
164     return MatCreateSeqDense(comm,m,n,0,V);
165   }
166   if (type == MATSEQBDIAG || type == MATMPIBDIAG) {
167     int nb = 1, ndiag = 0, ndiag2 = 0, *d = 0;
168     if (OptionsHasName(PetscNull,"-help")) {
169       MPIU_printf(comm,"Options with -mat_bdiag: -mat_bdiag_bsize <block_size>\n");
170       MPIU_printf(comm,"  -mat_bdiag_ndiag <number_diags> \n");
171       MPIU_printf(comm,"  -mat_bdiag_dvals <d1,d2,d3,...> (diagonal numbers)\n");
172       MPIU_printf(comm,"   (for example) -mat_bdiag_dvals -5,-1,0,1,5\n");
173     }
174     OptionsGetInt(PetscNull,"-mat_bdiag_bsize",&nb);
175     OptionsGetInt(PetscNull,"-mat_bdiag_ndiag",&ndiag);
176     if (ndiag) {
177       d = (int *)PetscMalloc( ndiag * sizeof(int) ); CHKPTRQ(d);
178       ndiag2 = ndiag;
179       OptionsGetIntArray(PetscNull,"-mat_bdiag_dvals",d,&ndiag2);
180       if (ndiag2 != ndiag)
181         SETERRQ(1,"MatCreate: Incompatible number of diags and diagonal vals");
182     } else if (OptionsHasName(PetscNull,"-mat_bdiag_dvals")) {
183       SETERRQ(1,"MatCreate: Must specify number of diagonals with -mat_bdiag_ndiag");
184     }
185     if (type == MATMPIBDIAG) {
186       ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,ndiag,nb,d,PetscNull,V); CHKERRQ(ierr);
187     } else {
188       ierr = MatCreateSeqBDiag(comm,m,n,ndiag,nb,d,PetscNull,V); CHKERRQ(ierr);
189     }
190     if (d) PetscFree(d);
191     return 0;
192   }
193   if (type == MATMPIROWBS) {
194     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,5,0,PetscNull,V);
195   }
196   if (type == MATMPIROW) {
197     return MatCreateMPIRow(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,0,0,0,V);
198   }
199   if (type == MATSEQROW) {
200     return MatCreateSeqRow(comm,m,n,10,0,V);
201   }
202   if (type == MATMPIDENSE) {
203     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,0,V);
204   }
205   if (type == MATMPIAIJ) {
206     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,0,0,0,V);
207   }
208   return MatCreateSeqAIJ(comm,m,n,10,0,V);
209 }
210 
211 #include "matimpl.h"
212 /*@C
213    MatGetName - Gets the matrix type name (as a string) from the matrix.
214 
215    Input Parameter:
216 .  mat - the matrix
217 
218    Output Parameter:
219 .  name - name of matrix type
220 
221 .keywords: matrix, get, name
222 
223 .seealso:  MatGetType()
224 @*/
225 int MatGetName(Mat mat,char **name)
226 {
227   int  itype = (int)mat->type;
228   char *matname[10];
229 
230   /* Note:  Be sure that this list corresponds to the enum in mat.h */
231   matname[0] = "MATSEQDENSE";
232   matname[1] = "MATSEQAIJ";
233   matname[2] = "MATMPIAIJ";
234   matname[3] = "MATSHELL";
235   matname[4] = "MATSEQROW";
236   matname[5] = "MATMPIROW";
237   matname[6] = "MATMPIROWBS";
238   matname[7] = "MATSEQBDIAG";
239   matname[8] = "MATMPIBDIAG";
240   matname[9] = "MATMPIDENSE";
241   if (itype < 0 || itype > 9) *name = "Unknown matrix type";
242   else                        *name = matname[itype];
243   return 0;
244 }
245 
246 
247 
248