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