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