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