xref: /petsc/src/mat/utils/gcreate.c (revision 9a28b0a686c82f833cf0123ded378bbf53aa0f9d)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.63 1996/01/02 20:17:03 bsmith Exp curfman $";
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   }
42   if (OptionsHasName(pre,"-mat_seqdense")) {
43     *type = MATSEQDENSE;
44     *set = 1;
45   }
46   else if (OptionsHasName(pre,"-mat_mpidense")) {
47     *type = MATMPIDENSE;
48     *set = 1;
49   }
50   else if (OptionsHasName(pre,"-mat_seqbdiag")) {
51     *type = MATSEQBDIAG;
52     *set = 1;
53   }
54   else if (OptionsHasName(pre,"-mat_mpibdiag")) {
55     *type = MATMPIBDIAG;
56     *set = 1;
57   }
58   else if (OptionsHasName(pre,"-mat_mpirowbs")) {
59     *type = MATMPIROWBS;
60     *set = 1;
61   }
62   else if (OptionsHasName(pre,"-mat_mpirow")) {
63     *type = MATMPIROW;
64     *set = 1;
65   }
66   else if (OptionsHasName(pre,"-mat_seqrow")){
67     *type = MATSEQROW;
68     *set = 1;
69   }
70   else if (OptionsHasName(pre,"-mat_mpiaij")) {
71     *type = MATMPIAIJ;
72     *set = 1;
73   }
74   else if (OptionsHasName(pre,"-mat_seqaij")){
75     *type = MATSEQAIJ;
76     *set = 1;
77   }
78   else if (OptionsHasName(pre,"-mat_aij")){
79     if (size == 1) *type = MATSEQAIJ;
80     else *type = MATMPIAIJ;
81     *set = 1;
82   }
83   else if (OptionsHasName(pre,"-mat_row")){
84     if (size == 1) *type = MATSEQROW;
85     else *type = MATMPIROW;
86     *set = 1;
87   }
88   else if (OptionsHasName(pre,"-mat_bdiag")){
89     if (size == 1) *type = MATSEQBDIAG;
90     else *type = MATMPIBDIAG;
91     *set = 1;
92   }
93   else if (OptionsHasName(pre,"-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,0,&type,&set); CHKERRQ(ierr);
156   if (type == MATSEQDENSE) {
157     return MatCreateSeqDense(comm,m,n,PETSC_NULL,V);
158   }
159   if (type == MATSEQBDIAG || type == MATMPIBDIAG) {
160     int nb = 1, ndiag = 0, ndiag2 = 0, *d = 0;
161     if (OptionsHasName(PETSC_NULL,"-help")) {
162       MPIU_printf(comm,"Options with -mat_bdiag, -mat_seqbdiag, -mat_mpibdiag:\n");
163       MPIU_printf(comm,"  -mat_bdiag_bsize <block_size>\n");
164       MPIU_printf(comm,"  -mat_bdiag_ndiag <number_diags> \n");
165       MPIU_printf(comm,"  -mat_bdiag_dvals <d1,d2,d3,...> (diagonal numbers)\n");
166       MPIU_printf(comm,"   (for example) -mat_bdiag_dvals -5,-1,0,1,5\n");
167     }
168     OptionsGetInt(PETSC_NULL,"-mat_bdiag_bsize",&nb);
169     OptionsGetInt(PETSC_NULL,"-mat_bdiag_ndiag",&ndiag);
170     if (ndiag) {
171       d = (int *)PetscMalloc( ndiag * sizeof(int) ); CHKPTRQ(d);
172       ndiag2 = ndiag;
173       OptionsGetIntArray(PETSC_NULL,"-mat_bdiag_dvals",d,&ndiag2);
174       if (ndiag2 != ndiag)
175         SETERRQ(1,"MatCreate: Incompatible number of diags and diagonal vals");
176     } else if (OptionsHasName(PETSC_NULL,"-mat_bdiag_dvals")) {
177       SETERRQ(1,"MatCreate: Must specify number of diagonals with -mat_bdiag_ndiag");
178     }
179     if (type == MATMPIBDIAG) {
180       ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,ndiag,nb,d,PETSC_NULL,V); CHKERRQ(ierr);
181     } else {
182       ierr = MatCreateSeqBDiag(comm,m,n,ndiag,nb,d,PETSC_NULL,V); CHKERRQ(ierr);
183     }
184     if (d) PetscFree(d);
185     return 0;
186   }
187   if (type == MATMPIROWBS) {
188     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,5,PETSC_NULL,PETSC_NULL,V);
189   }
190   if (type == MATMPIROW) {
191     return MatCreateMPIRow(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,PETSC_NULL,0,PETSC_NULL,V);
192   }
193   if (type == MATSEQROW) {
194     return MatCreateSeqRow(comm,m,n,10,PETSC_NULL,V);
195   }
196   if (type == MATMPIDENSE) {
197     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V);
198   }
199   if (type == MATMPIAIJ) {
200     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,5,PETSC_NULL,0,PETSC_NULL,V);
201   }
202   return MatCreateSeqAIJ(comm,m,n,10,PETSC_NULL,V);
203 }
204 
205 #include "matimpl.h"
206 /*@C
207    MatGetType - Gets the matrix type and name (as a string) from the matrix.
208 
209    Input Parameter:
210 .  mat - the matrix
211 
212    Output Parameter:
213 .  type - the matrix type (or use PETSC_NULL)
214 .  name - name of matrix type (or use PETSC_NULL)
215 
216 .keywords: matrix, get, name
217 @*/
218 int MatGetType(Mat mat,MatType *type,char **name)
219 {
220   int  itype = (int)mat->type;
221   char *matname[10];
222 
223   if (type) *type = (MatType) mat->type;
224   if (name) {
225     /* Note:  Be sure that this list corresponds to the enum in mat.h */
226     matname[0] = "MATSEQDENSE";
227     matname[1] = "MATSEQAIJ";
228     matname[2] = "MATMPIAIJ";
229     matname[3] = "MATSHELL";
230     matname[4] = "MATSEQROW";
231     matname[5] = "MATMPIROW";
232     matname[6] = "MATMPIROWBS";
233     matname[7] = "MATSEQBDIAG";
234     matname[8] = "MATMPIBDIAG";
235     matname[9] = "MATMPIDENSE";
236     if (itype < 0 || itype > 9) *name = "Unknown matrix type";
237     else                        *name = matname[itype];
238   }
239   return 0;
240 }
241 
242 
243 
244