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