xref: /petsc/src/mat/utils/gcreate.c (revision 3f3760d9f26c0cf679475d2d7a7a670d761122a5)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.64 1996/01/03 14:45:55 curfman 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   if (type == MATMPIBDIAG)
159     return MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
160            PETSC_NULL,PETSC_NULL,V);
161   if (type == MATSEQBDIAG)
162     return MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
163            PETSC_NULL,V);
164   if (type == MATMPIROWBS)
165     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,PETSC_NULL,V);
166   if (type == MATMPIROW)
167     return MatCreateMPIRow(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
168            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V);
169   if (type == MATSEQROW)
170     return MatCreateSeqRow(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V);
171   if (type == MATMPIDENSE)
172     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V);
173   if (type == MATMPIAIJ)
174     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
175            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V);
176   return MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V);
177 }
178 
179 #include "matimpl.h"
180 /*@C
181    MatGetType - Gets the matrix type and name (as a string) from the matrix.
182 
183    Input Parameter:
184 .  mat - the matrix
185 
186    Output Parameter:
187 .  type - the matrix type (or use PETSC_NULL)
188 .  name - name of matrix type (or use PETSC_NULL)
189 
190 .keywords: matrix, get, name
191 @*/
192 int MatGetType(Mat mat,MatType *type,char **name)
193 {
194   int  itype = (int)mat->type;
195   char *matname[10];
196 
197   if (type) *type = (MatType) mat->type;
198   if (name) {
199     /* Note:  Be sure that this list corresponds to the enum in mat.h */
200     matname[0] = "MATSEQDENSE";
201     matname[1] = "MATSEQAIJ";
202     matname[2] = "MATMPIAIJ";
203     matname[3] = "MATSHELL";
204     matname[4] = "MATSEQROW";
205     matname[5] = "MATMPIROW";
206     matname[6] = "MATMPIROWBS";
207     matname[7] = "MATSEQBDIAG";
208     matname[8] = "MATMPIBDIAG";
209     matname[9] = "MATMPIDENSE";
210     if (itype < 0 || itype > 9) *name = "Unknown matrix type";
211     else                        *name = matname[itype];
212   }
213   return 0;
214 }
215 
216 
217 
218