xref: /petsc/src/mat/utils/gcreate.c (revision b4fd42875b350b60e141cbdf895ae7195e067e9f)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.60 1995/12/12 22:55:20 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(PETSC_NULL,"-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,PETSC_NULL,V);
165   }
166   if (type == MATSEQBDIAG || type == MATMPIBDIAG) {
167     int nb = 1, ndiag = 0, ndiag2 = 0, *d = 0;
168     if (OptionsHasName(PETSC_NULL,"-help")) {
169       MPIU_printf(comm,"Options with -mat_bdiag, -mat_seqbdiag, -mat_mpibdiag:\n");
170       MPIU_printf(comm,"  -mat_bdiag_bsize <block_size>\n");
171       MPIU_printf(comm,"  -mat_bdiag_ndiag <number_diags> \n");
172       MPIU_printf(comm,"  -mat_bdiag_dvals <d1,d2,d3,...> (diagonal numbers)\n");
173       MPIU_printf(comm,"   (for example) -mat_bdiag_dvals -5,-1,0,1,5\n");
174     }
175     OptionsGetInt(PETSC_NULL,"-mat_bdiag_bsize",&nb);
176     OptionsGetInt(PETSC_NULL,"-mat_bdiag_ndiag",&ndiag);
177     if (ndiag) {
178       d = (int *)PetscMalloc( ndiag * sizeof(int) ); CHKPTRQ(d);
179       ndiag2 = ndiag;
180       OptionsGetIntArray(PETSC_NULL,"-mat_bdiag_dvals",d,&ndiag2);
181       if (ndiag2 != ndiag)
182         SETERRQ(1,"MatCreate: Incompatible number of diags and diagonal vals");
183     } else if (OptionsHasName(PETSC_NULL,"-mat_bdiag_dvals")) {
184       SETERRQ(1,"MatCreate: Must specify number of diagonals with -mat_bdiag_ndiag");
185     }
186     if (type == MATMPIBDIAG) {
187       ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,ndiag,nb,d,PETSC_NULL,V); CHKERRQ(ierr);
188     } else {
189       ierr = MatCreateSeqBDiag(comm,m,n,ndiag,nb,d,PETSC_NULL,V); CHKERRQ(ierr);
190     }
191     if (d) PetscFree(d);
192     return 0;
193   }
194   if (type == MATMPIROWBS) {
195     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,5,PETSC_NULL,PETSC_NULL,V);
196   }
197   if (type == MATMPIROW) {
198     return MatCreateMPIRow(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,PETSC_NULL,0,PETSC_NULL,V);
199   }
200   if (type == MATSEQROW) {
201     return MatCreateSeqRow(comm,m,n,10,PETSC_NULL,V);
202   }
203   if (type == MATMPIDENSE) {
204     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V);
205   }
206   if (OptionsHasName(PETSC_NULL,"-help")) {
207     MPIU_printf(comm,"Options with default formats (-mat_aij, -mat_seqaij, -mat_mpiaij):\n");
208     MPIU_printf(comm,"  -mat_aij_no_inode : Do not use inodes\n");
209     MPIU_printf(comm,"  -mat_aij_inode_limit <limit> : Set inode limit (max limit=5)\n");
210   }
211   if (type == MATMPIAIJ) {
212     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,PETSC_NULL,0,PETSC_NULL,V);
213   }
214   return MatCreateSeqAIJ(comm,m,n,10,PETSC_NULL,V);
215 }
216 
217 #include "matimpl.h"
218 /*@C
219    MatGetName - Gets the matrix type name (as a string) from the matrix.
220 
221    Input Parameter:
222 .  mat - the matrix
223 
224    Output Parameter:
225 .  name - name of matrix type
226 
227 .keywords: matrix, get, name
228 
229 .seealso:  MatGetType()
230 @*/
231 int MatGetName(Mat mat,char **name)
232 {
233   int  itype = (int)mat->type;
234   char *matname[10];
235 
236   /* Note:  Be sure that this list corresponds to the enum in mat.h */
237   matname[0] = "MATSEQDENSE";
238   matname[1] = "MATSEQAIJ";
239   matname[2] = "MATMPIAIJ";
240   matname[3] = "MATSHELL";
241   matname[4] = "MATSEQROW";
242   matname[5] = "MATMPIROW";
243   matname[6] = "MATMPIROWBS";
244   matname[7] = "MATSEQBDIAG";
245   matname[8] = "MATMPIBDIAG";
246   matname[9] = "MATMPIDENSE";
247   if (itype < 0 || itype > 9) *name = "Unknown matrix type";
248   else                        *name = matname[itype];
249   return 0;
250 }
251 
252 
253 
254