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